From 286e569e098fd9b337b050491ae16dd8fca64223 Mon Sep 17 00:00:00 2001 From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Date: Tue, 3 Sep 2024 23:03:30 -0400 Subject: [PATCH] Add ollama provider option --- package.json | 2 +- src/api/index.ts | 3 + src/api/ollama.ts | 74 ++++++++++++++++++++++++ src/providers/ClaudeDevProvider.ts | 6 ++ src/shared/api.ts | 3 +- webview-ui/src/components/ApiOptions.tsx | 49 ++++++++++++++-- webview-ui/src/components/TaskHeader.tsx | 6 +- webview-ui/src/utils/validate.ts | 5 ++ 8 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 src/api/ollama.ts diff --git a/package.json b/package.json index 8801159..f6e0d5f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "claude-dev", "displayName": "Claude Dev", "description": "Autonomous coding agent right in your IDE, capable of creating/editing files, executing commands, and more with your permission every step of the way.", - "version": "1.5.20", + "version": "1.5.21", "icon": "icon.png", "engines": { "vscode": "^1.84.0" diff --git a/src/api/index.ts b/src/api/index.ts index 59b2bbc..40aeb80 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -5,6 +5,7 @@ import { AwsBedrockHandler } from "./bedrock" import { OpenRouterHandler } from "./openrouter" import { VertexHandler } from "./vertex" import { OpenAiHandler } from "./openai" +import { OllamaHandler } from "./ollama" export interface ApiHandlerMessageResponse { message: Anthropic.Messages.Message @@ -43,6 +44,8 @@ export function buildApiHandler(configuration: ApiConfiguration): ApiHandler { return new VertexHandler(options) case "openai": return new OpenAiHandler(options) + case "ollama": + return new OllamaHandler(options) default: return new AnthropicHandler(options) } diff --git a/src/api/ollama.ts b/src/api/ollama.ts new file mode 100644 index 0000000..323f99e --- /dev/null +++ b/src/api/ollama.ts @@ -0,0 +1,74 @@ +import { Anthropic } from "@anthropic-ai/sdk" +import OpenAI from "openai" +import { ApiHandler, ApiHandlerMessageResponse, withoutImageData } from "." +import { ApiHandlerOptions, ModelInfo, openAiModelInfoSaneDefaults } from "../shared/api" +import { convertToAnthropicMessage, convertToOpenAiMessages } from "../utils/openai-format" + +export class OllamaHandler implements ApiHandler { + private options: ApiHandlerOptions + private client: OpenAI + + constructor(options: ApiHandlerOptions) { + this.options = options + this.client = new OpenAI({ + baseURL: "http://localhost:11434/v1", + apiKey: "ollama", + }) + } + + async createMessage( + systemPrompt: string, + messages: Anthropic.Messages.MessageParam[], + tools: Anthropic.Messages.Tool[] + ): Promise { + const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { role: "system", content: systemPrompt }, + ...convertToOpenAiMessages(messages), + ] + const openAiTools: OpenAI.Chat.ChatCompletionTool[] = tools.map((tool) => ({ + type: "function", + function: { + name: tool.name, + description: tool.description, + parameters: tool.input_schema, + }, + })) + const createParams: OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming = { + model: this.options.ollamaModelId ?? "", + messages: openAiMessages, + tools: openAiTools, + tool_choice: "auto", + } + const completion = await this.client.chat.completions.create(createParams) + const errorMessage = (completion as any).error?.message + if (errorMessage) { + throw new Error(errorMessage) + } + const anthropicMessage = convertToAnthropicMessage(completion) + return { message: anthropicMessage } + } + + createUserReadableRequest( + userContent: Array< + | Anthropic.TextBlockParam + | Anthropic.ImageBlockParam + | Anthropic.ToolUseBlockParam + | Anthropic.ToolResultBlockParam + > + ): any { + return { + model: this.options.ollamaModelId ?? "", + system: "(see SYSTEM_PROMPT in src/ClaudeDev.ts)", + messages: [{ conversation_history: "..." }, { role: "user", content: withoutImageData(userContent) }], + tools: "(see tools in src/ClaudeDev.ts)", + tool_choice: "auto", + } + } + + getModel(): { id: string; info: ModelInfo } { + return { + id: this.options.ollamaModelId ?? "", + info: openAiModelInfoSaneDefaults, + } + } +} diff --git a/src/providers/ClaudeDevProvider.ts b/src/providers/ClaudeDevProvider.ts index 6249955..e0daf3a 100644 --- a/src/providers/ClaudeDevProvider.ts +++ b/src/providers/ClaudeDevProvider.ts @@ -29,6 +29,7 @@ type GlobalStateKey = | "taskHistory" | "openAiBaseUrl" | "openAiModelId" + | "ollamaModelId" export class ClaudeDevProvider implements vscode.WebviewViewProvider { public static readonly sideBarId = "claude-dev.SidebarProvider" // used in package.json as the view's id. This value cannot be changed due to how vscode caches views based on their id, and updating the id would break existing instances of the extension. @@ -319,6 +320,7 @@ export class ClaudeDevProvider implements vscode.WebviewViewProvider { openAiBaseUrl, openAiApiKey, openAiModelId, + ollamaModelId, } = message.apiConfiguration await this.updateGlobalState("apiProvider", apiProvider) await this.updateGlobalState("apiModelId", apiModelId) @@ -333,6 +335,7 @@ export class ClaudeDevProvider implements vscode.WebviewViewProvider { await this.updateGlobalState("openAiBaseUrl", openAiBaseUrl) await this.storeSecret("openAiApiKey", openAiApiKey) await this.updateGlobalState("openAiModelId", openAiModelId) + await this.updateGlobalState("ollamaModelId", ollamaModelId) this.claudeDev?.updateApi(message.apiConfiguration) } await this.postStateToWebview() @@ -623,6 +626,7 @@ export class ClaudeDevProvider implements vscode.WebviewViewProvider { openAiBaseUrl, openAiApiKey, openAiModelId, + ollamaModelId, lastShownAnnouncementId, customInstructions, alwaysAllowReadOnly, @@ -641,6 +645,7 @@ export class ClaudeDevProvider implements vscode.WebviewViewProvider { this.getGlobalState("openAiBaseUrl") as Promise, this.getSecret("openAiApiKey") as Promise, this.getGlobalState("openAiModelId") as Promise, + this.getGlobalState("ollamaModelId") as Promise, this.getGlobalState("lastShownAnnouncementId") as Promise, this.getGlobalState("customInstructions") as Promise, this.getGlobalState("alwaysAllowReadOnly") as Promise, @@ -676,6 +681,7 @@ export class ClaudeDevProvider implements vscode.WebviewViewProvider { openAiBaseUrl, openAiApiKey, openAiModelId, + ollamaModelId, }, lastShownAnnouncementId, customInstructions, diff --git a/src/shared/api.ts b/src/shared/api.ts index 6c2a3de..cac46aa 100644 --- a/src/shared/api.ts +++ b/src/shared/api.ts @@ -1,4 +1,4 @@ -export type ApiProvider = "anthropic" | "openrouter" | "bedrock" | "vertex" | "openai" +export type ApiProvider = "anthropic" | "openrouter" | "bedrock" | "vertex" | "openai" | "ollama" export interface ApiHandlerOptions { apiModelId?: string @@ -13,6 +13,7 @@ export interface ApiHandlerOptions { openAiBaseUrl?: string openAiApiKey?: string openAiModelId?: string + ollamaModelId?: string } export type ApiConfiguration = ApiHandlerOptions & { diff --git a/webview-ui/src/components/ApiOptions.tsx b/webview-ui/src/components/ApiOptions.tsx index a4a480f..fce7ac6 100644 --- a/webview-ui/src/components/ApiOptions.tsx +++ b/webview-ui/src/components/ApiOptions.tsx @@ -79,6 +79,7 @@ const ApiOptions: React.FC = ({ showModelOptions, apiErrorMessa AWS Bedrock GCP Vertex AI OpenAI Compatible + Ollama @@ -268,7 +269,7 @@ const ApiOptions: React.FC = ({ showModelOptions, apiErrorMessa style={{ width: "100%" }} type="url" onInput={handleInputChange("openAiBaseUrl")} - placeholder={"e.g. http://localhost:11434/v1"}> + placeholder={"Enter base URL..."}> Base URL = ({ showModelOptions, apiErrorMessa style={{ width: "100%" }} type="password" onInput={handleInputChange("openAiApiKey")} - placeholder="e.g. ollama"> + placeholder="Enter API Key..."> API Key + placeholder={"Enter Model ID..."}> Model ID

= ({ showModelOptions, apiErrorMessa )} + {selectedProvider === "ollama" && ( +

+ + Model ID + +

+ Ollama allows you to run models locally on your computer. For instructions on how to get started + with Ollama, see their + + quickstart guide. + {" "} + You can use any models that support{" "} + + tool use. + + + (Note: Claude Dev uses complex prompts, so less + capable models may not work as expected.) + +

+
+ )} + {apiErrorMessage && (

= ({ showModelOptions, apiErrorMessa

)} - {selectedProvider !== "openai" && showModelOptions && ( + {selectedProvider !== "openai" && selectedProvider !== "ollama" && showModelOptions && ( <>
- {apiConfiguration?.apiProvider === "openai" && } + {(apiConfiguration?.apiProvider === "openai" || apiConfiguration?.apiProvider === "ollama") && ( + + )} {(doesModelSupportPromptCache || cacheReads !== undefined || cacheWrites !== undefined) && ( @@ -248,7 +250,7 @@ const TaskHeader: React.FC = ({ )} - {apiConfiguration?.apiProvider !== "openai" && ( + {apiConfiguration?.apiProvider !== "openai" && apiConfiguration?.apiProvider !== "ollama" && (