Skip to content

Commit

Permalink
Merge pull request #19 from lingmengcan/dev
Browse files Browse the repository at this point in the history
support ollama locally llm
  • Loading branch information
lingmengcan authored Sep 25, 2024
2 parents 15d6fda + ccb0692 commit 28d74ef
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
7 changes: 6 additions & 1 deletion doc/lingmengcan-ai-202409241125.sql
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ INSERT INTO `dict` VALUES
(102,'CONTROL_NET_TYPE','SoftEdge','SoftEdge (软边缘)',17,0,'SoftEdge (软边缘)','admin','admin','2024-08-12 17:25:47','2024-08-12 17:25:47'),
(103,'CONTROL_NET_TYPE','SparseCtrl','SparseCtrl (稀疏控制)',18,0,'SparseCtrl (稀疏控制)','admin','admin','2024-08-12 17:26:30','2024-08-12 17:26:30'),
(104,'CONTROL_NET_TYPE','T2I-Adapter','T2I-Adapter',19,0,'T2I-Adapter','admin','admin','2024-08-12 17:27:08','2024-08-12 17:27:23'),
(105,'CONTROL_NET_TYPE','Tile','Tile (分块)',20,0,'Tile (分块)','admin','admin','2024-08-12 17:27:09','2024-08-12 17:27:09');
(105,'CONTROL_NET_TYPE','Tile','Tile (分块)',20,0,'Tile (分块)','admin','admin','2024-08-12 17:27:09','2024-08-12 17:27:09'),
(106,'0_ROOT_TYPE','LLM_API_TYPE','大语言模型访问类型',9,0,'大语言模型访问类型','admin','admin','2024-09-24 16:53:33','2024-09-24 16:53:40'),
(107,'LLM_API_TYPE','LLM_API_OPENAI','OpenAI',1,0,'openai','admin','admin','2024-09-24 16:54:46','2024-09-24 16:55:53'),
(108,'LLM_API_TYPE','LLM_API_OLLAMA','Ollama',2,0,'Ollama','admin','admin','2024-09-24 16:55:13','2024-09-24 16:55:13');
/*!40000 ALTER TABLE `dict` ENABLE KEYS */;
UNLOCK TABLES;

Expand Down Expand Up @@ -305,6 +308,7 @@ CREATE TABLE `llm` (
`model_type_name` varchar(32) COLLATE utf8mb4_bin NOT NULL,
`base_url` varchar(512) COLLATE utf8mb4_bin NOT NULL,
`api_key` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`api_type` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`default_embedding_model` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`status` tinyint(1) NOT NULL,
`description` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
Expand All @@ -324,6 +328,7 @@ LOCK TABLES `llm` WRITE;
/*!40000 ALTER TABLE `llm` DISABLE KEYS */;
INSERT INTO `llm` VALUES
('3ada5390-cd0a-4dc5-89f4-a25aba1355a9','hunyuan-13B','GENERAL_LLM','通用模型','http://hunyuanapi.tencent.com/openapi/v1','','',0,'腾讯公司开发的大型语言模型混元大模型(HunYuan)。主要功能是通过丰富的语义理解和计算能力,为用户提供问答式的服务。','admin','admin','2024-06-25 10:49:53','2024-06-25 14:36:06'),
('51770c03-75dc-4f72-84f8-73e7a1a96739','qwen2.5','GENERAL_LLM','通用模型','','','LLM_API_OLLAMA','bge-large-zh-v1.5',0,'qwen2.5','admin','admin','2024-09-24 16:33:29','2024-09-25 15:46:53'),
('601278f2-8bf1-4eff-a684-a7eff61d1274','text-embedding-3-small','EMBEDDING_LLM','Embedding','','','',0,'openai 嵌入式模型','admin','admin','2024-06-11 19:34:12','2024-06-24 20:16:39'),
('78e33981-4e01-445d-89e1-bfacc0affb0d','ChatGLM3-6B','GENERAL_LLM','通用模型','http://127.0.0.1:8000/v1/','','bge-large-zh-v1.5',0,'ChatGLM3 是智谱AI和清华大学 KEG 实验室联合发布的对话预训练模型','admin','admin','2024-06-11 16:47:27','2024-06-24 20:16:31'),
('86409bd7-666d-4ddb-9809-7c52ef3dc56a','gpt-4','GENERAL_LLM','通用模型','https://oai.hconeai.com/v1','','',0,'GPT-4(Generative Pretrained Transformer 4)是一种自然语言处理(NLP)AI模型。','admin','admin','2024-06-07 17:13:21','2024-06-24 20:16:16'),
Expand Down
3 changes: 3 additions & 0 deletions service/src/entities/llm.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export class Llm {
@Column('varchar', { name: 'api_key', length: 128 })
apiKey: string;

@Column('varchar', { name: 'api_type', length: 32 })
apiType: string;

@Column('varchar', { name: 'default_embedding_model', length: 128 })
defaultEmbeddingModel: string;

Expand Down
50 changes: 29 additions & 21 deletions service/src/services/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import { AIMessage, HumanMessage } from '@langchain/core/messages';
import { VectorStore } from '@langchain/core/vectorstores';
import { Chroma } from '@langchain/community/vectorstores/chroma';
import { LlmService } from './llm.service';
import { Ollama } from '@langchain/ollama';
import { Llm } from '@/entities/llm.entity';
import { ChatPromptValueInterface } from '@langchain/core/dist/prompt_values';
import { RunnableLike } from '@langchain/core/runnables';

@Injectable()
export class ChatService {
Expand Down Expand Up @@ -82,30 +86,32 @@ export class ChatService {
},
);

return this.chatfileOpenAi(
message.messageText,
temperature,
messageHistory,
model.baseUrl,
model.apiKey,
vectorStore,
);
return this.chatfileOpenAi(message.messageText, temperature, messageHistory, model, vectorStore);
} else {
return this.chatOpenAi(message.messageText, temperature, messageHistory, model.baseUrl, model.apiKey);
return this.chatOpenAi(message.messageText, temperature, messageHistory, model);
}
}

