Skip to content

Commit

Permalink
refactor: migrate to xsai (#22)
Browse files Browse the repository at this point in the history
* refactor: openai-xsai
* refactor: fully migrate to xsai
* fix: ut

---------

Co-authored-by: Neko Ayaka <[email protected]>
Co-authored-by: 藍+85CD <[email protected]>
  • Loading branch information
3 people authored Dec 18, 2024
1 parent 55b7c10 commit fc4201a
Show file tree
Hide file tree
Showing 28 changed files with 725 additions and 803 deletions.
5 changes: 3 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
VITE_OPENAI_API_KEY=''
VITE_OPENAI_API_BASEURL=''
OPENAI_API_BASEURL=''
OPENAI_API_KEY=''
SERPAPI_API_KEY=''
1 change: 1 addition & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dictionaries: []
words:
- antfu
- bumpp
- Cmpl
- defu
- execa
- guiiai
Expand Down
10 changes: 5 additions & 5 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"docs:preview": "vitepress preview"
},
"devDependencies": {
"@unocss/eslint-config": "^0.65.0",
"@unocss/eslint-plugin": "^0.65.0",
"unocss": "^0.65.0",
"vite": "^6.0.2",
"vite-plugin-inspect": "^0.10.0",
"@unocss/eslint-config": "^0.65.1",
"@unocss/eslint-plugin": "^0.65.1",
"unocss": "^0.65.1",
"vite": "^6.0.3",
"vite-plugin-inspect": "^0.10.3",
"vitepress": "^1.5.0",
"vue": "^3.5.13"
}
Expand Down
4 changes: 2 additions & 2 deletions examples/neuri/documentations-from-unit-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"version": "0.0.15",
"private": true,
"packageManager": "pnpm@9.14.4",
"packageManager": "pnpm@9.15.0",
"description": "",
"author": "",
"license": "ISC",
Expand All @@ -14,7 +14,7 @@
},
"dependencies": {
"neuri": "workspace:^",
"openai": "^4.74.0"
"openai": "^4.77.0"
},
"devDependencies": {
"tsx": "^4.19.2"
Expand Down
8 changes: 4 additions & 4 deletions examples/neuri/weather-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"version": "0.0.15",
"private": true,
"packageManager": "pnpm@9.14.4",
"packageManager": "pnpm@9.15.0",
"description": "",
"author": "",
"license": "ISC",
Expand All @@ -18,9 +18,9 @@
"@typeschema/valibot": "^0.14.0",
"@typeschema/zod": "^0.14.0",
"neuri": "workspace:^",
"openai": "^4.74.0",
"valibot": "^0.42.1",
"zod": "^3.23.8"
"openai": "^4.77.0",
"valibot": "^1.0.0-beta.9",
"zod": "^3.24.1"
},
"devDependencies": {
"tsx": "^4.19.2"
Expand Down
4 changes: 2 additions & 2 deletions examples/neuri/weather-query/src/valibot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
composeAgent,
defineToolFunction,

resolveFirstTextMessageFromCompletion,
resolveFirstTextContentFromChatCmpl,
system,
toolFunction,
user,
Expand Down Expand Up @@ -82,7 +82,7 @@ async function main() {
model: 'openai/gpt-3.5-turbo',
})

return resolveFirstTextMessageFromCompletion(res)
return resolveFirstTextContentFromChatCmpl(res)
}

// eslint-disable-next-line no-console
Expand Down
4 changes: 2 additions & 2 deletions examples/neuri/weather-query/src/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { env } from 'node:process'
import {
composeAgent,
defineToolFunction,
resolveFirstTextMessageFromCompletion,
resolveFirstTextContentFromChatCmpl,
system,
toolFunction,
user,
Expand Down Expand Up @@ -82,7 +82,7 @@ async function main() {
model: 'openai/gpt-3.5-turbo',
})

return resolveFirstTextMessageFromCompletion(res)
return resolveFirstTextContentFromChatCmpl(res)
}

// eslint-disable-next-line no-console
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@antfu/eslint-config": "^3.11.2",
"@antfu/eslint-config": "^3.12.0",
"@antfu/ni": "^0.23.1",
"@types/node": "^22.10.1",
"@types/node": "^22.10.2",
"@vitest/coverage-v8": "2.1.8",
"bumpp": "^9.8.1",
"eslint": "^9.16.0",
"bumpp": "^9.9.1",
"eslint": "^9.17.0",
"typescript": "^5.7.2",
"unbuild": "^3.0.0-rc.11",
"vite": "^6.0.2",
"unbuild": "3.0.0-rc.11",
"vite": "^6.0.3",
"vitest": "^2.1.8"
},
"workspaces": [
Expand Down
12 changes: 6 additions & 6 deletions packages/format-code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@
},
"dependencies": {
"@guiiai/logg": "^1.0.6",
"@shikijs/core": "^1.24.0",
"@shikijs/engine-oniguruma": "^1.24.0",
"@shikijs/vscode-textmate": "^9.3.0",
"shiki": "^1.24.0",
"tm-grammars": "^1.21.0"
"@shikijs/core": "^1.24.2",
"@shikijs/engine-oniguruma": "^1.24.2",
"@shikijs/vscode-textmate": "^9.3.1",
"shiki": "^1.24.2",
"tm-grammars": "^1.21.7"
},
"devDependencies": {
"@types/node": "^22.10.1"
"@types/node": "^22.10.2"
}
}
17 changes: 11 additions & 6 deletions packages/neuri/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"import": "./dist/index.mjs"
},
"./openai": {
"types": "./dist/openai.d.ts",
"import": "./dist/openai.mjs"
"types": "./dist/openai/index.d.ts",
"import": "./dist/openai/index.mjs"
},
"./test": {
"types": "./dist/test.d.ts",
Expand All @@ -54,18 +54,23 @@
"@guiiai/logg": "^1.0.6",
"@typeschema/main": "^0.14.1",
"@xsai/generate-text": "^0.0.22",
"@xsai/providers": "^0.0.22",
"@xsai/shared-chat": "^0.0.22",
"@xsai/stream-text": "^0.0.22",
"@xsai/tool": "^0.0.22",
"defu": "^6.1.4",
"nanoid": "^5.0.9",
"openai": "^4.74.0"
"openai": "^4.77.0"
},
"devDependencies": {
"@gcornut/valibot-json-schema": "^0.42.0",
"@types/mdast": "^4.0.4",
"@types/node": "^22.10.1",
"@types/node": "^22.10.2",
"@typeschema/zod": "^0.14.0",
"json-schema": "^0.4.0",
"remark": "^15.0.1",
"zod": "^3.23.8",
"zod-to-json-schema": "^3.23.5"
"valibot": "^1.0.0-beta.9",
"zod": "^3.24.1",
"zod-to-json-schema": "^3.24.1"
}
}
9 changes: 4 additions & 5 deletions packages/neuri/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import OpenAI from 'openai'
import { describe, expect, it } from 'vitest'

import { object, string } from 'zod'
Expand All @@ -22,10 +21,10 @@ describe('neuri', async () => {
.build(),
)
.build({
openAI: new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: process.env.OPENAI_API_BASEURL,
}),
provider: {
apiKey: process.env.OPENAI_API_KEY!,
baseURL: process.env.OPENAI_API_BASEURL!,
},
})

const name = await n.handle(messages(
Expand Down
37 changes: 19 additions & 18 deletions packages/neuri/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import type { Infer, Schema } from '@typeschema/main'
import type OpenAI from 'openai'
import type { CommonProviderOptions } from '@xsai/providers'
import type { Message } from '@xsai/shared-chat'

import type { ChatCompletion, InvokeContext, Tool, ToolHooks } from './openai'
import type { ChatCompletion, DefinedTool, DefinedToolHooks, InvokeContext } from './openai'
import { composeAgent, defineToolFunction, toolFunction } from './openai'

export interface CallOptions {
model: string
}

export interface NeuriContext {
message: OpenAI.ChatCompletionMessageParam | OpenAI.ChatCompletionMessageParam[]
messages: OpenAI.ChatCompletionMessageParam[]
reroute: (name: string, messages: OpenAI.ChatCompletionMessageParam[], options: CallOptions) => Promise<ChatCompletion | undefined>
message: Message | Message[]
messages: Message[]
reroute: (name: string, messages: Message[], options: CallOptions) => Promise<ChatCompletion | undefined>
}

interface NeuriContextOptions {
openAI: OpenAI
message: OpenAI.ChatCompletionMessageParam | OpenAI.ChatCompletionMessageParam[]
messages?: OpenAI.ChatCompletionMessageParam[]
provider: CommonProviderOptions
message: Message | Message[]
messages?: Message[]
agents?: Agent[]
}

Expand All @@ -38,29 +39,29 @@ function newContext(options: NeuriContextOptions): NeuriContext {
throw new Error(`Agent "${name}" not found`)
}

const { call } = composeAgent({ openAI: options.openAI, tools: agent.tools })
const { call } = composeAgent({ provider: options.provider, tools: agent.tools })
return await call(messages, { model: rerouteOpts.model })
},
}
}

export interface Neuri {
handle: <R>(message: OpenAI.ChatCompletionMessageParam | OpenAI.ChatCompletionMessageParam[], handler: (ctx: NeuriContext) => Promise<R>) => Promise<R>
handle: <R>(message: Message | Message[], handler: (ctx: NeuriContext) => Promise<R>) => Promise<R>
}

interface NeuriInternal extends Neuri {
agents: Agent[]
messages: OpenAI.ChatCompletionMessageParam[]
messages: Message[]
}

export interface Agent {
name: string
tools: Tool<any, any>[]
tools: DefinedTool<any, any>[]
}

export interface NeuriBuilder {
agent: (agent: Agent | Promise<Agent>) => NeuriBuilder
build: (options: { openAI: OpenAI }) => Promise<Neuri>
build: (options: { provider: CommonProviderOptions }) => Promise<Neuri>
}

export interface NeuriBuilderInternal extends Partial<NeuriBuilder> {
Expand All @@ -69,7 +70,7 @@ export interface NeuriBuilderInternal extends Partial<NeuriBuilder> {
}

export type ToolFunc<P, R> = (ctx: InvokeContext<P, R>) => R
export interface ToolOption<P, R> { openAI?: OpenAI, hooks?: Partial<ToolHooks<P, R>>, description?: string }
export interface ToolOption<P, R> { provider?: CommonProviderOptions, hooks?: Partial<DefinedToolHooks<P, R>>, description?: string }

export interface AgentBuilder {
tool: <S extends Schema, R>(name: string, parameters: S, handle: ToolFunc<Infer<S>, R>, options?: ToolOption<Infer<S>, R>) => AgentBuilder
Expand All @@ -78,8 +79,8 @@ export interface AgentBuilder {

interface AgentBuilderInternal extends Partial<AgentBuilder> {
name: string
tools: Tool<any, any>[]
promiseTools: Promise<Tool<any, any>>[]
tools: DefinedTool<any, any>[]
promiseTools: Promise<DefinedTool<any, any>>[]
}

function newNeuriBuilderAgent(cb: () => NeuriBuilderInternal): (agent: Agent | Promise<Agent>) => NeuriBuilder {
Expand All @@ -97,7 +98,7 @@ function newNeuriBuilderAgent(cb: () => NeuriBuilderInternal): (agent: Agent | P
}
}

function newNeuriBuilderBuild(cb: () => NeuriBuilderInternal): (options: { openAI: OpenAI }) => Promise<Neuri> {
function newNeuriBuilderBuild(cb: () => NeuriBuilderInternal): (options: { provider: CommonProviderOptions }) => Promise<Neuri> {
return async (options): Promise<Neuri> => {
const neuriBuilder = cb()

Expand All @@ -122,7 +123,7 @@ function newNeuriBuilderBuild(cb: () => NeuriBuilderInternal): (options: { openA
message,
messages: neuriInternal.messages,
agents: neuriInternal.agents,
openAI: options.openAI,
provider: options.provider,
}))
},
}
Expand Down
Loading

0 comments on commit fc4201a

Please sign in to comment.