邯郸网站建设联系电话网站建造免费
1、使用Ollama安装启动deepseek-r1:7b模型
2、Ollama启动访问地址:http://localhost:11434
3、python程序调用本地模型,实现简单的聊天机器人
实现代码如下app.py:
import chainlit as cl
import requests
from abc import ABC, abstractmethod
from typing import Any, Sequence# 假设以下类型已经从 llama_index.core.base.llms.types 导入
ChatMessage = dict
ChatResponse = str
CompletionResponse = strclass BaseLLM(ABC):@abstractmethoddef chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:pass@abstractmethoddef complete(self, prompt: str, **kwargs: Any) -> CompletionResponse:passclass DeepSeekLLM(BaseLLM):def __init__(self, model="deepseek-r1:7b", api_base="http://localhost:11434/v1", temperature=0.7):self.model = modelself.api_base = api_baseself.temperature = temperaturedef chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:url = f"{self.api_base}/v1/chat/completions"data = {"model": self.model,"messages": messages,"temperature": self.temperature,"stream": False}response = requests.post(url, json=data)response.raise_for_status()response_data = response.json()return response_data["choices"][0]["message"]["content"]def complete(self, prompt: str, **kwargs: Any) -> CompletionResponse:url = f"{self.api_base}/chat/completions"data = {"model": self.model,"messages": [{"role": "user", "content": prompt}],"temperature": self.temperature,"stream": False}response = requests.post(url, json=data)response.raise_for_status()response_data = response.json()return response_data["choices"][0]["message"]["content"]@cl.on_chat_start
async def start():await cl.Message(author="Assistant", content="你好!我是你的聊天助手。").send()@cl.on_message
async def handle_message(message: cl.Message):llm = DeepSeekLLM()response = llm.complete(message.content)await cl.Message(author="Assistant", content=response).send()if __name__ == "__main__":cl.run()
注意:①安装chainlit前端框架插件:pip install chainlit
②页面启动命令:chainlit run app.py -w
③访问页面:http://localhost:8000/