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!: rename & move #28

Merged
merged 3 commits into from
Jan 15, 2025
Merged
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
13 changes: 4 additions & 9 deletions packages/generate-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type FinishReason,
type Message,
type Tool,
type Usage,
} from '@xsai/shared-chat'

export interface GenerateTextOptions extends ChatOptions {
Expand All @@ -28,13 +29,7 @@ export interface GenerateTextResponse {
model: string
object: 'chat.completion'
system_fingerprint: string
usage: GenerateTextResponseUsage
}

export interface GenerateTextResponseUsage {
completion_tokens: number
prompt_tokens: number
total_tokens: number
usage: Usage
}

export interface ToolCall {
Expand All @@ -57,7 +52,7 @@ export interface GenerateTextResult {
text?: string
toolCalls: ToolCall[]
toolResults: ToolResult[]
usage: GenerateTextResponseUsage
usage: Usage
}

export interface StepResult {
Expand All @@ -66,7 +61,7 @@ export interface StepResult {
// type: 'continue' | 'initial' | 'tool-result'
toolCalls: ToolCall[]
toolResults: ToolResult[]
usage: GenerateTextResponseUsage
usage: Usage
}

/** @internal */
Expand Down
1 change: 1 addition & 0 deletions packages/shared-chat/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export type * from './message'
export type * from './message-part'
export type * from './tool'
export type * from './tool-choice'
export type * from './usage'
5 changes: 5 additions & 0 deletions packages/shared-chat/src/types/usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Usage {
completion_tokens: number
prompt_tokens: number
total_tokens: number
}
21 changes: 8 additions & 13 deletions packages/stream-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import {
chat,
type ChatOptions,
type FinishReason,
type Usage,
} from '@xsai/shared-chat'

export interface StreamTextOptions extends ChatOptions {
onChunk?: (chunk: StreamTextResponse) => Promise<void> | void
onChunk?: (chunk: ChunkResult) => Promise<void> | void
/** if you want to disable stream, use `@xsai/generate-{text,object}` */
stream?: never
streamOptions?: {
Expand All @@ -18,21 +19,15 @@ export interface StreamTextOptions extends ChatOptions {
}
}

export interface StreamTextResponseUsage {
completion_tokens: number
prompt_tokens: number
total_tokens: number
}

export interface StreamTextResult {
chunkStream: ReadableStream<StreamTextResponse>
chunkStream: ReadableStream<ChunkResult>
finishReason?: FinishReason
textStream: ReadableStream<string>
usage?: StreamTextResponseUsage
usage?: Usage
}

// TODO: improve chunk type
export interface StreamTextResponse {
export interface ChunkResult {
choices: {
delta: {
content: string
Expand All @@ -46,7 +41,7 @@ export interface StreamTextResponse {
model: string
object: 'chat.completion.chunk'
system_fingerprint: string
usage?: StreamTextResponseUsage
usage?: Usage
}

const chunkHeaderPrefix = 'data: '
Expand All @@ -62,7 +57,7 @@ export const streamText = async (options: StreamTextOptions): Promise<StreamText
const decoder = new TextDecoder()

let finishReason: string | undefined
let usage: StreamTextResponseUsage | undefined
let usage: undefined | Usage
let buffer = ''

// null body handled by import('@xsai/shared-chat').chat()
Expand Down Expand Up @@ -92,7 +87,7 @@ export const streamText = async (options: StreamTextOptions): Promise<StreamText
break
}

const chunk: StreamTextResponse = JSON.parse(lineWithoutPrefix)
const chunk: ChunkResult = JSON.parse(lineWithoutPrefix)
controller.enqueue(chunk)

if (options.onChunk)
Expand Down
4 changes: 2 additions & 2 deletions packages/xsai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export * as providers from './providers'
export { embed, embedMany, type EmbedManyOptions, type EmbedManyResult, type EmbedOptions, type EmbedResponseUsage, type EmbedResult } from '@xsai/embed'
export { generateObject, type GenerateObjectOptions, type GenerateObjectResult } from '@xsai/generate-object'
export { generateSpeech, type GenerateSpeechOptions } from '@xsai/generate-speech'
export { generateText, type GenerateTextOptions, type GenerateTextResponseUsage, type GenerateTextResult } from '@xsai/generate-text'
export { generateText, type GenerateTextOptions, type GenerateTextResponse, type GenerateTextResult, type StepResult } from '@xsai/generate-text'
export { generateTranscription, type GenerateTranscriptionOptions, type GenerateTranscriptionResult } from '@xsai/generate-transcription'
export { listModels, type ListModelsOptions, type ListModelsResponse, type Model, retrieveModel, type RetrieveModelOptions } from '@xsai/model'
// shared chat utils
export * from '@xsai/shared-chat'
export { streamObject, type StreamObjectOptions, type StreamObjectResult } from '@xsai/stream-object'
export { streamText, type StreamTextOptions, type StreamTextResponse, type StreamTextResponseUsage, type StreamTextResult } from '@xsai/stream-text'
export { type ChunkResult, streamText, type StreamTextOptions, type StreamTextResult } from '@xsai/stream-text'
export { tool, type ToolOptions, type ToolResult } from '@xsai/tool'
Loading