Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: type definitions and usages of @xsai/tool #34

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,4 @@
"pcss",
"postcss"
],

// Enable unstable_ts_config
"eslint.options": {
"flags": ["unstable_ts_config"]
}
}
4 changes: 2 additions & 2 deletions packages/generate-object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export interface GenerateObjectOptions<T extends Schema> extends GenerateTextOpt
schemaName?: string
}

// TODO: toolCalls, toolResults
export interface GenerateObjectResult<T extends Schema> extends Omit<GenerateTextResult, 'text' | 'toolCalls' | 'toolResults'> {
// TODO: toolCalls
export interface GenerateObjectResult<T extends Schema> extends Omit<GenerateTextResult, 'text' | 'toolCalls'> {
object: Infer<T>
}

Expand Down
3 changes: 2 additions & 1 deletion packages/generate-text/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"test:watch": "vitest"
},
"dependencies": {
"@xsai/shared-chat": "workspace:"
"@xsai/shared-chat": "workspace:",
"@xsai/tool": "workspace:"
},
"devDependencies": {
"@xsai/providers": "workspace:"
Expand Down
59 changes: 20 additions & 39 deletions packages/generate-text/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {
type AssistantMessageResponse,
chat,
type ChatOptions,
type FinishReason,
type Message,
type Tool,
type Usage,
import type {
AssistantMessageResponse,
ChatOptions,
FinishReason,
Message,
Usage,
} from '@xsai/shared-chat'
import type { ToolCall } from '@xsai/tool'

import { chat } from '@xsai/shared-chat'

export interface GenerateTextOptions extends ChatOptions {
/** @default 1 */
Expand Down Expand Up @@ -37,7 +38,6 @@ export interface GenerateTextResult {
steps: StepResult[]
text?: string
toolCalls: ToolCall[]
toolResults: ToolResult[]
usage: Usage
}

Expand All @@ -46,24 +46,9 @@ export interface StepResult {
// TODO: step type
// type: 'continue' | 'initial' | 'tool-result'
toolCalls: ToolCall[]
toolResults: ToolResult[]
usage: Usage
}

export interface ToolCall {
args: string
toolCallId: string
toolCallType: 'function'
toolName: string
}

export interface ToolResult {
args: Record<string, unknown>
result: string
toolCallId: string
toolName: string
}

/** @internal */
type RawGenerateText = (options: GenerateTextOptions) => RawGenerateTextTrampoline<GenerateTextResult>

Expand All @@ -84,15 +69,13 @@ const rawGenerateText: RawGenerateText = async (options: GenerateTextOptions) =>
const messages: Message[] = options.messages
const steps: StepResult[] = options.steps ?? []
const toolCalls: ToolCall[] = []
const toolResults: ToolResult[] = []

const { finish_reason: finishReason, message } = choices[0]

if (message.content || !message.tool_calls || steps.length >= (options.maxSteps ?? 1)) {
const step: StepResult = {
text: message.content,
toolCalls,
toolResults,
usage,
}

Expand All @@ -115,22 +98,21 @@ const rawGenerateText: RawGenerateText = async (options: GenerateTextOptions) =>
id: toolCallId,
type: toolCallType,
} of message.tool_calls) {
const tool = (options.tools as Tool[]).find(tool => tool.function.name === toolName)!
const tool = options.tools?.find(tool => tool.function.name === toolName)
if (!tool) {
continue
}

const parsedArgs: Record<string, unknown> = JSON.parse(toolArgs)
const result = await tool.execute(parsedArgs)
const result = await tool.execute(parsedArgs as never)

toolCalls.push({
args: toolArgs,
toolCallId,
toolCallType,
toolName,
})

toolResults.push({
args: parsedArgs,
id: toolCallId,
name: toolName,
parameters: toolArgs,
parsedParameters: parsedArgs,
result,
toolCallId,
toolName,
type: toolCallType,
})

messages.push({
Expand All @@ -143,7 +125,6 @@ const rawGenerateText: RawGenerateText = async (options: GenerateTextOptions) =>
const step: StepResult = {
text: message.content,
toolCalls,
toolResults,
usage,
}

Expand Down
3 changes: 1 addition & 2 deletions packages/generate-text/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('@xsai/generate-text', () => {
it('basic', async () => {
let step: StepResult | undefined

const { finishReason, steps, text, toolCalls, toolResults } = await generateText({
const { finishReason, steps, text, toolCalls } = await generateText({
...ollama.chat('llama3.2'),
messages: [
{
Expand All @@ -27,7 +27,6 @@ describe('@xsai/generate-text', () => {
expect(text).toStrictEqual('YES')
expect(finishReason).toBe('stop')
expect(toolCalls.length).toBe(0)
expect(toolResults.length).toBe(0)
expect(steps).toMatchSnapshot()

expect(steps[0]).toStrictEqual(step)
Expand Down
3 changes: 2 additions & 1 deletion packages/shared-chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"build:watch": "pkgroll --watch"
},
"dependencies": {
"@xsai/shared": "workspace:"
"@xsai/shared": "workspace:",
"@xsai/tool": "workspace:"
}
}
2 changes: 2 additions & 0 deletions packages/shared-chat/src/types/chat-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CommonRequestOptions } from '@xsai/shared'
import type { Tool } from '@xsai/tool'

import type { Message } from './message'
import type { ToolChoice } from './tool-choice'
Expand All @@ -7,4 +8,5 @@ export interface ChatOptions extends CommonRequestOptions {
[key: string]: unknown
messages: Message[]
toolChoice?: ToolChoice
tools?: Tool<never>[]
}
1 change: 0 additions & 1 deletion packages/shared-chat/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ export type * from './chat-options'
export type * from './finish-reason'
export type * from './message'
export type * from './message-part'
export type * from './tool'
export type * from './tool-choice'
export type * from './usage'
10 changes: 0 additions & 10 deletions packages/shared-chat/src/types/tool.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/shared-chat/src/utils/chat.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { requestBody, requestHeaders, requestURL, responseCatch } from '@xsai/shared'

import type { ChatOptions, Tool } from '../types'
import type { ChatOptions } from '../types'

export const chat = async <T extends ChatOptions>(options: T) =>
await (options.fetch ?? globalThis.fetch)(requestURL('chat/completions', options.baseURL), {
body: requestBody({
...options,
tools: (options.tools as Tool[] | undefined)?.map(tool => ({
tools: options.tools?.map(tool => ({
function: tool.function,
type: 'function',
})),
Expand Down
3 changes: 0 additions & 3 deletions packages/tool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./generate-text": {
"types": "./dist/generate-text.d.ts"
}
},
"files": [
Expand Down
11 changes: 0 additions & 11 deletions packages/tool/src/generate-text.ts

This file was deleted.

33 changes: 21 additions & 12 deletions packages/tool/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@ import type { InferIn, Schema } from '@typeschema/main'
import { toJSONSchema } from '@typeschema/main'
import { clean } from '@xsai/shared'

export interface ToolOptions<T extends Schema> {
description?: string
execute: (input: InferIn<T>) => Promise<string> | string
name: string
parameters: T
strict?: boolean
}

export interface ToolResult<T extends Schema> {
execute: (input: InferIn<T>) => Promise<string> | string
export interface Tool<T extends Schema> {
execute: (input: unknown extends InferIn<T> ? never : InferIn<T>) => Promise<string> | string
function: {
description?: string
name: string
parameters: Record<string, unknown>
parameters: InferIn<T>
strict?: boolean
}
type: 'function'
}

export const tool = async <T extends Schema>(options: ToolOptions<T>): Promise<ToolResult<T>> => ({
export interface ToolCall {
id: string
name: string
parameters: string
parsedParameters: Record<string, unknown>
result: string
type: 'function'
}

export interface ToolOptions<T extends Schema> {
description?: string
execute: (input: InferIn<T>) => Promise<string> | string
name: string
parameters: T
strict?: boolean
}

export const tool = async <T extends Schema>(options: ToolOptions<T>): Promise<Tool<T>> => ({
execute: options.execute,
function: {
description: options.description,
Expand Down
12 changes: 5 additions & 7 deletions packages/tool/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,11 @@ describe('@xsai/tool', () => {

expect(text).toMatchSnapshot()

const { toolCalls, toolResults } = steps[0]
const { toolCalls } = steps[0]

expect(toolCalls[0].toolName).toBe('weather')
expect(toolCalls[0].args).toBe('{"location":"San Francisco"}')

expect(toolCalls[0].toolName).toBe('weather')
expect(toolResults[0].args).toStrictEqual({ location: 'San Francisco' })
expect(toolResults[0].result).toBe('{"location":"San Francisco","temperature":42}')
expect(toolCalls[0].name).toBe('weather')
expect(toolCalls[0].parameters).toBe('{"location":"San Francisco"}')
expect(toolCalls[0].parsedParameters).toStrictEqual({ location: 'San Francisco' })
expect(toolCalls[0].result).toBe('{"location":"San Francisco","temperature":42}')
}, 20000)
})
2 changes: 1 addition & 1 deletion packages/xsai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ export { listModels, type ListModelsOptions, type ListModelsResponse, type Model
export * from '@xsai/shared-chat'
export { streamObject, type StreamObjectOptions, type StreamObjectResult } from '@xsai/stream-object'
export { type ChunkResult, streamText, type StreamTextOptions, type StreamTextResult } from '@xsai/stream-text'
export { tool, type ToolOptions, type ToolResult } from '@xsai/tool'
export { tool, type Tool, type ToolCall, type ToolOptions } from '@xsai/tool'
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading