Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions examples/job-catalog/src/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]!,
Expand Down Expand Up @@ -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 () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work, because the key param is the same on each call. Not sure why it's necessary to do two calls to createChatCompetion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:
{'index': 0,
'message': {'role': 'assistant',
'content': None,
'function_call': {'name': 'get_n_day_weather_forecast',
'arguments': '{\n "location": "Glasgow, Scotland",\n "format": "celsius",\n "num_days": 5\n}'}},
'finish_reason': 'function_call'}

Using this we need to call our functions. and pass the result to GPT again for summarization. so we need 2 calls.

Reference: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_call_functions_with_chat_models.ipynb

const chatCompletion = await io.openai.createChatCompletion("chat-completion", {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use the new way of doing this io.openai.chats.completions.create

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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() || '');
}
},
});

Expand Down