| 1 | import { |
| 2 | ChatMessage, |
| 3 | CompletionOptions, |
| 4 | LLMOptions, |
| 5 | TextMessagePart, |
| 6 | } from "../../index.js"; |
| 7 | import { BaseLLM } from "../index.js"; |
| 8 | |
| 9 | class Asksage extends BaseLLM { |
| 10 | static providerName = "askSage"; |
| 11 | static defaultOptions: Partial<LLMOptions> = { |
| 12 | apiBase: "https://api.asksage.ai/server/", |
| 13 | model: "gpt-4o", |
| 14 | }; |
| 15 | |
| 16 | private static modelConversion: { [key: string]: string } = { |
| 17 | "gpt-4o-gov": "gpt-4o-gov", // Works |
| 18 | "gpt-4o-mini-gov": "gpt-4o-mini-gov", |
| 19 | "gpt4-gov": "gpt4-gov", // Works |
| 20 | "gpt-gov": "gpt-gov", // Works |
| 21 | "gpt-4o": "gpt-4o", // Works |
| 22 | "gpt-4o-mini": "gpt-4o-mini", // Works |
| 23 | gpt4: "gpt4", // Works |
| 24 | "gpt4-32k": "gpt4-32k", |
| 25 | "gpt-o1": "gpt-o1", // Works |
| 26 | "gpt-o1-mini": "gpt-o1-mini", // Works |
| 27 | "gpt-o3-mini": "gpt-o3-mini", // Stub |
| 28 | "gpt-3.5-turbo": "gpt35-16k", // Works |
| 29 | "aws-bedrock-claude-35-sonnet-gov": "aws-bedrock-claude-35-sonnet-gov", // Works |
| 30 | "claude-3-5-sonnet-latest": "claude-35-sonnet", // Works |
| 31 | "claude-3-opus-20240229": "claude-3-opus", // Works |
| 32 | "claude-3-sonnet-20240229": "claude-3-sonnet", // Works |
| 33 | "grok-beta": "xai-grok", |
| 34 | "groq-llama33": "groq-llama33", |
| 35 | "groq-70b": "groq-70b", |
| 36 | "mistral-large-latest": "mistral-large", // Works |
| 37 | "llama3-70b": "llma3", // Works |
| 38 | "gemini-1.5-pro-latest": "google-gemini-pro", // Works |
| 39 | }; |
| 40 | |
| 41 | constructor(options: LLMOptions) { |
| 42 | super(options); |
| 43 | this.apiVersion = options.apiVersion ?? "v1.2.4"; |
| 44 | } |
| 45 | |
| 46 | protected _convertModelName(model: string): string { |
| 47 | console.log("Converting model:", model); |
| 48 | const convertedModel = Asksage.modelConversion[model] ?? model; |
| 49 | console.log("Converted model:", convertedModel); |
| 50 | return convertedModel; |
| 51 | } |
| 52 | |
| 53 | protected _convertMessage(message: ChatMessage) { |
| 54 | return { |
| 55 | user: message.role === "assistant" ? "gpt" : "me", |
| 56 | message: |
| 57 | typeof message.content === "string" |
| 58 | ? message.content |
| 59 | : message.content |
| 60 | .filter((part) => part.type === "text") |
| 61 | .map((part) => (part as TextMessagePart).text) |
| 62 | .join(""), |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | protected _convertArgs(options: any, messages: ChatMessage[]) { |
| 67 | let formattedMessage: any; |
| 68 | if (messages.length === 1) { |
| 69 | formattedMessage = messages[0].content; |
| 70 | } else { |
| 71 | formattedMessage = messages.map(this._convertMessage); |
| 72 | } |
| 73 | |
| 74 | const args: any = { |
| 75 | message: formattedMessage, |
| 76 | persona: options.persona ?? "default", |
| 77 | dataset: options.dataset ?? "none", |
| 78 | limit_references: options.limitReferences ?? 0, |
| 79 | temperature: options.temperature ?? 0.0, |
| 80 | live: options.live ?? 0, |
| 81 | model: this._convertModelName(options.model), |
| 82 | system_prompt: |
| 83 | options.systemPrompt ?? |
| 84 | "You are an expert software developer. You give helpful and concise responses.", |
| 85 | tools: options.tools, |
| 86 | tool_choice: options.toolChoice, |
| 87 | }; |
| 88 | |
| 89 | Object.keys(args).forEach( |
| 90 | (key) => args[key] === undefined && delete args[key], |
| 91 | ); |
| 92 | |
| 93 | return args; |
| 94 | } |
| 95 | |
| 96 | protected _getHeaders() { |
| 97 | const headers: Record<string, string> = { |
| 98 | "Content-Type": "application/json", |
| 99 | }; |
| 100 | |
| 101 | if (this.apiKey) { |
| 102 | headers["x-access-tokens"] = this.apiKey; |
| 103 | } |
| 104 | |
| 105 | return headers; |
| 106 | } |
| 107 | |
| 108 | protected _getEndpoint(endpoint: string) { |
| 109 | if (!this.apiBase) { |
| 110 | throw new Error( |
| 111 | "No API base URL provided. Please set the 'apiBase' option.", |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | return new URL(endpoint, this.apiBase); |
| 116 | } |
| 117 | |
| 118 | protected async _complete( |
| 119 | prompt: string, |
| 120 | signal: AbortSignal, |
| 121 | options: CompletionOptions, |
| 122 | ): Promise<string> { |
| 123 | if (typeof prompt !== "string" || prompt.trim() === "") { |
| 124 | throw new Error("Prompt must be a non-empty string."); |
| 125 | } |
| 126 | |
| 127 | const messages: ChatMessage[] = [{ role: "user", content: prompt }]; |
| 128 | |
| 129 | const args = this._convertArgs(options, messages); |
| 130 | |
| 131 | const response = await this.fetch(this._getEndpoint("query"), { |
| 132 | method: "POST", |
| 133 | headers: this._getHeaders(), |
| 134 | body: JSON.stringify(args), |
| 135 | }); |
| 136 | |
| 137 | if (response.status === 499) { |
| 138 | return ""; // Aborted by user |
| 139 | } |
| 140 | |
| 141 | const data = await response.json(); |
| 142 | return data.message; |
| 143 | } |
| 144 | |
| 145 | protected async *_streamComplete( |
| 146 | prompt: string, |
| 147 | signal: AbortSignal, |
| 148 | options: CompletionOptions, |
| 149 | ): AsyncGenerator<string> { |
| 150 | const completion = await this._complete(prompt, signal, options); |
| 151 | yield completion; |
| 152 | } |
| 153 | |
| 154 | protected async *_streamChat( |
| 155 | messages: ChatMessage[], |
| 156 | signal: AbortSignal, |
| 157 | options: CompletionOptions, |
| 158 | ): AsyncGenerator<ChatMessage> { |
| 159 | const args = this._convertArgs(options, messages); |
| 160 | |
| 161 | const response = await this.fetch(this._getEndpoint("query"), { |
| 162 | method: "POST", |
| 163 | headers: this._getHeaders(), |
| 164 | body: JSON.stringify(args), |
| 165 | signal, |
| 166 | }); |
| 167 | |
| 168 | if (response.status === 499) { |
| 169 | return; // Aborted by user |
| 170 | } |
| 171 | |
| 172 | const data = await response.json(); |
| 173 | |
| 174 | const assistantMessage: ChatMessage = { |
| 175 | role: "assistant", |
| 176 | content: data.message, |
| 177 | }; |
| 178 | |
| 179 | yield assistantMessage; |
| 180 | } |
| 181 | |
| 182 | async listModels(): Promise<string[]> { |
| 183 | return Object.keys(Asksage.modelConversion); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | export default Asksage; |