Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Remove model type and refactor code for Yorkie Intelligence #374

Merged
merged 5 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions backend/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ YORKIE_PROJECT_NAME=default
YORKIE_PROJECT_SECRET_KEY=""

# YORKIE_INTELLIGENCE: Whether to enable Yorkie Intelligence for collaborative editing.
# Available options: false, ollama:lamma3.1, ollama:gemma2, ollama:gemma2:2b, ollama:phi3, ollama:mistral, ollama:neural-chat, ollama:starling-lm, ollama:solar, openai:gpt-3.5-turbo, openai:gpt-4o-mini
# Available options: false, ollama:llama3.1, ollama:gemma2, ollama:gemma2:2b, ollama:phi3, ollama:mistral, ollama:neural-chat, ollama:starling-lm, ollama:solar, openai:gpt-3.5-turbo, openai:gpt-4o-mini, etc.
# If set to openai:gpt-3.5-turbo or openai:gpt-4o-mini, OPENAI_API_KEY is not required.
YORKIE_INTELLIGENCE="ollama:gemma2:2b"
YORKIE_INTELLIGENCE="ollama:llama3.2:1b"

# OLLAMA_HOST_URL: yorkie-intelligence ollama url
OLLAMA_HOST_URL=http://localhost:11434
Expand Down
47 changes: 13 additions & 34 deletions backend/src/langchain/langchain.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,34 @@ import { Module } from "@nestjs/common";
import { ChatOpenAI } from "@langchain/openai";
import { ChatOllama } from "@langchain/ollama";
import { BaseChatModel } from "@langchain/core/language_models/chat_models";

type ModelList = {
[key: string]: string[];
};

const modelList: ModelList = {
ollama: [
"lamma3.1",
"gemma2",
"gemma2:2b",
"phi3",
"mistral",
"neural-chat",
"starling-lm",
"solar",
],
openai: ["gpt-3.5-turbo", "gpt-4o-mini"],
};
import { ConfigModule, ConfigService } from "@nestjs/config";

const chatModelFactory = {
provide: "ChatModel",
useFactory: () => {
const modelType = process.env.YORKIE_INTELLIGENCE;
useFactory: (configService: ConfigService) => {
const modelType = configService.get("YORKIE_INTELLIGENCE");
try {
// Split the modelType string into provider and model
// ex) "ollama:gemma2:2b" => ["ollama", "gemma2:2b"]
const [provider, model] = modelType.split(/:(.+)/);
let chatModel: BaseChatModel | ChatOllama;

if (modelList[provider]?.includes(model)) {
if (provider === "ollama") {
chatModel = new ChatOllama({
model: model,
baseUrl: process.env.OLLAMA_HOST_URL,
checkOrPullModel: true,
streaming: true,
});
} else if (provider === "openai") {
chatModel = new ChatOpenAI({ modelName: model });
}
if (provider === "ollama") {
chatModel = new ChatOllama({
model: model,
baseUrl: process.env.OLLAMA_HOST_URL,
checkOrPullModel: true,
streaming: true,
});
} else if (provider === "openai") {
chatModel = new ChatOpenAI({ modelName: model });
}

if (!chatModel) throw new Error();

return chatModel;
} catch {
throw new Error(`${modelType} is not found. Please check your model name`);
}
},
inject: [ConfigService],
};

@Module({
Expand Down