Skip to content

Commit

Permalink
feat!: add model alias
Browse files Browse the repository at this point in the history
BREAKING CHANGE: export type changed from `Model` to `ModelAlias`. The
parameter passed to `initChat` is changed to `ModelAlias`.
  • Loading branch information
mumu-lhl committed Aug 9, 2024
1 parent 85a0c84 commit 0c6acc9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ deno add @mumulhl/duckduckgo-ai-chat
```javascript
import { initChat } from "@mumulhl/duckduckgo-ai-chat";

// Initialize, optional models are gpt-4o-mini, claude-3-haiku-20240307, meta-llama/Llama-3-70b-chat-hf, mistralai/Mixtral-8x7B-Instruct-v0.1
// Initialize, optional models are gpt-4o-mini, claude-3-haiku, llama, mixtral
const chat = await initChat("gpt-4o-mini");

// Fetch the full reply in one go
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ deno add @mumulhl/duckduckgo-ai-chat
```javascript
import { initChat } from "@mumulhl/duckduckgo-ai-chat";

// 初始化,可选模型有 gpt-4o-mini, claude-3-haiku-20240307, meta-llama/Llama-3-70b-chat-hf, mistralai/Mixtral-8x7B-Instruct-v0.1
// 初始化,可选模型有 gpt-4o-mini, claude-3-haiku, llama, mixtral
const chat = await initChat("gpt-4o-mini");

// 一次性获取完整的回复
Expand Down
21 changes: 17 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,29 @@ const STATUS_HEADERS = { "x-vqd-accept": "1" };
type Model =
| "gpt-4o-mini"
| "claude-3-haiku-20240307"
| "meta-llama/Llama-3-70b-chat-hf"
| "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo"
| "mistralai/Mixtral-8x7B-Instruct-v0.1";

type ModelAlias =
| "gpt-4o-mini"
| "claude-3-haiku"
| "llama"
| "mixtral";

type Messages = { content: string; role: "user" | "assistant" }[];

type ChatPayload = {
model: Model;
messages: Messages;
};

const _model: { [property: string]: Model } = {
"gpt-4o-mini": "gpt-4o-mini",
"claude-3-haiku": "claude-3-haiku-20240307",
"llama": "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
"mixtral": "mistralai/Mixtral-8x7B-Instruct-v0.1",
};

class Chat {
oldVqd: string;
newVqd: string;
Expand Down Expand Up @@ -132,16 +145,16 @@ class Chat {
* @param model The model used by chat.
* @returns A Chat instance.
*/
async function initChat(model: Model): Promise<Chat> {
async function initChat(model: ModelAlias): Promise<Chat> {
const status = await fetch(STATUS_URL, { headers: STATUS_HEADERS });
const vqd = status.headers.get("x-vqd-4");
if (!vqd) {
throw new Error(
`${status.status}: Failed to initialize chat. ${status.statusText}`,
);
}
return new Chat(vqd, model);
return new Chat(vqd, _model[model]);
}

export { initChat };
export type { Chat, Model };
export type { Chat, ModelAlias };

0 comments on commit 0c6acc9

Please sign in to comment.