add support for tool call (function call)
This commit is contained in:
@@ -12,6 +12,11 @@ export interface Message {
|
||||
role: "system" | "user" | "assistant" | "function";
|
||||
content: string | MessageDetail[];
|
||||
name?: "example_user" | "example_assistant";
|
||||
tool_calls?: {
|
||||
id: string;
|
||||
type: string;
|
||||
function: any;
|
||||
}[];
|
||||
}
|
||||
export const getMessageText = (message: Message): string => {
|
||||
if (typeof message.content === "string") {
|
||||
@@ -78,6 +83,7 @@ class Chat {
|
||||
OPENAI_API_KEY: string;
|
||||
messages: Message[];
|
||||
sysMessageContent: string;
|
||||
toolsString: string;
|
||||
total_tokens: number;
|
||||
max_tokens: number;
|
||||
max_gen_tokens: number;
|
||||
@@ -96,6 +102,7 @@ class Chat {
|
||||
OPENAI_API_KEY: string | undefined,
|
||||
{
|
||||
systemMessage = "",
|
||||
toolsString = "",
|
||||
max_tokens = 4096,
|
||||
max_gen_tokens = 2048,
|
||||
enable_max_gen_tokens = true,
|
||||
@@ -121,6 +128,7 @@ class Chat {
|
||||
this.enable_max_gen_tokens = enable_max_gen_tokens;
|
||||
this.tokens_margin = tokens_margin;
|
||||
this.sysMessageContent = systemMessage;
|
||||
this.toolsString = toolsString;
|
||||
this.apiEndpoint = apiEndPoint;
|
||||
this.model = model;
|
||||
this.temperature = temperature;
|
||||
@@ -178,6 +186,25 @@ class Chat {
|
||||
body["max_tokens"] = this.max_gen_tokens;
|
||||
}
|
||||
|
||||
// parse toolsString to function call format
|
||||
const ts = this.toolsString.trim();
|
||||
if (ts) {
|
||||
try {
|
||||
const fcList: any[] = JSON.parse(ts);
|
||||
body["tools"] = fcList.map((fc) => {
|
||||
return {
|
||||
type: "function",
|
||||
function: fc,
|
||||
};
|
||||
});
|
||||
} catch (e) {
|
||||
console.log("toolsString parse error");
|
||||
throw (
|
||||
"Function call toolsString parse error, not a valied json list: " + e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return fetch(this.apiEndpoint, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -234,7 +261,7 @@ class Chat {
|
||||
}
|
||||
}
|
||||
|
||||
processFetchResponse(resp: FetchResponse): string {
|
||||
processFetchResponse(resp: FetchResponse): Message {
|
||||
if (resp.error !== undefined) {
|
||||
throw JSON.stringify(resp.error);
|
||||
}
|
||||
@@ -249,15 +276,19 @@ class Chat {
|
||||
this.forgetSomeMessages();
|
||||
}
|
||||
|
||||
return (
|
||||
(resp?.choices[0]?.message?.content as string) ??
|
||||
`Error: ${JSON.stringify(resp)}`
|
||||
);
|
||||
}
|
||||
let content = "";
|
||||
if (
|
||||
!resp.choices[0]?.message?.content &&
|
||||
!resp.choices[0]?.message?.tool_calls
|
||||
) {
|
||||
content = `Unparsed response: ${JSON.stringify(resp)}`;
|
||||
}
|
||||
|
||||
async complete(): Promise<string> {
|
||||
const resp = await this.fetch();
|
||||
return this.processFetchResponse(resp);
|
||||
return {
|
||||
role: "assistant",
|
||||
content,
|
||||
tool_calls: resp?.choices[0]?.message?.tool_calls,
|
||||
};
|
||||
}
|
||||
|
||||
completeWithSteam() {
|
||||
|
||||
Reference in New Issue
Block a user