From 98d76cef359017b80b9fdfa46de028adbd7b8096 Mon Sep 17 00:00:00 2001 From: Ram Date: Thu, 14 Sep 2023 10:46:42 +0530 Subject: [PATCH 1/2] feat: Added example for openAI function call --- examples/job-catalog/src/openai.ts | 85 ++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/examples/job-catalog/src/openai.ts b/examples/job-catalog/src/openai.ts index 35be1fa4907..c74c6244933 100644 --- a/examples/job-catalog/src/openai.ts +++ b/examples/job-catalog/src/openai.ts @@ -10,6 +10,59 @@ export const client = new TriggerClient({ ioLogLocalEnabled: true, }); +const functions = [ + { + "name": "get_current_affairs", + "description": "Get the current affairs from google", + "parameters": { + "type": "object", + "properties": { + "query": { + "type": "string", + "description": "query to search for google result", + } + }, + "required": ["query"], + }, + } +] + +type ChatMessage = { + role: 'system' | 'user' | 'assistant' | 'function'; + content: string; + name?: string; + function_call?: () => void +}; + +const chatMessages: ChatMessage[] = [ + { + role: "user", + content: "who won fifa world cup 2023 with some related links to read more about it", + } +] + +const get_current_affairs = async (query: string) => { + const params = { + key: process.env["GOOGLE_CUSTOM_SEARCH_API_KEY"] || '', + cx: process.env["googleCSEId"] || '', + q: query + } + const queryString = new URLSearchParams(params).toString(); + const apiUrl = `https://www.googleapis.com/customsearch/v1?${queryString}`; + const response = await fetch(apiUrl); + const data = await response.json(); + const results = data?.items?.slice(0, 5)?.map((item: any) => ({ + title: item.title, + link: item.link, + snippet: item.snippet, + })) || [] + return JSON.stringify(results) +} + +const available_functions: { [key: string]: (query: string) => Promise } = { + get_current_affairs: get_current_affairs, +}; + const openai = new OpenAI({ id: "openai", apiKey: process.env["OPENAI_API_KEY"]!, @@ -74,6 +127,38 @@ client.defineJob({ model: "text-embedding-ada-002", input: "The food was delicious and the waiter...", }); + + //Function call example for OpenAI + const createCompletion = async () => { + const chatCompletion = await io.openai.createChatCompletion("chat-completion", { + model: "gpt-3.5-turbo", + messages: chatMessages, + functions, + function_call: "auto" + }); + return chatCompletion + } + + const chatCompletion = await createCompletion(); + + // @ts-ignore + const response = chatCompletion?.data?.choices[0].message; + + if ('function_call' in response) { + const function_name: string = "get_current_affairs"; + const function_call = available_functions[function_name]; + const args = JSON.parse(response['function_call']['arguments']) + // @ts-ignore + const fn_response = await function_call(...Object.values(args)); + chatMessages.push({ + role: 'function', + name: function_name, + content: fn_response + }) + const finalCompletion = await createCompletion(); + // @ts-ignore + await io.logger.info(finalCompletion?.data?.choices[0].message); + } }, }); From 238a1705ebabefd7c378e9144e270043d90a4e1a Mon Sep 17 00:00:00 2001 From: Ram Date: Fri, 15 Sep 2023 21:29:49 +0530 Subject: [PATCH 2/2] fix: Added more comments and ts error fixed --- examples/job-catalog/src/openai.ts | 44 +++++++++++++----------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/examples/job-catalog/src/openai.ts b/examples/job-catalog/src/openai.ts index c74c6244933..dd6a84ef2a9 100644 --- a/examples/job-catalog/src/openai.ts +++ b/examples/job-catalog/src/openai.ts @@ -37,29 +37,17 @@ type ChatMessage = { const chatMessages: ChatMessage[] = [ { role: "user", - content: "who won fifa world cup 2023 with some related links to read more about it", + content: "who won fifa world cup 2022 with some related links to read more about it", } ] const get_current_affairs = async (query: string) => { - const params = { - key: process.env["GOOGLE_CUSTOM_SEARCH_API_KEY"] || '', - cx: process.env["googleCSEId"] || '', - q: query - } - const queryString = new URLSearchParams(params).toString(); - const apiUrl = `https://www.googleapis.com/customsearch/v1?${queryString}`; - const response = await fetch(apiUrl); - const data = await response.json(); - const results = data?.items?.slice(0, 5)?.map((item: any) => ({ - title: item.title, - link: item.link, - snippet: item.snippet, - })) || [] - return JSON.stringify(results) + //Call to google search api + // const results = await fetch(`https://www.googleapis.com/customsearch/v1?${query}`); + return 'Argentina national football team' } -const available_functions: { [key: string]: (query: string) => Promise } = { +const available_functions: Record Promise> = { get_current_affairs: get_current_affairs, }; @@ -129,6 +117,7 @@ client.defineJob({ }); //Function call example for OpenAI + //Send the conversation and available functions to GPT const createCompletion = async () => { const chatCompletion = await io.openai.createChatCompletion("chat-completion", { model: "gpt-3.5-turbo", @@ -140,24 +129,29 @@ client.defineJob({ } const chatCompletion = await createCompletion(); - - // @ts-ignore - const response = chatCompletion?.data?.choices[0].message; + // This response will contain JSON which contains function name and args + // that you can use to call the function in your code. + const response = chatCompletion?.choices[0].message; if ('function_call' in response) { const function_name: string = "get_current_affairs"; const function_call = available_functions[function_name]; - const args = JSON.parse(response['function_call']['arguments']) - // @ts-ignore - const fn_response = await function_call(...Object.values(args)); + //Get arguments from GPT response + const args = JSON.parse(response['function_call']?.['arguments'] || '{}'); + + //Call the function in our code using the arguments from openAI + const fn_response = await function_call(...Object.values(args) as [string]); + + // Extend conversation with function result & Get new response from GPT chatMessages.push({ role: 'function', name: function_name, content: fn_response }) const finalCompletion = await createCompletion(); - // @ts-ignore - await io.logger.info(finalCompletion?.data?.choices[0].message); + + //This will contain the final result + await io.logger.info(finalCompletion?.choices[0].message.content?.toString() || ''); } }, });