Skip to content

Commit

Permalink
Merge pull request #301 from TBXark/dev
Browse files Browse the repository at this point in the history
perf: support cohere v2 api
  • Loading branch information
TBXark authored Oct 31, 2024
2 parents a9b84c2 + 62b8147 commit 74ab291
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 99 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json

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

54 changes: 11 additions & 43 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/timestamp

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"start:debug": "wrangler dev --local",
"prepare:vercel": "tsx ./scripts/plugins/vercel/setenv.ts",
"wrangler": "wrangler",
"test": "tsx ./src/config/env.test.ts"
"test": "tsx ./src/agent/index.test.ts"
},
"dependencies": {
"cloudflare-worker-adapter": "^1.3.3"
Expand Down
57 changes: 7 additions & 50 deletions src/agent/cohere.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import type { AgentUserConfig } from '../config/env';
import type { SseChatCompatibleOptions } from './request';
import type { SSEMessage, SSEParserResult } from './stream';
import type { ChatAgent, ChatStreamTextHandler, HistoryItem, LLMChatParams } from './types';
import type { ChatAgent, ChatStreamTextHandler, LLMChatParams } from './types';
import { requestChatCompletions } from './request';
import { Stream } from './stream';

export class Cohere implements ChatAgent {
readonly name = 'cohere';
readonly modelKey = 'COHERE_CHAT_MODEL';

static COHERE_ROLE_MAP: Record<string, string> = {
assistant: 'CHATBOT',
user: 'USER',
};

readonly enable = (context: AgentUserConfig): boolean => {
return !!(context.COHERE_API_KEY);
};
Expand All @@ -22,37 +15,6 @@ export class Cohere implements ChatAgent {
return ctx.COHERE_CHAT_MODEL;
};

private render = (item: HistoryItem): any => {
return {
role: Cohere.COHERE_ROLE_MAP[item.role] || 'USER',
content: item.content,
};
};

static parser(sse: SSEMessage): SSEParserResult {
// example:
// event: text-generation
// data: {"is_finished":false,"event_type":"text-generation","text":"?"}
//
// event: stream-end
// data: {"is_finished":true,...}
switch (sse.event) {
case 'text-generation':
try {
return { data: JSON.parse(sse.data || '') };
} catch (e) {
console.error(e, sse.data);
return {};
}
case 'stream-start':
return {};
case 'stream-end':
return { finish: true };
default:
return {};
}
}

readonly request = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise<string> => {
const { message, prompt, history } = params;
const url = `${context.COHERE_API_BASE}/chat`;
Expand All @@ -63,27 +25,22 @@ export class Cohere implements ChatAgent {
};

const messages = [...history || [], { role: 'user', content: message }];
if (prompt) {
messages.unshift({ role: 'assistant', content: prompt });
}

const body = {
message,
messages,
model: context.COHERE_CHAT_MODEL,
stream: onStream != null,
preamble: prompt,
chat_history: messages.map(this.render),
};
if (!body.preamble) {
delete body.preamble;
}

const options: SseChatCompatibleOptions = {};
options.streamBuilder = function (r, c) {
return new Stream(r, c, Cohere.parser);
};
options.contentExtractor = function (data: any) {
return data?.text;
return data?.delta?.message?.content?.text;
};
options.fullContentExtractor = function (data: any) {
return data?.text;
return data?.messages[0].content;
};
options.errorExtractor = function (data: any) {
return data?.message;
Expand Down
2 changes: 1 addition & 1 deletion src/agent/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '../config/env.test';
{
const agent = loadChatLLM({
...ENV.USER_CONFIG,
AI_PROVIDER: 'anthropic',
AI_PROVIDER: 'cohere',
});
const params: LLMChatParams = {
prompt: 'You are a useful assistant.',
Expand Down
2 changes: 1 addition & 1 deletion src/agent/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function defaultSSEJsonParser(sse: SSEMessage): SSEParserResult {
if (sse.data?.startsWith('[DONE]')) {
return { finish: true };
}
if (sse.event === null && sse.data) {
if (sse.data) {
try {
return { data: JSON.parse(sse.data) };
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class CohereConfig {
// cohere api key
COHERE_API_KEY: string | null = null;
// cohere api base
COHERE_API_BASE = 'https://api.cohere.com/v1';
COHERE_API_BASE = 'https://api.cohere.com/v2';
// cohere api model
COHERE_CHAT_MODEL = 'command-r-plus';
}
Expand Down

0 comments on commit 74ab291

Please sign in to comment.