diff --git a/core/config/index.d.ts b/core/config/index.d.ts index 7cf84f966..12c5c5df6 100644 --- a/core/config/index.d.ts +++ b/core/config/index.d.ts @@ -52,8 +52,11 @@ type ModelProvider = | "replicate" | "text-gen-webui" | "google-palm" + | "google-palm-real" | "lmstudio" - | "llamafile"; + | "llamafile" + | "gemini" + | "mistral"; export type ModelName = // OpenAI @@ -65,6 +68,7 @@ export type ModelName = | "gpt-4-1106-preview" // Open Source | "mistral-7b" + | "mistral-8x7b" | "llama2-7b" | "llama2-13b" | "codellama-7b" @@ -82,7 +86,9 @@ export type ModelName = // Anthropic | "claude-2" // Google PaLM - | "chat-bison-001"; + | "chat-bison-001" + // Gemini + | "gemini-pro"; interface RequestOptions { timeout?: number; diff --git a/core/llm/index.ts b/core/llm/index.ts index 581711a35..5f7a59aa4 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -30,7 +30,8 @@ function autodetectTemplateType(model: string): TemplateType | undefined { if ( lower.includes("gpt") || lower.includes("chat-bison") || - lower.includes("pplx") + lower.includes("pplx") || + lower.includes("gemini") ) { return undefined; } @@ -139,6 +140,10 @@ export interface LLMOptions { engine?: string; apiVersion?: string; apiType?: string; + + // GCP Options + region?: string; + projectId?: string; } interface LLMFullCompletionOptions { @@ -182,6 +187,8 @@ export abstract class LLM implements LLMOptions { engine?: string; apiVersion?: string; apiType?: string; + region?: string; + projectId?: string; constructor(options: LLMOptions) { // Set default options @@ -219,6 +226,8 @@ export abstract class LLM implements LLMOptions { this.engine = options.engine; this.apiVersion = options.apiVersion; this.apiType = options.apiType; + this.region = options.region; + this.projectId = options.projectId; } private _compileChatMessages( diff --git a/core/llm/llms/Gemini.ts b/core/llm/llms/Gemini.ts new file mode 100644 index 000000000..c165d166a --- /dev/null +++ b/core/llm/llms/Gemini.ts @@ -0,0 +1,63 @@ +import { LLM, LLMOptions } from ".."; +import { ModelProvider } from "../../config"; +import { ChatMessage, CompletionOptions } from "../types"; + +class Gemini extends LLM { + static providerName: ModelProvider = "gemini"; + + static defaultOptions: Partial = { + model: "gemini-pro", + region: "us-central1", + }; + + protected async *_streamComplete( + prompt: string, + options: CompletionOptions + ): AsyncGenerator { + for await (const chunk of this._streamChat( + [{ role: "user", content: prompt }], + options + )) { + yield chunk.content; + } + } + + protected async *_streamChat( + messages: ChatMessage[], + options: CompletionOptions + ): AsyncGenerator { + const apiUrl = `https://${this.region}-aiplatform.googleapis.com/v1/projects/${this.projectId}/locations/us-central1/publishers/google/models/gemini-pro:streamGenerateContent`; + const body = { + contents: messages.map((msg) => { + return { + role: msg.role === "assistant" ? "ASSISTANT" : "USER", + parts: [{ text: msg.content }], + }; + }), + generationConfig: { + temperature: options.temperature, + topP: options.topP, + topK: options.topK, + candidateCount: 1, + maxOutputTokens: options.maxTokens, + stopSequences: options.stop, + }, + }; + const response = await fetch(apiUrl, { + method: "POST", + body: JSON.stringify(body), + headers: { + Authorization: `Bearer ${this.apiKey}`, + "Content-Type": "application/json; charset=utf-8", + }, + }); + console.log(await response.text(), "GEMINIIIIIIIIIII..."); + const data = await response.json(); + if (data[0]?.error) { + throw new Error(data[0].error.message); + } + yield data[0]?.candidates?.[0]?.content?.parts?.[0]?.text || ""; + } +} + +export default Gemini; diff --git a/core/llm/llms/GooglePalm.ts b/core/llm/llms/GooglePalm.ts index d576e3433..39e4a773f 100644 --- a/core/llm/llms/GooglePalm.ts +++ b/core/llm/llms/GooglePalm.ts @@ -3,33 +3,66 @@ import { ModelProvider } from "../../config"; import { ChatMessage, CompletionOptions } from "../types"; class GooglePalm extends LLM { - static providerName: ModelProvider = "google-palm"; + static providerName: ModelProvider = "google-palm-real"; static defaultOptions: Partial = { model: "chat-bison-001", }; - private _convertArgs(options: CompletionOptions, prompt: string) { - const finalOptions = {}; - - return finalOptions; - } - protected async *_streamComplete( prompt: string, options: CompletionOptions ): AsyncGenerator { - const apiURL = `https://generativelanguage.googleapis.com/v1beta2/models/${options.model}:generateMessage?key=${this.apiKey}`; - const body = { prompt: { messages: [{ content: prompt }] } }; + for await (const message of this._streamChat( + [{ content: prompt, role: "user" }], + options + )) { + yield message.content; + } + } + + protected async *_streamChat( + messages: ChatMessage[], + options: CompletionOptions + ): AsyncGenerator { + if (options.model.includes("gemini")) { + for await (const message of this.streamChatGemini(messages, options)) { + yield message; + } + } else { + for await (const message of this.streamChatBison(messages, options)) { + yield message; + } + } + } + + private async *streamChatGemini( + messages: ChatMessage[], + options: CompletionOptions + ): AsyncGenerator { + const apiURL = `https://generativelanguage.googleapis.com/v1beta/models/${options.model}:generateMessage?key=${this.apiKey}`; + const body = { + contents: messages.map((msg) => { + return { + role: msg.role === "assistant" ? "ASSISTANT" : "USER", + parts: [{ text: msg.content }], + }; + }), + }; const response = await fetch(apiURL, { method: "POST", body: JSON.stringify(body), }); const data = await response.json(); - return data.candidates[0].content; + if (data.error) { + throw new Error(data.error.message); + } + yield { + role: "assistant", + content: data.candidates?.[0]?.content?.parts?.[0]?.text || "", + }; } - - protected async *_streamChat( + private async *streamChatBison( messages: ChatMessage[], options: CompletionOptions ): AsyncGenerator { @@ -45,7 +78,7 @@ class GooglePalm extends LLM { body: JSON.stringify(body), }); const data = await response.json(); - return { role: "assistant", content: data.candidates[0].content }; + yield { role: "assistant", content: data.candidates[0].content }; } } diff --git a/core/llm/llms/GooglePalmProxy.ts b/core/llm/llms/GooglePalmProxy.ts new file mode 100644 index 000000000..e335a231b --- /dev/null +++ b/core/llm/llms/GooglePalmProxy.ts @@ -0,0 +1,37 @@ +import { LLM, LLMOptions } from ".."; +import { ModelProvider } from "../../config"; +import { ideRequest } from "../../ide/messaging"; +import { ChatMessage, CompletionOptions } from "../types"; + +class GooglePalmProxy extends LLM { + static providerName: ModelProvider = "google-palm"; + static defaultOptions: Partial = { + model: "chat-bison-001", + }; + + protected async *_streamComplete( + prompt: string, + options: CompletionOptions + ): AsyncGenerator { + for await (const message of this._streamChat( + [{ content: prompt, role: "user" }], + options + )) { + yield message.content; + } + } + + protected async *_streamChat( + messages: ChatMessage[], + options: CompletionOptions + ): AsyncGenerator { + const response = await ideRequest("googlePalmCompletion", { + modelTitle: this.title, + options, + messages, + }); + yield { role: "user", content: response }; + } +} + +export default GooglePalmProxy; diff --git a/core/llm/llms/Mistral.ts b/core/llm/llms/Mistral.ts new file mode 100644 index 000000000..05d2305eb --- /dev/null +++ b/core/llm/llms/Mistral.ts @@ -0,0 +1,12 @@ +import { LLMOptions } from ".."; +import { ModelProvider } from "../../config"; +import OpenAI from "./OpenAI"; + +class Mistral extends OpenAI { + static providerName: ModelProvider = "mistral"; + static defaultOptions: Partial = { + apiBase: "https://api.mistral.ai/v1", + }; +} + +export default Mistral; diff --git a/core/llm/llms/Together.ts b/core/llm/llms/Together.ts index 525365470..6bcd6ae68 100644 --- a/core/llm/llms/Together.ts +++ b/core/llm/llms/Together.ts @@ -16,6 +16,7 @@ class Together extends LLM { "llama2-13b": "togethercomputer/llama-2-13b-chat", "llama2-70b": "togethercomputer/llama-2-70b-chat", "mistral-7b": "mistralai/Mistral-7B-Instruct-v0.1", + "mistral-8x7b": "mistralai/Mixtral-8x7B-Instruct-v0.1", "phind-codellama-34b": "Phind/Phind-CodeLlama-34B-v2", }; diff --git a/core/llm/llms/index.ts b/core/llm/llms/index.ts index 99afa5bcc..7a531c175 100644 --- a/core/llm/llms/index.ts +++ b/core/llm/llms/index.ts @@ -2,7 +2,9 @@ import { LLM, LLMOptions } from ".."; import { BaseCompletionOptions, ModelDescription } from "../../config"; import Anthropic from "./Anthropic"; import FreeTrial from "./FreeTrial"; +import Gemini from "./Gemini"; import GooglePalm from "./GooglePalm"; +import GooglePalmProxy from "./GooglePalmProxy"; import HuggingFaceInferenceAPI from "./HuggingFaceInferenceAPI"; import HuggingFaceTGI from "./HuggingFaceTGI"; import LMStudio from "./LMStudio"; @@ -17,6 +19,7 @@ import Together from "./Together"; const LLMs = [ Anthropic, FreeTrial, + GooglePalmProxy, // TODO: Hacky fix for now bc of proxy details GooglePalm, Llamafile, Ollama, @@ -28,6 +31,7 @@ const LLMs = [ LlamaCpp, OpenAI, LMStudio, + Gemini, ]; export function llmFromDescription( diff --git a/core/llm/test/llm.test.ts b/core/llm/test/llm.test.ts index bdaf989e9..df8a38656 100644 --- a/core/llm/test/llm.test.ts +++ b/core/llm/test/llm.test.ts @@ -1,61 +1,61 @@ -// // import * as dotenv from "dotenv"; -// // import { LLM } from ".."; -// // import Together from "../llms/Together"; -// // import { CompletionOptions } from "../types"; +// import * as dotenv from "dotenv"; +// import { LLM } from ".."; +// import GooglePalm from "../llms/GooglePalm"; +// import { CompletionOptions } from "../types"; -// // jest.setTimeout(100_000); +// jest.setTimeout(100_000); -// // dotenv.config(); +// dotenv.config(); -// // const COMPLETION_OPTIONS: Partial = { -// // temperature: 0.5, -// // topP: 1, -// // topK: 40, -// // presencePenalty: 0, -// // frequencyPenalty: 0, -// // // stop: ["\n"], -// // // maxTokens: 5, -// // }; +// const COMPLETION_OPTIONS: Partial = { +// temperature: 0.5, +// topP: 1, +// topK: 40, +// presencePenalty: 0, +// frequencyPenalty: 0, +// // stop: ["\n"], +// // maxTokens: 5, +// }; -// // function testLLM(llm: LLM) { -// // beforeAll(() => { -// // llm.completionOptions = { ...llm.completionOptions, ...COMPLETION_OPTIONS }; -// // }); +// function testLLM(llm: LLM) { +// beforeAll(() => { +// llm.completionOptions = { ...llm.completionOptions, ...COMPLETION_OPTIONS }; +// }); -// // describe(llm.providerName, () => { -// // test("Stream Chat works", async () => { -// // let total = ""; -// // for await (const chunk of llm.streamChat([ -// // { role: "user", content: "Hello" }, -// // ])) { -// // total += chunk.content; -// // } +// describe(llm.providerName, () => { +// test("Stream Chat works", async () => { +// let total = ""; +// for await (const chunk of llm.streamChat([ +// { role: "user", content: "Hello" }, +// ])) { +// total += chunk.content; +// } -// // expect(total.length).toBeGreaterThan(0); -// // console.log(total); -// // return; -// // }); +// expect(total.length).toBeGreaterThan(0); +// console.log(total); +// return; +// }); -// // test("Stream Complete works", async () => { -// // let total = ""; -// // for await (const chunk of llm.streamComplete("Hello")) { -// // total += chunk; -// // } +// test("Stream Complete works", async () => { +// let total = ""; +// for await (const chunk of llm.streamComplete("Hello")) { +// total += chunk; +// } -// // expect(total.length).toBeGreaterThan(0); -// // console.log(total); -// // return; -// // }); +// expect(total.length).toBeGreaterThan(0); +// console.log(total); +// return; +// }); -// // test("Complete works", async () => { -// // const completion = await llm.complete("Hello"); +// test("Complete works", async () => { +// const completion = await llm.complete("Hello"); -// // expect(completion.length).toBeGreaterThan(0); -// // console.log(completion); -// // return; -// // }); -// // }); -// // } +// expect(completion.length).toBeGreaterThan(0); +// console.log(completion); +// return; +// }); +// }); +// } // describe("LLM", () => { // // // testLLM( @@ -93,7 +93,14 @@ // // // testLLM(new TextGenWebUI({ model: "codellama-7b" })); // // // testLLM(new HuggingFaceTGI({ model: "codellama-7b" })); // // // testLLM(new HuggingFaceInferenceAPI({ model: "codellama-7b" })); -// test.skip("works", () => { -// expect(true).toBe(true); -// }); +// testLLM( +// new GooglePalm({ +// model: "gemini-pro", +// // model: "chat-bison-001", +// apiKey: process.env.GOOGLE_PALM_API_KEY, +// }) +// ); +// // testLLM( +// // new Gemini({ model: "gemini-pro", apiKey: process.env.GOOGLE_PALM_API_KEY }) +// // ); // }); diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index 940987156..86acd2930 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -185,6 +185,21 @@ "$ref": "#/definitions/RequestOptions" } ] + }, + "apiType": { + "title": "Api Type", + "description": "OpenAI API type, either `openai` or `azure`", + "enum": ["openai", "azure"] + }, + "apiVersion": { + "title": "Api Version", + "description": "Azure OpenAI API version (e.g. 2023-07-01-preview)", + "type": "string" + }, + "engine": { + "title": "Engine", + "description": "Azure OpenAI engine", + "type": "string" } }, "required": ["title", "provider", "model"], @@ -211,6 +226,7 @@ "gpt-4-32k", "gpt-4-1106-preview", "mistral-7b", + "mistral-8x7b", "llama2-7b", "llama2-13b", "codellama-7b", @@ -224,7 +240,8 @@ "codeup-13b", "deepseek-1b", "deepseek-7b", - "deepseek-33b" + "deepseek-33b", + "gemini-pro" ] } } @@ -375,6 +392,7 @@ "gpt-4-32k", "gpt-4-1106-preview", "mistral-7b", + "mistral-8x7b", "llama2-7b", "llama2-13b", "codellama-7b", @@ -423,7 +441,7 @@ "then": { "properties": { "model": { - "enum": ["chat-bison-001"] + "enum": ["chat-bison-001", "gemini-pro"] } } } @@ -442,6 +460,7 @@ "model": { "enum": [ "mistral-7b", + "mistral-8x7b", "llama2-7b", "llama2-13b", "codellama-7b", @@ -473,6 +492,7 @@ "model": { "enum": [ "mistral-7b", + "mistral-8x7b", "llama2-7b", "llama2-13b", "codellama-7b", diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index 7f3ee546b..0e7f19ca9 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -80,11 +80,6 @@ "category": "Continue", "title": "Quick Text Entry" }, - { - "command": "continue.viewLogs", - "category": "Continue", - "title": "View Continue Server Logs" - }, { "command": "continue.toggleAuxiliaryBar", "category": "Continue", diff --git a/extensions/vscode/src/activation/proxy.ts b/extensions/vscode/src/activation/proxy.ts index 7b482d435..a31923614 100644 --- a/extensions/vscode/src/activation/proxy.ts +++ b/extensions/vscode/src/activation/proxy.ts @@ -54,7 +54,10 @@ app.use((req, res, next) => { // }); export function startProxy() { - app.listen(PROXY_PORT, () => { + const server = app.listen(PROXY_PORT, () => { console.log(`Proxy server is running on port ${PROXY_PORT}`); }); + server.on("error", (e) => { + console.log("Proxy server already running on 65433: ", e.message); + }); } diff --git a/extensions/vscode/src/debugPanel.ts b/extensions/vscode/src/debugPanel.ts index 953150b99..f99e54020 100644 --- a/extensions/vscode/src/debugPanel.ts +++ b/extensions/vscode/src/debugPanel.ts @@ -1,4 +1,5 @@ import { ModelDescription } from "core/config"; +import { llmFromDescription } from "core/llm/llms"; import { FileEdit } from "core/types"; import { editConfigJson, @@ -393,6 +394,32 @@ export function getSidebarContent( ideProtocolClient.configUpdate(configJson); break; } + case "googlePalmCompletion": { + const sConfig = await ide.getSerializedConfig(); + const modelDesc = sConfig.models.find( + (m) => + m.provider === "google-palm" && m.title === data.message.modelTitle + ); + if (!modelDesc) { + return ""; + } + const model = llmFromDescription({ + ...modelDesc, + provider: "google-palm-real", + }); + if (!model) { + return ""; + } + let completion = ""; + for await (const update of model.streamChat( + data.message.messages, + data.message.options + )) { + completion += update.content; + } + respond(completion); + break; + } } }); diff --git a/gui/public/logos/gemini.png b/gui/public/logos/gemini.png new file mode 100644 index 000000000..8496a4b08 Binary files /dev/null and b/gui/public/logos/gemini.png differ diff --git a/gui/src/components/modelSelection/ModelCard.tsx b/gui/src/components/modelSelection/ModelCard.tsx index dee9b82ec..f16c95885 100644 --- a/gui/src/components/modelSelection/ModelCard.tsx +++ b/gui/src/components/modelSelection/ModelCard.tsx @@ -1,13 +1,13 @@ +import { BookOpenIcon } from "@heroicons/react/24/outline"; import React, { useEffect, useState } from "react"; import styled from "styled-components"; import { buttonColor, defaultBorderRadius, lightGray } from ".."; -import { BookOpenIcon } from "@heroicons/react/24/outline"; -import HeaderButtonWithText from "../HeaderButtonWithText"; import { MODEL_PROVIDER_TAG_COLORS, PROVIDER_INFO, PackageDimension, } from "../../util/modelData"; +import HeaderButtonWithText from "../HeaderButtonWithText"; import InfoHover from "../InfoHover"; const Div = styled.div<{ color: string; disabled: boolean; hovered: boolean }>` @@ -211,6 +211,9 @@ function ModelCard(props: ModelCardProps) {
{props.providerOptions?.map((option, i) => { const info = PROVIDER_INFO[option]; + if (!info) { + return null; + } return ( Note: Google's PaLM language models do not support streaming, so the response will appear all at once after a few seconds.`, icon: "google-palm.png", tags: [ModelProviderTag["Requires API Key"]], @@ -640,6 +669,7 @@ After it's up and running, you can start using Continue.`, contextLength: 8000, }, }, + gemini, ], }, "openai-aiohttp": {