Comparing version 0.7.0 to 0.7.1
{ | ||
"name": "iudex", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "Iudex client", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -28,2 +28,3 @@ # Iudex client | ||
api to be able to call. For Iudex all parameters except `messages` is ignored. | ||
Functions only need to be uploaded once. | ||
@@ -37,23 +38,55 @@ ```typescript | ||
/* 1. Instantiate client */ | ||
const iudex = new Iudex({ apiKey: process.env.IUDEX_API_KEY }); | ||
// Message history | ||
const messagesHist: OpenAI.ChatCompletionMessageParam[] = []; | ||
// Log when messages are pushed | ||
const messages = { | ||
push: (...items: OpenAI.ChatCompletionMessageParam[]) => { | ||
console.log('new message:', items); | ||
messagesHist.push(...items); | ||
/* 2. Upload function json schemas */ | ||
await iudex.uploadFunctions([ | ||
{ | ||
name: 'getCurrentWeather', | ||
description: 'Gets the current weather', | ||
parameters: { | ||
type: 'object', | ||
properties: { | ||
location: { | ||
type: 'string', | ||
description: 'The city and state, e.g. San Francisco, CA', | ||
}, | ||
unit: { type: 'string', enum: ['celsius', 'fahrenheit'] }, | ||
}, | ||
required: ['location'], | ||
}, | ||
}, | ||
value: messagesHist, | ||
]); | ||
// Example function defined in code | ||
function getCurrentWeather({ location, unit }: { location: string; unit: string }) { | ||
if (location.toLowerCase().includes('tokyo')) { | ||
return { location: 'Tokyo', temperature: '10', unit: 'celsius' }; | ||
} else if (location.toLowerCase().includes('san francisco')) { | ||
return { location: 'San Francisco', temperature: '72', unit: 'fahrenheit' }; | ||
} else if (location.toLowerCase().includes('paris')) { | ||
return { location: 'Paris', temperature: '22', unit: 'fahrenheit' }; | ||
} else { | ||
return { location, temperature: 'unknown' }; | ||
} | ||
} | ||
/* 3. Create a way to reference functions using strings */ | ||
const fnMap: Record<string, (...args: any[]) => any> = { | ||
getCurrentWeather, | ||
}; | ||
// Initial request | ||
messages.push({ | ||
role: 'user', | ||
content: `what is ubers revenue in yen, use edgar`, | ||
}); | ||
// Push message to pick a tool | ||
/* 4. Track message history */ | ||
const messages = createMessages(); | ||
/* 5. Initial message goes here */ | ||
messages.push({ role: 'user', content: `what is the weather in San Francisco?` }); | ||
/* 6. Wait for AI response for which function to call */ | ||
messages.push(await iudex.chat.completions.create({ | ||
@@ -67,12 +100,12 @@ model: 'gpt-4-turbo-preview', | ||
/* 7. Keep calling functions until the initial message request is resolved */ | ||
let toolMessage = _.last(messages.value); | ||
// Loop while the latest message contains the AI's request for a function to be called | ||
while (toolMessage && messageHasToolCall(toolMessage)) { | ||
const { | ||
function: { name: fnName, arguments: fnArgs }, | ||
id: tool_call_id, | ||
} = toolMessage.tool_calls[0]; | ||
// Extract tool_call_id, function name, and function arguments | ||
const { function: fnCall, id: tool_call_id } = toolMessage.tool_calls[0]; | ||
const { name: fnName, arguments: fnArgs } = fnCall; | ||
// Call the function | ||
// const fnReturn = await fnMap[fnName](JSON.parse(fnArgs)); | ||
const fnReturn = 'stubbed value'; | ||
const fnReturn = await fnMap[fnName](JSON.parse(fnArgs)); | ||
@@ -96,7 +129,28 @@ // Push function return to message history | ||
console.log('FINISHED'); | ||
/* 8. Print final result */ | ||
console.log('FINISHED', toolMessage); | ||
// Helpers | ||
//// Helpers | ||
// Helper message object that also outputs to console | ||
class Messages { | ||
messagesHist: OpenAI.ChatCompletionMessageParam[] = []; | ||
push = (...items: OpenAI.ChatCompletionMessageParam[]) => { | ||
console.log('new message:', items); | ||
this.messagesHist.push(...items); | ||
}; | ||
get value() { | ||
return this.messagesHist; | ||
} | ||
} | ||
function createMessages() { | ||
return new Messages(); | ||
} | ||
// Helper to check if a message has a tool call | ||
type OpenAIToolCallMessage = OpenAI.ChatCompletionAssistantMessageParam | ||
@@ -103,0 +157,0 @@ & { tool_calls: OpenAI.ChatCompletionMessageToolCall[] }; |
62509
161