//自由对话
async chatOpenAi(
message: string,
temperature: number,
messageHistory: ChatMessageHistory,
basePath: string,
openAIApiKey: string,
) {
async chatOpenAi(message: string, temperature: number, messageHistory: ChatMessageHistory, model: Llm) {
//根据内容回答问题
// Instantiate your model and prompt.
const llm = new ChatOpenAI({ openAIApiKey, temperature, streaming: true }, { basePath });

// 工厂函数,创建模型实例
function createModelInstance(model: Llm, temperature: number): RunnableLike<ChatPromptValueInterface, unknown> {
if (model.apiType === 'LLM_API_OLLAMA') {
return new Ollama({
model: model.modelName,
temperature,
});
} else {
return new ChatOpenAI(
{ openAIApiKey: model.apiKey, temperature, streaming: true },
{ basePath: model.baseUrl },
);
}
}

const llm = createModelInstance(model, temperature);

const prompt = ChatPromptTemplate.fromMessages([
new MessagesPlaceholder('history'),
Expand All @@ -129,15 +135,17 @@ export class ChatService {
message: string,
temperature: number,
messageHistory: ChatMessageHistory,
basePath: string,
openAIApiKey: string,
model: Llm,
vectorStore: VectorStore,
) {
const result = await vectorStore.similaritySearch(message, 1);

//根据内容回答问题
// Instantiate your model and prompt.
const llm = new ChatOpenAI({ openAIApiKey, temperature, streaming: true }, { basePath });
const llm = new ChatOpenAI(
{ openAIApiKey: model.apiKey, temperature, streaming: true },
{ basePath: model.baseUrl },
);
const prompt = ChatPromptTemplate.fromMessages([
SystemMessagePromptTemplate.fromTemplate(
`基于已知内容, 回答用户问题。如果无法从中得到答案,请说'没有足够的相关信息'已知内容:${result[0].pageContent}`,
Expand Down
2 changes: 2 additions & 0 deletions service/src/services/llm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export class LlmService {
entity.modelType = model.modelType;
entity.baseUrl = model.baseUrl;
entity.apiKey = model.apiKey;
entity.apiType = model.apiType;
entity.defaultEmbeddingModel = model.defaultEmbeddingModel;
entity.status = model.status;
entity.description = model.description;
Expand All @@ -161,6 +162,7 @@ export class LlmService {
entity.modelType = model.modelType;
entity.baseUrl = model.baseUrl;
entity.apiKey = model.apiKey;
entity.apiType = model.apiType;
entity.defaultEmbeddingModel = model.defaultEmbeddingModel;
entity.status = model.status;
entity.description = model.description ?? '';
Expand Down
1 change: 1 addition & 0 deletions web/src/models/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Llm {
modelTypeName?: string;
baseUrl?: string;
apiKey?: string;
apiType?: string;
defaultEmbeddingModel?: string;
status: string;
description?: string;
Expand Down
4 changes: 4 additions & 0 deletions web/src/views/llm/model/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
dict-type="LLM_TYPE"
/>
</n-form-item>
<n-form-item label="访问类型OpenAI/Ollama" name="apiType">
<selectDict v-model:dict-code="drawerFormData.apiType" dict-type="LLM_API_TYPE" />
</n-form-item>
<n-form-item label="base url" name="baseUrl">
<n-input v-model:value="drawerFormData.baseUrl" placeholder="请输入模型api url" />
</n-form-item>
Expand Down Expand Up @@ -142,6 +145,7 @@
modelTypeName: '',
baseUrl: '',
apiKey: '',
apiType: undefined,
defaultEmbeddingModel: undefined,
status: '0',
description: '',
Expand Down

0 comments on commit 28d74ef

Please sign in to comment.