feat: function call support

pull/588/head
Joe 2023-06-17 19:59:41 +08:00
rodzic bfc707c396
commit 33a7ceeda3
2 zmienionych plików z 94 dodań i 2 usunięć

Wyświetl plik

@ -140,7 +140,9 @@ export class ChatGPTAPI {
onProgress,
stream = onProgress ? true : false,
completionParams,
isFineTune
isFineTune,
functions,
function_call
} = opts
let { abortSignal } = opts
@ -173,7 +175,8 @@ export class ChatGPTAPI {
role: 'assistant',
id: uuidv4(),
parentMessageId: messageId,
text: ''
text: '',
finish_reason: ''
}
const responseP = new Promise<types.ChatMessage>(
@ -190,6 +193,8 @@ export class ChatGPTAPI {
max_tokens: maxTokens,
...this._completionParams,
...completionParams,
functions,
function_call,
stream
} as any
@ -228,12 +233,14 @@ export class ChatGPTAPI {
}
if (response?.choices?.length) {
const choice = response?.choices[0]
const delta = isFineTune
? {
content: (response.choices[0] as any).text,
role: 'assistant' as any
}
: response.choices[0].delta
result.delta = delta.content
if (delta?.content) result.text += delta.content
result.detail = response
@ -242,6 +249,26 @@ export class ChatGPTAPI {
result.role = delta.role
}
const functionCall = (delta as any)
?.function_call as types.FunctionCall
if (functionCall) {
if (!result.functionCall) {
result.functionCall = {
arguments: '',
name: ''
}
}
if (functionCall.arguments) {
result.functionCall.arguments += functionCall.arguments
}
if (functionCall.name) {
result.functionCall.name = functionCall.name
}
}
result.finish_reason = choice?.finish_reason
onProgress?.(result)
}
} catch (err) {

Wyświetl plik

@ -32,6 +32,50 @@ export type ChatGPTAPIOptions = {
fetch?: FetchFn
}
export interface ChatCompletionRequestMessageFunctionCall {
/**
* The name of the function to call.
* @type {string}
*/
name?: string
/**
* The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
* @type {string}
*/
arguments?: string
}
export interface ChatCompletionFunctions {
/**
* The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
* @type {string}
*/
name: string
/**
* The description of what the function does.
* @type {string}
*/
description?: string
/**
* The parameters the functions accepts, described as a JSON Schema object. See the [guide](/docs/guides/gpt/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format.
* @type {{ [key: string]: any; }}
*/
parameters?: { [key: string]: any }
}
export interface CreateChatCompletionRequestFunctionCallOneOf {
/**
* The name of the function to call.
* @type {string}
* @memberof CreateChatCompletionRequestFunctionCallOneOf
*/
name: string
}
export type CreateChatCompletionRequestFunctionCall =
| CreateChatCompletionRequestFunctionCallOneOf
| string
export type SendMessageOptions = {
/** The name of a user in a multi-user chat. */
name?: string
@ -47,6 +91,18 @@ export type SendMessageOptions = {
Omit<openai.CreateChatCompletionRequest, 'messages' | 'n' | 'stream'>
>
isFineTune?: boolean
/**
* A list of functions the model may generate JSON inputs for.
* @type {Array<ChatCompletionFunctions>}
* @memberof CreateChatCompletionRequest
*/
functions?: Array<ChatCompletionFunctions>
/**
*
* @type {CreateChatCompletionRequestFunctionCall}
* @memberof CreateChatCompletionRequest
*/
function_call?: CreateChatCompletionRequestFunctionCall
}
export type MessageActionType = 'next' | 'variant'
@ -68,11 +124,17 @@ export interface ChatMessage {
name?: string
delta?: string
detail?: any
finish_reason?: string
// relevant for both ChatGPTAPI and ChatGPTUnofficialProxyAPI
parentMessageId?: string
// only relevant for ChatGPTUnofficialProxyAPI
conversationId?: string
functionCall?: {
arguments?: string
name?: string
}
}
export class ChatGPTError extends Error {
@ -175,6 +237,8 @@ export type MessageContent = {
export type MessageMetadata = any
export type FunctionCall = { arguments: string; name: string }
export namespace openai {
export interface CreateChatCompletionDeltaResponse {
id: string
@ -186,6 +250,7 @@ export namespace openai {
delta: {
role: Role
content?: string
function_call?: FunctionCall
}
index: number
finish_reason: string | null