-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CallChatWithFunctions - functions support, incl. OpenAI Implementation
May be rough on the edges, but should not create issues. The implementation is defensive, excessively validates the return types as the OpenAI API is brittle and can easily misbehave
- Loading branch information
Showing
8 changed files
with
193 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,49 @@ | ||
import { DLLMId } from '~/modules/llms/llm.types'; | ||
import { findVendorById } from '~/modules/llms/vendor.registry'; | ||
import { useModelsStore } from '~/modules/llms/store-llms'; | ||
|
||
import { DLLM, DLLMId } from './llm.types'; | ||
import { OpenAI } from './openai/openai.types'; | ||
import { findVendorById } from './vendor.registry'; | ||
import { useModelsStore } from './store-llms'; | ||
|
||
|
||
export type ModelVendorCallChatFn = (llm: DLLM, messages: VChatMessageIn[], maxTokens?: number) => Promise<VChatMessageOut>; | ||
export type ModelVendorCallChatWithFunctionsFn = (llm: DLLM, messages: VChatMessageIn[], functions: VChatFunctionIn[], maxTokens?: number) => Promise<VChatMessageOrFunctionCallOut>; | ||
|
||
export interface VChatMessageIn { | ||
role: 'assistant' | 'system' | 'user'; // | 'function'; | ||
content: string; | ||
//name?: string; // when role: 'function' | ||
} | ||
|
||
export type VChatFunctionIn = OpenAI.Wire.ChatCompletion.RequestFunctionDef; | ||
|
||
export interface VChatMessageOut { | ||
role: 'assistant' | 'system' | 'user'; | ||
content: string; | ||
finish_reason: 'stop' | 'length' | null; | ||
} | ||
|
||
export interface VChatFunctionCallOut { | ||
function_name: string; | ||
function_arguments: object | null; | ||
} | ||
|
||
export type VChatMessageOrFunctionCallOut = VChatMessageOut | VChatFunctionCallOut; | ||
|
||
export async function callChatGenerate(llmId: DLLMId, messages: OpenAI.Wire.ChatCompletion.RequestMessage[], maxTokens?: number): Promise<OpenAI.API.Chat.Response> { | ||
|
||
// get the vendor | ||
|
||
export async function callChatGenerate(llmId: DLLMId, messages: VChatMessageIn[], maxTokens?: number): Promise<VChatMessageOut> { | ||
const { llm, vendor } = getLLMAndVendorOrThrow(llmId); | ||
return await vendor.callChat(llm, messages, maxTokens); | ||
} | ||
|
||
export async function callChatGenerateWithFunctions(llmId: DLLMId, messages: VChatMessageIn[], functions: VChatFunctionIn[], maxTokens?: number): Promise<VChatMessageOrFunctionCallOut> { | ||
const { llm, vendor } = getLLMAndVendorOrThrow(llmId); | ||
return await vendor.callChatWithFunctions(llm, messages, functions, maxTokens); | ||
} | ||
|
||
|
||
function getLLMAndVendorOrThrow(llmId: string) { | ||
const llm = useModelsStore.getState().llms.find(llm => llm.id === llmId); | ||
const vendor = findVendorById(llm?._source.vId); | ||
if (!llm || !vendor) throw new Error(`callChat: Vendor not found for LLM ${llmId}`); | ||
|
||
// go for it | ||
return await vendor.callChat(llm, messages, maxTokens); | ||
return { llm, vendor }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
2d4c0e9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
big-agi – ./
big-agi-enricoros.vercel.app
get.big-agi.com
big-agi-git-main-enricoros.vercel.app