Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add OLLAMA as Model Provider #221

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ XAI_API_KEY=
XAI_MODEL=


#Set to Use for New OLLAMA provider
OLLAMA_SERVER_URL= #Leave blank for default localhost:11434
OLLAMA_MODEL=
OLLAMA_EMBEDDING_MODEL= #default mxbai-embed-large
#To use custom model types for different tasks set these
SMALL_OLLAMA_MODEL= #default llama3.2
MEDIUM_OLLAMA_MODEL= #default herems3
LARGE_OLLAMA_MODEL= #default hermes3:70b

#to still use the original LOCALLLAMA provider but with ollama
LOCAL_LLAMA_PROVIDER= #"OLLAMA" #Leave blank for LLAMA-CPP or add OLLAMA



# For asking Claude stuff
ANTHROPIC_API_KEY=

Expand Down
1 change: 1 addition & 0 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@ai-sdk/google-vertex": "^0.0.42",
"@ai-sdk/groq": "^0.0.3",
"@ai-sdk/openai": "^0.0.70",
"ollama-ai-provider": "^0.16.1",
"@anthropic-ai/sdk": "^0.30.1",
"@cliqz/adblocker-playwright": "1.34.0",
"@coral-xyz/anchor": "^0.30.1",
Expand Down
9 changes: 6 additions & 3 deletions core/src/core/embedding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ export async function embed(runtime: IAgentRuntime, input: string) {
// get the charcter, and handle by model type
const model = models[runtime.character.settings.model];

if (model !== ModelProvider.OPENAI) {
if (model !== ModelProvider.OPENAI && model !== ModelProvider.OLLAMA) {
return await runtime.llamaService.getEmbeddingResponse(input);
//ollama supports embedding api so just use that
}

const embeddingModel = models[runtime.modelProvider].model.embedding;
Expand All @@ -26,7 +27,8 @@ export async function embed(runtime: IAgentRuntime, input: string) {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${runtime.token}`,
//Authorization: `Bearer ${runtime.token}`,
...(runtime.modelProvider !== ModelProvider.OLLAMA && { Authorization: `Bearer ${runtime.token}` }),
},
body: JSON.stringify({
input,
Expand All @@ -36,7 +38,8 @@ export async function embed(runtime: IAgentRuntime, input: string) {
};
try {
const response = await fetch(
`${runtime.serverUrl}/embeddings`,
//`${runtime.serverUrl}/embeddings`,
`${runtime.serverUrl}${runtime.modelProvider === ModelProvider.OLLAMA ? '/v1' : ''}/embeddings`,
requestOptions
);

Expand Down
26 changes: 26 additions & 0 deletions core/src/core/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { generateText as aiGenerateText } from "ai";
import { createAnthropic } from "@ai-sdk/anthropic";
import { prettyConsole } from "../index.ts";

import { createOllama } from 'ollama-ai-provider';

/**
* Send a message to the model for a text generateText - receive a string back and parse how you'd like
* @param opts - The options for the generateText request.
Expand Down Expand Up @@ -190,6 +192,30 @@ export async function generateText({
break;
}

case ModelProvider.OLLAMA: {
console.log("Initializing Ollama model.");

const ollamaProvider = createOllama({
baseURL: models[provider].endpoint + "/api",
})
const ollama = ollamaProvider(model);

console.log('****** MODEL\n', model)

const { text: ollamaResponse } = await aiGenerateText({
model: ollama,
prompt: context,
temperature: temperature,
maxTokens: max_response_length,
frequencyPenalty: frequency_penalty,
presencePenalty: presence_penalty,
});

response = ollamaResponse;
}
console.log("Received response from Ollama model.");
break;

default: {
const errorMessage = `Unsupported provider: ${provider}`;
prettyConsole.error(errorMessage);
Expand Down
18 changes: 18 additions & 0 deletions core/src/core/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Models = {
[ModelProvider.GOOGLE]: Model;
[ModelProvider.CLAUDE_VERTEX]: Model;
[ModelProvider.REDPILL]: Model;
[ModelProvider.OLLAMA]: Model;
// TODO: add OpenRouter - feel free to do this :)
};

Expand Down Expand Up @@ -169,6 +170,23 @@ const models: Models = {
[ModelClass.EMBEDDING]: "text-embedding-3-small",
},
},
[ModelProvider.OLLAMA]: {
settings: {
stop: [],
maxInputTokens: 128000,
maxOutputTokens: 8192,
frequency_penalty: 0.0,
presence_penalty: 0.0,
temperature: 0.3,
},
endpoint: process.env.OLLAMA_SERVER_URL || "http://localhost:11434",
model: {
[ModelClass.SMALL]: process.env.SMALL_OLLAMA_MODEL || process.env.OLLAMA_MODEL || "llama3.2",
[ModelClass.MEDIUM]: process.env.MEDIUM_OLLAMA_MODEL ||process.env.OLLAMA_MODEL || "hermes3",
[ModelClass.LARGE]: process.env.LARGE_OLLAMA_MODEL || process.env.OLLAMA_MODEL || "hermes3:70b",
[ModelClass.EMBEDDING]: process.env.OLLAMA_EMBEDDING_MODEL || "mxbai-embed-large"
},
},
};

export function getModel(provider: ModelProvider, type: ModelClass) {
Expand Down
3 changes: 2 additions & 1 deletion core/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export enum ModelProvider {
LLAMALOCAL = "llama_local",
GOOGLE = "google",
CLAUDE_VERTEX = "claude_vertex",
REDPILL = "redpill"
REDPILL = "redpill",
OLLAMA = "ollama"
}

/**
Expand Down
Loading