@cloudbase/ai 2.7.3-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +2 -0
- package/.eslintrc.js +15 -0
- package/LICENSE +202 -0
- package/dist/cjs/index.d.ts +40 -0
- package/dist/cjs/index.js +171 -0
- package/dist/cjs/models/HunYuan/index.d.ts +25 -0
- package/dist/cjs/models/HunYuan/index.js +191 -0
- package/dist/cjs/models/HunYuan/type.d.ts +52 -0
- package/dist/cjs/models/HunYuan/type.js +3 -0
- package/dist/cjs/models/HunYuanPreview/index.d.ts +25 -0
- package/dist/cjs/models/HunYuanPreview/index.js +191 -0
- package/dist/cjs/models/HunYuanPreview/type.d.ts +52 -0
- package/dist/cjs/models/HunYuanPreview/type.js +3 -0
- package/dist/cjs/models/HuoShan/index.d.ts +24 -0
- package/dist/cjs/models/HuoShan/index.js +163 -0
- package/dist/cjs/models/HuoShan/type.d.ts +38 -0
- package/dist/cjs/models/HuoShan/type.js +3 -0
- package/dist/cjs/models/LingJi/index.d.ts +24 -0
- package/dist/cjs/models/LingJi/index.js +163 -0
- package/dist/cjs/models/LingJi/type.d.ts +43 -0
- package/dist/cjs/models/LingJi/type.js +3 -0
- package/dist/cjs/models/LingYi/index.d.ts +24 -0
- package/dist/cjs/models/LingYi/index.js +163 -0
- package/dist/cjs/models/LingYi/type.d.ts +49 -0
- package/dist/cjs/models/LingYi/type.js +3 -0
- package/dist/cjs/models/YueZhi/index.d.ts +24 -0
- package/dist/cjs/models/YueZhi/index.js +163 -0
- package/dist/cjs/models/YueZhi/type.d.ts +31 -0
- package/dist/cjs/models/YueZhi/type.js +3 -0
- package/dist/cjs/models/ZhiPu/index.d.ts +24 -0
- package/dist/cjs/models/ZhiPu/index.js +163 -0
- package/dist/cjs/models/ZhiPu/type.d.ts +71 -0
- package/dist/cjs/models/ZhiPu/type.js +3 -0
- package/dist/cjs/models/index.d.ts +17 -0
- package/dist/cjs/models/index.js +27 -0
- package/dist/cjs/type.d.ts +37 -0
- package/dist/cjs/type.js +16 -0
- package/dist/cjs/utils.d.ts +28 -0
- package/dist/cjs/utils.js +152 -0
- package/dist/esm/index.d.ts +40 -0
- package/dist/esm/index.js +141 -0
- package/dist/esm/models/HunYuan/index.d.ts +25 -0
- package/dist/esm/models/HunYuan/index.js +188 -0
- package/dist/esm/models/HunYuan/type.d.ts +52 -0
- package/dist/esm/models/HunYuan/type.js +2 -0
- package/dist/esm/models/HunYuanPreview/index.d.ts +25 -0
- package/dist/esm/models/HunYuanPreview/index.js +188 -0
- package/dist/esm/models/HunYuanPreview/type.d.ts +52 -0
- package/dist/esm/models/HunYuanPreview/type.js +2 -0
- package/dist/esm/models/HuoShan/index.d.ts +24 -0
- package/dist/esm/models/HuoShan/index.js +160 -0
- package/dist/esm/models/HuoShan/type.d.ts +38 -0
- package/dist/esm/models/HuoShan/type.js +2 -0
- package/dist/esm/models/LingJi/index.d.ts +24 -0
- package/dist/esm/models/LingJi/index.js +160 -0
- package/dist/esm/models/LingJi/type.d.ts +43 -0
- package/dist/esm/models/LingJi/type.js +2 -0
- package/dist/esm/models/LingYi/index.d.ts +24 -0
- package/dist/esm/models/LingYi/index.js +160 -0
- package/dist/esm/models/LingYi/type.d.ts +49 -0
- package/dist/esm/models/LingYi/type.js +2 -0
- package/dist/esm/models/YueZhi/index.d.ts +24 -0
- package/dist/esm/models/YueZhi/index.js +160 -0
- package/dist/esm/models/YueZhi/type.d.ts +31 -0
- package/dist/esm/models/YueZhi/type.js +2 -0
- package/dist/esm/models/ZhiPu/index.d.ts +24 -0
- package/dist/esm/models/ZhiPu/index.js +160 -0
- package/dist/esm/models/ZhiPu/type.d.ts +71 -0
- package/dist/esm/models/ZhiPu/type.js +2 -0
- package/dist/esm/models/index.d.ts +17 -0
- package/dist/esm/models/index.js +18 -0
- package/dist/esm/type.d.ts +37 -0
- package/dist/esm/type.js +13 -0
- package/dist/esm/utils.d.ts +28 -0
- package/dist/esm/utils.js +147 -0
- package/package.json +36 -0
- package/src/index.ts +77 -0
- package/src/models/HunYuan/index.ts +110 -0
- package/src/models/HunYuan/type.ts +61 -0
- package/src/models/HunYuanPreview/index.ts +110 -0
- package/src/models/HunYuanPreview/type.ts +61 -0
- package/src/models/HuoShan/index.ts +92 -0
- package/src/models/HuoShan/type.ts +41 -0
- package/src/models/LingJi/index.ts +92 -0
- package/src/models/LingJi/type.ts +34 -0
- package/src/models/LingYi/index.ts +92 -0
- package/src/models/LingYi/type.ts +49 -0
- package/src/models/YueZhi/index.ts +92 -0
- package/src/models/YueZhi/type.ts +28 -0
- package/src/models/ZhiPu/index.ts +92 -0
- package/src/models/ZhiPu/type.ts +60 -0
- package/src/models/index.ts +19 -0
- package/src/type.ts +42 -0
- package/src/utils.ts +110 -0
- package/tsconfig.esm.json +16 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type ParsedEvent } from 'eventsource-parser/stream'
|
|
2
|
+
import {
|
|
3
|
+
createAsyncIterable,
|
|
4
|
+
TransformStream,
|
|
5
|
+
TextDecoderStream,
|
|
6
|
+
createEventSourceParserTransformStream,
|
|
7
|
+
toPolyfillReadable,
|
|
8
|
+
} from '../../utils'
|
|
9
|
+
import type { HuoShanInputData, HuoShanGenerateTextOutput, HuoShanStreamTextOutput } from './type'
|
|
10
|
+
import type { ChatModel, StreamTextResult, ModelReq } from '../../type'
|
|
11
|
+
|
|
12
|
+
export class HuoShanModel implements ChatModel {
|
|
13
|
+
public subUrl = 'toutiao/api/v3/chat/completions'
|
|
14
|
+
constructor(private req: ModelReq, public baseUrl: string, subUrl?: string) {
|
|
15
|
+
if (subUrl != null) {
|
|
16
|
+
this.subUrl = subUrl
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private get url() {
|
|
21
|
+
return `${this.baseUrl}/${this.subUrl}`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async generateText(data: HuoShanInputData) {
|
|
25
|
+
const res = (await this.req({
|
|
26
|
+
url: this.url,
|
|
27
|
+
data: {
|
|
28
|
+
...data,
|
|
29
|
+
stream: false,
|
|
30
|
+
},
|
|
31
|
+
stream: false,
|
|
32
|
+
})) as HuoShanGenerateTextOutput
|
|
33
|
+
return {
|
|
34
|
+
text: (res?.choices?.[0]?.message?.content as string) ?? '',
|
|
35
|
+
rawResponse: res,
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async streamText(data: HuoShanInputData) {
|
|
40
|
+
const stream = await this.req({
|
|
41
|
+
url: this.url,
|
|
42
|
+
data: {
|
|
43
|
+
...data,
|
|
44
|
+
stream: true,
|
|
45
|
+
},
|
|
46
|
+
stream: true,
|
|
47
|
+
})
|
|
48
|
+
return new HuoShanModelStreamResult(stream)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
class HuoShanModelStreamResult implements StreamTextResult {
|
|
53
|
+
private _eventSourceStream: ReadableStream<ParsedEvent>
|
|
54
|
+
|
|
55
|
+
constructor(_stream: ReadableStream<Uint8Array>) {
|
|
56
|
+
const stream = toPolyfillReadable(_stream) as typeof _stream
|
|
57
|
+
this._eventSourceStream = stream
|
|
58
|
+
.pipeThrough(new TextDecoderStream())
|
|
59
|
+
.pipeThrough(createEventSourceParserTransformStream())
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private get teeedStream() {
|
|
63
|
+
const [s1, s2] = this._eventSourceStream.tee()
|
|
64
|
+
this._eventSourceStream = s2
|
|
65
|
+
return s1
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get eventSourceStream() {
|
|
69
|
+
return createAsyncIterable(this.teeedStream)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get dataStream() {
|
|
73
|
+
return createAsyncIterable(this.eventSourceStream.pipeThrough(new TransformStream<ParsedEvent, HuoShanStreamTextOutput>({
|
|
74
|
+
transform(chunk, controller) {
|
|
75
|
+
try {
|
|
76
|
+
const data = JSON.parse(chunk.data) as HuoShanStreamTextOutput
|
|
77
|
+
controller.enqueue(data)
|
|
78
|
+
} catch (e) {
|
|
79
|
+
console.warn('Error when transforming event source data to json', e)
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
}),),)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get textStream() {
|
|
86
|
+
return createAsyncIterable(this.dataStream.pipeThrough(new TransformStream<HuoShanStreamTextOutput, string>({
|
|
87
|
+
transform(chunk, controller) {
|
|
88
|
+
controller.enqueue(chunk?.choices?.[0]?.message?.content ?? '')
|
|
89
|
+
},
|
|
90
|
+
}),),)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
type HuoShanMessage = {
|
|
2
|
+
role: 'user' | 'system' | 'assistant'
|
|
3
|
+
content:
|
|
4
|
+
| string
|
|
5
|
+
| Array<{
|
|
6
|
+
type: 'image_url'
|
|
7
|
+
image_url: {
|
|
8
|
+
url: string
|
|
9
|
+
image_bytes: string
|
|
10
|
+
detail: string
|
|
11
|
+
}
|
|
12
|
+
}>
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// 文档描述模糊,暂只提供基础定义
|
|
16
|
+
export type HuoShanInputData = {
|
|
17
|
+
model: string
|
|
18
|
+
messages: Array<HuoShanMessage>
|
|
19
|
+
max_tokens?: number
|
|
20
|
+
stop?: string | Array<string>
|
|
21
|
+
frequency_penalty?: number
|
|
22
|
+
temperature?: number
|
|
23
|
+
top_p?: number
|
|
24
|
+
logprobs?: boolean
|
|
25
|
+
top_logprobs?: number
|
|
26
|
+
logit_bias?: Record<string, number>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// todo: more precise
|
|
30
|
+
export type HuoShanGenerateTextOutput = {
|
|
31
|
+
choices?: Array<{
|
|
32
|
+
message?: { content?: string }
|
|
33
|
+
}>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// todo: more precise
|
|
37
|
+
export type HuoShanStreamTextOutput = {
|
|
38
|
+
choices?: Array<{
|
|
39
|
+
message?: { content?: string }
|
|
40
|
+
}>
|
|
41
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type ParsedEvent } from 'eventsource-parser/stream'
|
|
2
|
+
import {
|
|
3
|
+
createAsyncIterable,
|
|
4
|
+
TransformStream,
|
|
5
|
+
TextDecoderStream,
|
|
6
|
+
createEventSourceParserTransformStream,
|
|
7
|
+
toPolyfillReadable,
|
|
8
|
+
} from '../../utils'
|
|
9
|
+
import type { LingJiInputData, LingJiGenerateTextOutput, LingJiStreamTextOutput } from './type'
|
|
10
|
+
import type { ChatModel, StreamTextResult, ModelReq } from '../../type'
|
|
11
|
+
|
|
12
|
+
export class LingJiModel implements ChatModel {
|
|
13
|
+
public subUrl = 'ali/compatible-mode/v1/chat/completions'
|
|
14
|
+
constructor(private req: ModelReq, public baseUrl: string, subUrl?: string) {
|
|
15
|
+
if (subUrl != null) {
|
|
16
|
+
this.subUrl = subUrl
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private get url() {
|
|
21
|
+
return `${this.baseUrl}/${this.subUrl}`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async generateText(data: LingJiInputData) {
|
|
25
|
+
const res = (await this.req({
|
|
26
|
+
url: this.url,
|
|
27
|
+
data: {
|
|
28
|
+
...data,
|
|
29
|
+
stream: false,
|
|
30
|
+
},
|
|
31
|
+
stream: false,
|
|
32
|
+
})) as LingJiGenerateTextOutput
|
|
33
|
+
return {
|
|
34
|
+
text: (res?.choices?.[0]?.message?.content as string) ?? '',
|
|
35
|
+
rawResponse: res,
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async streamText(data: LingJiInputData) {
|
|
40
|
+
const stream = await this.req({
|
|
41
|
+
url: this.url,
|
|
42
|
+
data: {
|
|
43
|
+
...data,
|
|
44
|
+
stream: true,
|
|
45
|
+
},
|
|
46
|
+
stream: true,
|
|
47
|
+
})
|
|
48
|
+
return new LingJiModelStreamResult(stream)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
class LingJiModelStreamResult implements StreamTextResult {
|
|
53
|
+
private _eventSourceStream: ReadableStream<ParsedEvent>
|
|
54
|
+
|
|
55
|
+
constructor(_stream: ReadableStream<Uint8Array>) {
|
|
56
|
+
const stream = toPolyfillReadable(_stream) as typeof _stream
|
|
57
|
+
this._eventSourceStream = stream
|
|
58
|
+
.pipeThrough(new TextDecoderStream())
|
|
59
|
+
.pipeThrough(createEventSourceParserTransformStream())
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private get teeedStream() {
|
|
63
|
+
const [s1, s2] = this._eventSourceStream.tee()
|
|
64
|
+
this._eventSourceStream = s2
|
|
65
|
+
return s1
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get eventSourceStream() {
|
|
69
|
+
return createAsyncIterable(this.teeedStream)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get dataStream() {
|
|
73
|
+
return createAsyncIterable(this.eventSourceStream.pipeThrough(new TransformStream<ParsedEvent, LingJiStreamTextOutput>({
|
|
74
|
+
transform(chunk, controller) {
|
|
75
|
+
try {
|
|
76
|
+
const data = JSON.parse(chunk.data) as LingJiStreamTextOutput
|
|
77
|
+
controller.enqueue(data)
|
|
78
|
+
} catch (e) {
|
|
79
|
+
console.warn('Error when transforming event source data to json', e)
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
}),),)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get textStream() {
|
|
86
|
+
return createAsyncIterable(this.dataStream.pipeThrough(new TransformStream<LingJiStreamTextOutput, string>({
|
|
87
|
+
transform(chunk, controller) {
|
|
88
|
+
controller.enqueue(chunk?.choices?.[0]?.delta?.content ?? '')
|
|
89
|
+
},
|
|
90
|
+
}),),)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
type LingJiMessage = { role: 'user' | 'system' | 'assistant'; content: string }
|
|
2
|
+
|
|
3
|
+
export type LingJiInputData = {
|
|
4
|
+
model: string
|
|
5
|
+
messages: Array<LingJiMessage>
|
|
6
|
+
temperature?: number
|
|
7
|
+
top_p?: number
|
|
8
|
+
presence_penalty?: number
|
|
9
|
+
max_tokens?: number
|
|
10
|
+
seed?: number
|
|
11
|
+
stop?: string | Array<string>
|
|
12
|
+
request_id?: string
|
|
13
|
+
do_sample?: boolean
|
|
14
|
+
tools?: Array<{
|
|
15
|
+
type: 'function'
|
|
16
|
+
function: { name: string; description: string; parameters: object }
|
|
17
|
+
}>
|
|
18
|
+
stream_options?: { include_usage: boolean }
|
|
19
|
+
enable_search?: boolean
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// todo: more precise
|
|
23
|
+
export type LingJiGenerateTextOutput = {
|
|
24
|
+
choices?: Array<{
|
|
25
|
+
message?: { content?: string }
|
|
26
|
+
}>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// todo: more precise
|
|
30
|
+
export type LingJiStreamTextOutput = {
|
|
31
|
+
choices?: Array<{
|
|
32
|
+
delta?: { content?: string }
|
|
33
|
+
}>
|
|
34
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type ParsedEvent } from 'eventsource-parser/stream'
|
|
2
|
+
import {
|
|
3
|
+
createAsyncIterable,
|
|
4
|
+
TransformStream,
|
|
5
|
+
TextDecoderStream,
|
|
6
|
+
createEventSourceParserTransformStream,
|
|
7
|
+
toPolyfillReadable,
|
|
8
|
+
} from '../../utils'
|
|
9
|
+
import type { LingYiGenerateTextOutput, LingYiStreamTextOutput } from './type'
|
|
10
|
+
import type { ChatModel, StreamTextResult, ModelReq, BaseChatModelInput } from '../../type'
|
|
11
|
+
|
|
12
|
+
export class LingYiModel implements ChatModel {
|
|
13
|
+
public subUrl = 'lingyiwanwu/v1/chat/completion'
|
|
14
|
+
constructor(private req: ModelReq, public baseUrl: string, subUrl?: string) {
|
|
15
|
+
if (subUrl != null) {
|
|
16
|
+
this.subUrl = subUrl
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private get url() {
|
|
21
|
+
return `${this.baseUrl}/${this.subUrl}`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async generateText(data: BaseChatModelInput) {
|
|
25
|
+
const res = (await this.req({
|
|
26
|
+
url: this.url,
|
|
27
|
+
data: {
|
|
28
|
+
...data,
|
|
29
|
+
stream: false,
|
|
30
|
+
},
|
|
31
|
+
stream: false,
|
|
32
|
+
})) as LingYiGenerateTextOutput
|
|
33
|
+
return {
|
|
34
|
+
text: (res?.choices?.[0]?.message?.content as string) ?? '',
|
|
35
|
+
rawResponse: res,
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async streamText(data: BaseChatModelInput) {
|
|
40
|
+
const stream = await this.req({
|
|
41
|
+
url: this.url,
|
|
42
|
+
data: {
|
|
43
|
+
...data,
|
|
44
|
+
stream: true,
|
|
45
|
+
},
|
|
46
|
+
stream: true,
|
|
47
|
+
})
|
|
48
|
+
return new LingYiModelStreamResult(stream)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
class LingYiModelStreamResult implements StreamTextResult {
|
|
53
|
+
private _eventSourceStream: ReadableStream<ParsedEvent>
|
|
54
|
+
|
|
55
|
+
constructor(_stream: ReadableStream<Uint8Array>) {
|
|
56
|
+
const stream = toPolyfillReadable(_stream) as typeof _stream
|
|
57
|
+
this._eventSourceStream = stream
|
|
58
|
+
.pipeThrough(new TextDecoderStream())
|
|
59
|
+
.pipeThrough(createEventSourceParserTransformStream())
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private get teeedStream() {
|
|
63
|
+
const [s1, s2] = this._eventSourceStream.tee()
|
|
64
|
+
this._eventSourceStream = s2
|
|
65
|
+
return s1
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get eventSourceStream() {
|
|
69
|
+
return createAsyncIterable(this.teeedStream)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get dataStream() {
|
|
73
|
+
return createAsyncIterable(this.eventSourceStream.pipeThrough(new TransformStream<ParsedEvent, LingYiStreamTextOutput>({
|
|
74
|
+
transform(chunk, controller) {
|
|
75
|
+
try {
|
|
76
|
+
const data = JSON.parse(chunk.data) as LingYiStreamTextOutput
|
|
77
|
+
controller.enqueue(data)
|
|
78
|
+
} catch (e) {
|
|
79
|
+
console.warn('Error when transforming event source data to json', e)
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
}),),)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get textStream() {
|
|
86
|
+
return createAsyncIterable(this.dataStream.pipeThrough(new TransformStream<LingYiStreamTextOutput, string>({
|
|
87
|
+
transform(chunk, controller) {
|
|
88
|
+
controller.enqueue(chunk?.choices?.[0]?.delta?.content ?? '')
|
|
89
|
+
},
|
|
90
|
+
}),),)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
type SystemMessage = {
|
|
2
|
+
role: 'system'
|
|
3
|
+
content: string
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
type UserMessage = {
|
|
7
|
+
role: 'user'
|
|
8
|
+
content: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type AssistantMessage = {
|
|
12
|
+
role: 'assistant'
|
|
13
|
+
content: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type ToolMessage = {
|
|
17
|
+
role: 'tool'
|
|
18
|
+
content: string
|
|
19
|
+
tool_call_id: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type LingYiMessage = SystemMessage | UserMessage | AssistantMessage | ToolMessage
|
|
23
|
+
|
|
24
|
+
export type LingYiInputData = {
|
|
25
|
+
model: string
|
|
26
|
+
messages: Array<LingYiMessage>
|
|
27
|
+
tools?: Array<{
|
|
28
|
+
type: 'function'
|
|
29
|
+
function: { name: string; description?: string; parameters: object }
|
|
30
|
+
}>
|
|
31
|
+
tool_choice?: string | object
|
|
32
|
+
max_tokens?: number | null
|
|
33
|
+
top_p?: number
|
|
34
|
+
temperature?: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// todo: more precise
|
|
38
|
+
export type LingYiGenerateTextOutput = {
|
|
39
|
+
choices?: Array<{
|
|
40
|
+
message?: { content?: string }
|
|
41
|
+
}>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// todo: more precise
|
|
45
|
+
export type LingYiStreamTextOutput = {
|
|
46
|
+
choices?: Array<{
|
|
47
|
+
delta?: { content?: string }
|
|
48
|
+
}>
|
|
49
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type ParsedEvent } from 'eventsource-parser/stream'
|
|
2
|
+
import {
|
|
3
|
+
createAsyncIterable,
|
|
4
|
+
TransformStream,
|
|
5
|
+
TextDecoderStream,
|
|
6
|
+
createEventSourceParserTransformStream,
|
|
7
|
+
toPolyfillReadable,
|
|
8
|
+
} from '../../utils'
|
|
9
|
+
import type { YueZhiInputData, YueZhiGenerateTextOutput, YueZhiStreamTextOutput } from './type'
|
|
10
|
+
import type { ChatModel, StreamTextResult, ModelReq } from '../../type'
|
|
11
|
+
|
|
12
|
+
export class YueZhiModel implements ChatModel {
|
|
13
|
+
public subUrl = 'yuezhianmian/v1/chat/completions'
|
|
14
|
+
constructor(private req: ModelReq, public baseUrl: string, subUrl?: string) {
|
|
15
|
+
if (subUrl != null) {
|
|
16
|
+
this.subUrl = subUrl
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private get url() {
|
|
21
|
+
return `${this.baseUrl}/${this.subUrl}`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async generateText(data: YueZhiInputData) {
|
|
25
|
+
const res = (await this.req({
|
|
26
|
+
url: this.url,
|
|
27
|
+
data: {
|
|
28
|
+
...data,
|
|
29
|
+
stream: false,
|
|
30
|
+
},
|
|
31
|
+
stream: false,
|
|
32
|
+
})) as YueZhiGenerateTextOutput
|
|
33
|
+
return {
|
|
34
|
+
text: (res?.choices?.[0]?.message?.content as string) ?? '',
|
|
35
|
+
rawResponse: res,
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async streamText(data: YueZhiInputData) {
|
|
40
|
+
const stream = await this.req({
|
|
41
|
+
url: this.url,
|
|
42
|
+
data: {
|
|
43
|
+
...data,
|
|
44
|
+
stream: true,
|
|
45
|
+
},
|
|
46
|
+
stream: true,
|
|
47
|
+
})
|
|
48
|
+
return new YueZhiModelStreamResult(stream)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
class YueZhiModelStreamResult implements StreamTextResult {
|
|
53
|
+
private _eventSourceStream: ReadableStream<ParsedEvent>
|
|
54
|
+
|
|
55
|
+
constructor(_stream: ReadableStream<Uint8Array>) {
|
|
56
|
+
const stream = toPolyfillReadable(_stream) as typeof _stream
|
|
57
|
+
this._eventSourceStream = stream
|
|
58
|
+
.pipeThrough(new TextDecoderStream())
|
|
59
|
+
.pipeThrough(createEventSourceParserTransformStream())
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private get teeedStream() {
|
|
63
|
+
const [s1, s2] = this._eventSourceStream.tee()
|
|
64
|
+
this._eventSourceStream = s2
|
|
65
|
+
return s1
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get eventSourceStream() {
|
|
69
|
+
return createAsyncIterable(this.teeedStream)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get dataStream() {
|
|
73
|
+
return createAsyncIterable(this.eventSourceStream.pipeThrough(new TransformStream<ParsedEvent, YueZhiStreamTextOutput>({
|
|
74
|
+
transform(chunk, controller) {
|
|
75
|
+
try {
|
|
76
|
+
const data = JSON.parse(chunk.data) as YueZhiStreamTextOutput
|
|
77
|
+
controller.enqueue(data)
|
|
78
|
+
} catch (e) {
|
|
79
|
+
console.warn('Error when transforming event source data to json', e)
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
}),),)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get textStream() {
|
|
86
|
+
return createAsyncIterable(this.dataStream.pipeThrough(new TransformStream<YueZhiStreamTextOutput, string>({
|
|
87
|
+
transform(chunk, controller) {
|
|
88
|
+
controller.enqueue(chunk?.choices?.[0]?.delta?.content ?? '')
|
|
89
|
+
},
|
|
90
|
+
}),),)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type YueZhiMessage = { role: 'user' | 'system' | 'assistant'; content: string }
|
|
2
|
+
|
|
3
|
+
export type YueZhiInputData = {
|
|
4
|
+
messages: Array<YueZhiMessage>
|
|
5
|
+
model: 'moonshot-v1-8k' | 'moonshot-v1-32k' | 'moonshot-v1-128k'
|
|
6
|
+
max_tokens?: number
|
|
7
|
+
temperature?: number
|
|
8
|
+
top_p?: number
|
|
9
|
+
n?: number
|
|
10
|
+
presence_penalty?: number
|
|
11
|
+
frequency_penalty?: number
|
|
12
|
+
response_format?: object
|
|
13
|
+
stop?: string | Array<string>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// todo: more precise
|
|
17
|
+
export type YueZhiGenerateTextOutput = {
|
|
18
|
+
choices?: Array<{
|
|
19
|
+
message?: { content?: string }
|
|
20
|
+
}>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// todo: more precise
|
|
24
|
+
export type YueZhiStreamTextOutput = {
|
|
25
|
+
choices?: Array<{
|
|
26
|
+
delta?: { content?: string }
|
|
27
|
+
}>
|
|
28
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type ParsedEvent } from 'eventsource-parser/stream'
|
|
2
|
+
import {
|
|
3
|
+
createAsyncIterable,
|
|
4
|
+
TransformStream,
|
|
5
|
+
TextDecoderStream,
|
|
6
|
+
createEventSourceParserTransformStream,
|
|
7
|
+
toPolyfillReadable,
|
|
8
|
+
} from '../../utils'
|
|
9
|
+
import type { ZhiPuGenerateTextOutput, ZhiPuStreamTextOutput } from './type'
|
|
10
|
+
import type { ChatModel, StreamTextResult, ModelReq, BaseChatModelInput } from '../../type'
|
|
11
|
+
|
|
12
|
+
export class ZhiPuModel implements ChatModel {
|
|
13
|
+
public subUrl = 'zhipu/api/paas/v4/chat/completions'
|
|
14
|
+
constructor(private req: ModelReq, public baseUrl: string, subUrl?: string) {
|
|
15
|
+
if (subUrl != null) {
|
|
16
|
+
this.subUrl = subUrl
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private get url() {
|
|
21
|
+
return `${this.baseUrl}/${this.subUrl}`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async generateText(data: BaseChatModelInput) {
|
|
25
|
+
const res = (await this.req({
|
|
26
|
+
url: this.url,
|
|
27
|
+
data: {
|
|
28
|
+
...data,
|
|
29
|
+
stream: false,
|
|
30
|
+
},
|
|
31
|
+
stream: false,
|
|
32
|
+
})) as ZhiPuGenerateTextOutput
|
|
33
|
+
return {
|
|
34
|
+
text: (res?.choices?.[0]?.message?.content as string) ?? '',
|
|
35
|
+
rawResponse: res,
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async streamText(data: BaseChatModelInput) {
|
|
40
|
+
const stream = await this.req({
|
|
41
|
+
url: this.url,
|
|
42
|
+
data: {
|
|
43
|
+
...data,
|
|
44
|
+
stream: true,
|
|
45
|
+
},
|
|
46
|
+
stream: true,
|
|
47
|
+
})
|
|
48
|
+
return new ZhiPuModelStreamResult(stream)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
class ZhiPuModelStreamResult implements StreamTextResult {
|
|
53
|
+
private _eventSourceStream: ReadableStream<ParsedEvent>
|
|
54
|
+
|
|
55
|
+
constructor(_stream: ReadableStream<Uint8Array>) {
|
|
56
|
+
const stream = toPolyfillReadable(_stream) as typeof _stream
|
|
57
|
+
this._eventSourceStream = stream
|
|
58
|
+
.pipeThrough(new TextDecoderStream())
|
|
59
|
+
.pipeThrough(createEventSourceParserTransformStream())
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private get teeedStream() {
|
|
63
|
+
const [s1, s2] = this._eventSourceStream.tee()
|
|
64
|
+
this._eventSourceStream = s2
|
|
65
|
+
return s1
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get eventSourceStream() {
|
|
69
|
+
return createAsyncIterable(this.teeedStream)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get dataStream() {
|
|
73
|
+
return createAsyncIterable(this.eventSourceStream.pipeThrough(new TransformStream<ParsedEvent, ZhiPuStreamTextOutput>({
|
|
74
|
+
transform(chunk, controller) {
|
|
75
|
+
try {
|
|
76
|
+
const data = JSON.parse(chunk.data) as ZhiPuStreamTextOutput
|
|
77
|
+
controller.enqueue(data)
|
|
78
|
+
} catch (e) {
|
|
79
|
+
console.warn('Error when transforming event source data to json', e)
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
}),),)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get textStream() {
|
|
86
|
+
return createAsyncIterable(this.dataStream.pipeThrough(new TransformStream<ZhiPuStreamTextOutput, string>({
|
|
87
|
+
transform(chunk, controller) {
|
|
88
|
+
controller.enqueue(chunk?.choices?.[0]?.delta?.content ?? '')
|
|
89
|
+
},
|
|
90
|
+
}),),)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
type SystemMessage = {
|
|
2
|
+
role: 'system'
|
|
3
|
+
content: string
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
type UserMessage = {
|
|
7
|
+
role: 'user'
|
|
8
|
+
content: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type AssistantMessage = {
|
|
12
|
+
role: 'assistant'
|
|
13
|
+
content?: string
|
|
14
|
+
tool_calls?: Array<{
|
|
15
|
+
id: string
|
|
16
|
+
type: 'web_search' | 'retrieval' | 'function'
|
|
17
|
+
function?: { name: string; auguments: string }
|
|
18
|
+
}>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type ToolMessage = {
|
|
22
|
+
role: 'tool'
|
|
23
|
+
content: string
|
|
24
|
+
tool_call_id: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type ZhiPuMessage = SystemMessage | UserMessage | AssistantMessage | ToolMessage
|
|
28
|
+
|
|
29
|
+
export type ZhiPuInputData = {
|
|
30
|
+
model: string
|
|
31
|
+
messages: Array<ZhiPuMessage>
|
|
32
|
+
request_id?: string
|
|
33
|
+
do_sample?: boolean
|
|
34
|
+
temperature?: number
|
|
35
|
+
top_p?: number
|
|
36
|
+
max_tokens?: number
|
|
37
|
+
stop?: Array<string>
|
|
38
|
+
tools?: Array<{
|
|
39
|
+
type: 'web_search' | 'retrieval' | 'function'
|
|
40
|
+
function: { name: string; description: string; parameters: object }
|
|
41
|
+
retrieval: { knowledge_id: string; prompt_template?: string; parameters: object }
|
|
42
|
+
web_search: { enable?: boolean; search_query?: string; search_result?: boolean }
|
|
43
|
+
}>
|
|
44
|
+
tool_choice?: 'auto'
|
|
45
|
+
user_id?: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// todo: more precise
|
|
49
|
+
export type ZhiPuGenerateTextOutput = {
|
|
50
|
+
choices?: Array<{
|
|
51
|
+
message?: { content?: string }
|
|
52
|
+
}>
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// todo: more precise
|
|
56
|
+
export type ZhiPuStreamTextOutput = {
|
|
57
|
+
choices?: Array<{
|
|
58
|
+
delta?: { content?: string }
|
|
59
|
+
}>
|
|
60
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { HunYuanModel } from './HunYuan'
|
|
2
|
+
import { HuoShanModel } from './HuoShan'
|
|
3
|
+
import { LingJiModel } from './LingJi'
|
|
4
|
+
import { LingYiModel } from './LingYi'
|
|
5
|
+
import { YueZhiModel } from './YueZhi'
|
|
6
|
+
import { ZhiPuModel } from './ZhiPu'
|
|
7
|
+
import { HunYuanPreviewModel } from './HunYuanPreview'
|
|
8
|
+
|
|
9
|
+
export const MODELS = {
|
|
10
|
+
HunYuan: HunYuanModel,
|
|
11
|
+
HunYuanPreview: HunYuanPreviewModel,
|
|
12
|
+
HuoShan: HuoShanModel,
|
|
13
|
+
LingJi: LingJiModel,
|
|
14
|
+
LingYi: LingYiModel,
|
|
15
|
+
YueZhi: YueZhiModel,
|
|
16
|
+
ZhiPu: ZhiPuModel,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { HunYuanModel, HunYuanPreviewModel, HuoShanModel, LingJiModel, LingYiModel, YueZhiModel, ZhiPuModel }
|