-
-
Notifications
You must be signed in to change notification settings - Fork 960
feat: Added example for openAI function call #475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<string, (query: string) => Promise<string>> = { | ||
| 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 () => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work, because the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the first call, openAI returns JSON which contains the function name and arguments Example: Using this we need to call our functions. and pass the result to GPT again for summarization. so we need 2 calls. |
||
| const chatCompletion = await io.openai.createChatCompletion("chat-completion", { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use the new way of doing this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't understand this part @ericallam, Do I need to use await io.openai.chats.completions.create instead of await io.openai.createChatCompletion ? |
||
| 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() || ''); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.