✨ gemini and chat bison

This commit is contained in:
Nate Sesti 2023-12-13 16:33:11 -08:00
parent adb308e58d
commit aefcd11428
16 changed files with 328 additions and 78 deletions

View file

@ -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;

View file

@ -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(

63
core/llm/llms/Gemini.ts Normal file
View file

@ -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<LLMOptions> = {
model: "gemini-pro",
region: "us-central1",
};
protected async *_streamComplete(
prompt: string,
options: CompletionOptions
): AsyncGenerator<string> {
for await (const chunk of this._streamChat(
[{ role: "user", content: prompt }],
options
)) {
yield chunk.content;
}
}
protected async *_streamChat(
messages: ChatMessage[],
options: CompletionOptions
): AsyncGenerator<ChatMessage> {
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;

View file

@ -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<LLMOptions> = {
model: "chat-bison-001",
};
private _convertArgs(options: CompletionOptions, prompt: string) {
const finalOptions = {};
return finalOptions;
}
protected async *_streamComplete(
prompt: string,
options: CompletionOptions
): AsyncGenerator<string> {
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<ChatMessage> {
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<ChatMessage> {
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<ChatMessage> {
@ -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 };
}
}

View file

@ -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<LLMOptions> = {
model: "chat-bison-001",
};
protected async *_streamComplete(
prompt: string,
options: CompletionOptions
): AsyncGenerator<string> {
for await (const message of this._streamChat(
[{ content: prompt, role: "user" }],
options
)) {
yield message.content;
}
}
protected async *_streamChat(
messages: ChatMessage[],
options: CompletionOptions
): AsyncGenerator<ChatMessage> {
const response = await ideRequest("googlePalmCompletion", {
modelTitle: this.title,
options,
messages,
});
yield { role: "user", content: response };
}
}
export default GooglePalmProxy;

12
core/llm/llms/Mistral.ts Normal file
View file

@ -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<LLMOptions> = {
apiBase: "https://api.mistral.ai/v1",
};
}
export default Mistral;

View file

@ -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",
};

View file

@ -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(

View file

@ -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<CompletionOptions> = {
// // temperature: 0.5,
// // topP: 1,
// // topK: 40,
// // presencePenalty: 0,
// // frequencyPenalty: 0,
// // // stop: ["\n"],
// // // maxTokens: 5,
// // };
// const COMPLETION_OPTIONS: Partial<CompletionOptions> = {
// 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 })
// // );
// });

View file

@ -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",

View file

@ -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",

View file

@ -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);
});
}

View file

@ -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;
}
}
});

BIN
gui/public/logos/gemini.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

View file

@ -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) {
<div className="flex items-center flex-wrap justify-end rtl">
{props.providerOptions?.map((option, i) => {
const info = PROVIDER_INFO[option];
if (!info) {
return null;
}
return (
<HeaderButtonWithText
text={info.title}

View file

@ -285,10 +285,38 @@ const mistral: ModelPackage = {
model: "mistral-7b",
contextLength: 2048,
},
dimensions: [
{
name: "Parameter Count",
description: "The number of parameters in the model",
options: {
"7b": {
model: "wizardcoder-7b",
title: "WizardCoder-7b",
},
"8x7b (MoE)": {
model: "mistral-8x7b",
title: "Mixtral",
},
},
},
],
icon: "mistral.png",
providerOptions: ["ollama", "lmstudio", "together", "llamacpp", "replicate"],
};
const gemini: ModelPackage = {
title: "Gemini",
description: "A highly capable model created by Google DeepMind",
params: {
title: "Gemini Pro",
model: "gemini-pro",
contextLength: 32_000,
},
icon: "gemini.png",
providerOptions: ["palm"],
};
const zephyr: ModelPackage = {
title: "Zephyr-Beta (7b)",
description:
@ -417,6 +445,7 @@ export const MODEL_INFO: ModelPackage[] = [
chatBison,
zephyr,
deepseek,
gemini,
];
export const PROVIDER_INFO: { [key: string]: ModelInfo } = {
@ -617,7 +646,7 @@ After it's up and running, you can start using Continue.`,
provider: "google-palm",
description:
"Try out the Google PaLM API, which is currently in public preview, using an API key from Google Makersuite",
longDescription: `To get started with Google Makersuite, obtain your API key from [here](https://developers.generativeai.google/products/makersuite) and paste it below.
longDescription: `To get started with Google Makersuite, obtain your API key from [here](https://makersuite.google.com) and paste it below.
> 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": {