Skip to content

Commit

Permalink
Merge pull request #884 from isamu/tool_calls
Browse files Browse the repository at this point in the history
Tool calls
  • Loading branch information
isamu authored Jan 13, 2025
2 parents 47d6dc6 + 3cab1ac commit 7e147aa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
35 changes: 20 additions & 15 deletions llm_agents/openai_agent/lib/openai_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,37 @@ exports.openAIMockAgent = exports.openAIAgent = void 0;
const openai_1 = __importDefault(require("openai"));
const graphai_1 = require("graphai");
const llm_utils_1 = require("@graphai/llm_utils");
const convToolCall = (tool_call) => {
return {
id: tool_call.id,
name: tool_call.function.name,
arguments: (() => {
try {
return JSON.parse(tool_call.function.arguments);
}
catch (__e) {
console.log(__e);
return undefined;
}
})(),
};
};
const convertOpenAIChatCompletion = (response, messages) => {
const message = response?.choices[0] && response?.choices[0].message ? response?.choices[0].message : null;
const text = message && message.content ? message.content : null;
const functionResponse = message?.tool_calls && message?.tool_calls[0] ? message?.tool_calls[0] : null;
const functionResponses = message?.tool_calls && Array.isArray(message?.tool_calls) ? message?.tool_calls : [];
const functionResponse = functionResponses[0] ? functionResponses[0] : null;
// const functionId = message?.tool_calls && message?.tool_calls[0] ? message?.tool_calls[0]?.id : null;
const tool = functionResponse
? {
id: functionResponse.id,
name: functionResponse?.function?.name,
arguments: (() => {
try {
return JSON.parse(functionResponse?.function?.arguments);
}
catch (__e) {
return undefined;
}
})(),
}
: undefined;
const tool = functionResponse ? convToolCall(functionResponse) : undefined;
const tool_calls = functionResponses.map(convToolCall);
if (message) {
messages.push(message);
}
return {
...response,
text,
tool,
tool_calls,
message,
messages,
};
Expand Down
2 changes: 1 addition & 1 deletion llm_agents/openai_agent/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@graphai/openai_agent",
"version": "0.2.2",
"version": "0.2.3",
"description": "OpenAI agents for GraphAI.",
"main": "lib/index.js",
"files": [
Expand Down
35 changes: 21 additions & 14 deletions llm_agents/openai_agent/src/openai_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,32 @@ type OpenAIParams = OpenAIInputs & OpenAIConfig;

type OpenAIResult = Record<string, any> | string;

const convToolCall = (tool_call: OpenAI.Chat.Completions.ChatCompletionMessageToolCall) => {
return {
id: tool_call.id,
name: tool_call.function.name,
arguments: (() => {
try {
return JSON.parse(tool_call.function.arguments);
} catch (__e) {
console.log(__e);
return undefined;
}
})(),
};
};

const convertOpenAIChatCompletion = (response: OpenAI.ChatCompletion, messages: OpenAI.ChatCompletionMessageParam[]) => {
const message = response?.choices[0] && response?.choices[0].message ? response?.choices[0].message : null;
const text = message && message.content ? message.content : null;

const functionResponse = message?.tool_calls && message?.tool_calls[0] ? message?.tool_calls[0] : null;
const functionResponses = message?.tool_calls && Array.isArray(message?.tool_calls) ? message?.tool_calls : [];
const functionResponse = functionResponses[0] ? functionResponses[0] : null;

// const functionId = message?.tool_calls && message?.tool_calls[0] ? message?.tool_calls[0]?.id : null;

const tool = functionResponse
? {
id: functionResponse.id,
name: functionResponse?.function?.name,
arguments: (() => {
try {
return JSON.parse(functionResponse?.function?.arguments);
} catch (__e) {
return undefined;
}
})(),
}
: undefined;
const tool = functionResponse ? convToolCall(functionResponse) : undefined;
const tool_calls = functionResponses.map(convToolCall);

if (message) {
messages.push(message);
Expand All @@ -54,6 +60,7 @@ const convertOpenAIChatCompletion = (response: OpenAI.ChatCompletion, messages:
...response,
text,
tool,
tool_calls,
message,
messages,
};
Expand Down

0 comments on commit 7e147aa

Please sign in to comment.