Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Feat ChatGPT LLM Api Tools A.ka Function Calling #86

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
57 changes: 56 additions & 1 deletion app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,51 @@ export class ChatGPTApi implements LLMApi {
}
}

/** Function Calling
* Author : @H0llyW00dzZ
* Todo : Function Calling which user can customize it easily
* This method should be a member of the ChatGPTApi class, not nested inside another method
**/
private getFunctionCalling(
model: string,
istools: boolean,
tools?: any
): { isGPTModel: boolean; tools?: any } {
const isGPTModel = model.includes("-1106") && istools === true;
if (isGPTModel) {
return {
/**
* Note : This just Sample Payload
**/
tools: tools || [
{
type: "function",
function: {
name: "get_current_weather",
description: "Get the current weather in a given location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The state and country, e.g. Bali, Indonesia",
},
unit: { type: "string", enum: ["celsius", "fahrenheit"] },
},
required: ["location"],
},
},
},
],
isGPTModel: true,
};
} else {
return {
isGPTModel: false,
};
}
}

async chat(options: ChatOptions) {
const textmoderation = useAppConfig.getState().textmoderation;
const latest = OpenaiPath.TextModerationModels.latest;
Expand Down Expand Up @@ -193,6 +238,11 @@ export class ChatGPTApi implements LLMApi {
modelConfig.system_fingerprint
);

const { isGPTModel, tools } = this.getFunctionCalling(
defaultModel,
modelConfig.istools ?? false,
);

const requestPayloads = {
chat: {
messages,
Expand All @@ -207,6 +257,10 @@ export class ChatGPTApi implements LLMApi {
...{ max_tokens }, // Spread the max_tokens value
// not yet ready
//...{ system_fingerprint }, // Spread the system_fingerprint value
// bug there is no response in text/event-stream from assistant after answer
// "Could you please provide me with the specific state and country for which you would like to get the current weather information?"
// might cause still beta
//...{ tools }, // Spread the tools payload
},
image: {
model: actualModel,
Expand All @@ -222,6 +276,7 @@ export class ChatGPTApi implements LLMApi {
* Author : @H0llyW00dzZ
**/
const magicPayload = this.getNewStuff(defaultModel);
const toolsPayload = this.getFunctionCalling(defaultModel, tools);

if (defaultModel.startsWith("dall-e")) {
console.log("[Request] openai payload: ", {
Expand Down Expand Up @@ -420,7 +475,7 @@ export class ChatGPTApi implements LLMApi {
try {
const resJson = await res.clone().json();
extraInfo = prettyObject(resJson);
} catch {}
} catch { }

if (res.status === 401) {
responseTexts.push(Locale.Error.Unauthorized);
Expand Down
1 change: 1 addition & 0 deletions app/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
UPDATE_URL,
} from "../constant";
import { Prompt, SearchService, usePromptStore } from "../store/prompt";
import { Tool, useToolStore, ToolSearchService } from "../store/tools";
import { ErrorBoundary } from "./error";
import { InputRange } from "./input-range";
import { useNavigate } from "react-router-dom";
Expand Down
18 changes: 10 additions & 8 deletions app/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ export enum SlotID {
AppBody = "app-body",
CustomModel = "custom-model",
}
// This will automatically generate JSON files without the need to include the ".json" extension.

export enum FileName {
Masks = "masks.json",
Prompts = "prompts.json",
Tool = "tools.json",
}

export enum StoreKey {
Chat = "chat-next-web-store",
Access = "access-control",
Config = "app-config",
Mask = "mask-store",
Prompt = "prompt-store",
Update = "chat-update",
Sync = "sync",
Chat = "chat-next-web-store",
Access = "access-control",
Config = "app-config",
Mask = "mask-store",
Prompt = "prompt-store",
Update = "chat-update",
Sync = "sync",
Tool = "tool-store",
}

export const DEFAULT_SIDEBAR_WIDTH = 300;
Expand Down
18 changes: 18 additions & 0 deletions app/locales/cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,24 @@ const cn = {
SubTitle:
"生成图像的风格\n必须是生动或自然之一\n此配置仅适用于dall-e-3",
},
FunctionCall: {
Title: "工具,又称函数调用",
Subtitle: "一种将大型语言模型与外部工具连接的工具,也称为函数调用。",
PropsContent: {
List: "工具列表",
ListCount: (builtin: number, custom: number) =>
`内置${builtin}个,自定义${custom}个`,
Edit: "编辑",
Modal: {
Title: "工具列表",
Add: "添加一个",
Search: "搜索工具",
},
EditModal: {
Title: "编辑工具",
},
},
},
},
Store: {
DefaultTopic: "新的聊天",
Expand Down
18 changes: 18 additions & 0 deletions app/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,24 @@ const en: LocaleType = {
SubTitle:
"A style of the generated images\nMust be one of vivid or natural\nThis Configuration is only supported for dall-e-3",
},
FunctionCall: {
Title: "Tools aka Function Calling",
Subtitle: "A tools aka function calling that connect large language models to external tools.",
PropsContent: {
List: "Tools List",
ListCount: (builtin: number, custom: number) =>
`${builtin} built-in, ${custom} user-defined`,
Edit: "Edit",
Modal: {
Title: "Tools List",
Add: "Add One",
Search: "Search Tools",
},
EditModal: {
Title: "Edit Tools",
},
},
},
},
Store: {
DefaultTopic: "New Conversation",
Expand Down
18 changes: 18 additions & 0 deletions app/locales/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,24 @@ const id: PartialLocaleType = {
SubTitle:
"Gaya gambar yang dihasilkan\nHarus menjadi salah satu dari cerah atau alami\nKonfigurasi ini hanya didukung untuk dall-e-3",
},
FunctionCall: {
Title: "Pemanggilan Fungsi Alat",
Subtitle: "Sebuah alat pemanggilan fungsi yang menghubungkan model bahasa besar ke alat eksternal.",
PropsContent: {
List: "Daftar Alat",
ListCount: (builtin: number, custom: number) =>
`${builtin} bawaan, ${custom} buatan pengguna`,
Edit: "Edit",
Modal: {
Title: "Daftar Alat",
Add: "Tambah Satu",
Search: "Cari Alat",
},
EditModal: {
Title: "Edit Alat",
},
},
},
},
Store: {
DefaultTopic: "Percakapan Baru",
Expand Down
13 changes: 12 additions & 1 deletion app/store/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export const DEFAULT_CONFIG = {
*/
style: "vivid", // Only DALL·E-3 for DALL·E-2 not not really needed
system_fingerprint: "",
/** Tools Aka Function Call OpenAI
* Author: @H0llyW00dzZ
**/
istools: false,
sendMemory: true,
historyMessageCount: 4,
compressMessageLengthThreshold: 1000,
Expand Down Expand Up @@ -182,7 +186,7 @@ export const useAppConfig = createPersistStore(
}),
{
name: StoreKey.Config,
version: 4.2, // DALL·E Models switching version to 4.1 because in 4.0 @Yidadaa using it.
version: 4.3, // DALL·E Models switching version to 4.1 because in 4.0 @Yidadaa using it.
migrate(persistedState, version) {
const state = persistedState as ChatConfig;

Expand Down Expand Up @@ -236,6 +240,13 @@ export const useAppConfig = createPersistStore(
};
}

if (version < 4.3) {
state.modelConfig = {
...state.modelConfig,
istools: false,
};
}

return state as any;
},
},
Expand Down
Loading