diff --git a/examples/job-catalog/src/openai.ts b/examples/job-catalog/src/openai.ts index 35be1fa490..dd6a84ef2a 100644 --- a/examples/job-catalog/src/openai.ts +++ b/examples/job-catalog/src/openai.ts @@ -10,6 +10,47 @@ 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 2022 with some related links to read more about it", + } +] + +const get_current_affairs = async (query: string) => { + //Call to google search api + // const results = await fetch(`https://www.googleapis.com/customsearch/v1?${query}`); + return 'Argentina national football team' +} + +const available_functions: Record Promise> = { + get_current_affairs: get_current_affairs, +}; + const openai = new OpenAI({ id: "openai", apiKey: process.env["OPENAI_API_KEY"]!, @@ -74,6 +115,44 @@ client.defineJob({ model: "text-embedding-ada-002", input: "The food was delicious and the waiter...", }); + + //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", + messages: chatMessages, + functions, + function_call: "auto" + }); + return chatCompletion + } + + const chatCompletion = await createCompletion(); + // 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]; + //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(); + + //This will contain the final result + await io.logger.info(finalCompletion?.choices[0].message.content?.toString() || ''); + } }, });