-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
75 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { StreamingTextResponse } from "ai"; | ||
import { AbstractLanguageModel } from "../abstractAI"; | ||
import { ModelBrandProvider } from "../types"; | ||
import { AgentRuntimeError, DEBUG_CHAT_COMPLETION, LanguageModelAgentRuntimeErrorType, debugStream } from "../utils"; | ||
import type { OpenAIChatMessage, OpenAIChatStreamPayload } from "../../types"; | ||
|
||
export class CelerisMockLanguageModel extends AbstractLanguageModel<any> { | ||
constructor() { | ||
super(ModelBrandProvider.Celeris); | ||
} | ||
|
||
async streamChatCompletions(messages: OpenAIChatMessage[]) { | ||
// 模拟将响应转换为流 | ||
const stream = new ReadableStream({ | ||
start(controller) { | ||
messages.map(message => | ||
// 模拟处理消息 | ||
controller.enqueue(`Mock completion for ${message.content}`)); | ||
controller.close(); | ||
}, | ||
}); | ||
|
||
return stream; | ||
} | ||
|
||
async chat(payload: OpenAIChatStreamPayload) { | ||
// ============ 1. preprocess messages ============ // | ||
const { messages } = payload; | ||
|
||
// ============ 2. send api ============ // | ||
|
||
try { | ||
const response = await this.streamChatCompletions( | ||
messages, | ||
); | ||
|
||
const [debug, prod] = response.tee(); | ||
|
||
if (DEBUG_CHAT_COMPLETION) { | ||
debugStream(debug).catch(console.error); | ||
} | ||
|
||
return new StreamingTextResponse(prod); | ||
} catch (e) { | ||
const error = e as { [key: string]: any; code: string; message: string }; | ||
|
||
const errorType = error.code | ||
? LanguageModelAgentRuntimeErrorType.MockAIBusinessError | ||
: LanguageModelAgentRuntimeErrorType.AgentRuntimeError; | ||
|
||
throw AgentRuntimeError.chat({ | ||
error, | ||
errorType, | ||
provider: this._brandId, | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters