day3Ollama的本地调用

419 阅读1分钟

参考文章

本地电脑使用ollama部署大模型并安装Chatbox:一文看通透某些场景下可能希望构建一个完全本地离线可用的大模型,方 - 掘金

使用Ollama本地离线体验SimpleRAG(手把手教程)Ollama介绍 Ollama是一个开源项目,专注于开发和部 - 掘金

第一步下载ollama

www.ollama.com/download

然后到ollama的指令那里,找想要使用的对应模型

www.ollama.com/library

然后cmd进入命令行输入指令

小心每天别输入不一样的指令,不然会重新下载

Error: max retries exceeded: unexpected EOF

怎么还报错了

然后是部署

可以docker部署

用ollama官方的 docs.openwebui.com

也可以直接下载一个exe**chatboxai.app/zh**

Ollama的地址,默认是http://localhost:11434,Ollama不需要Api Key随便写

我好像一个小丑,它通过本地调用的方式是直接本地接口,而不是API

让他写一个通过接口调用的代码就行

import requests

class SimpleChatLLM:
    def __init__(self, key, endpoint, model):
        self.key = key
        self.endpoint = endpoint
        self.model = model

    def chat(self, prompt):
        headers = {
            "Authorization": f"Bearer {self.key}",
            "Content-Type": "application/json"
        }
        data = {
            "model": self.model,
            "prompt": prompt,
            "max_tokens": 50  # 限制回答的 token 长度
        }
        
        response = requests.post(f"{self.endpoint}/v1/completions", headers=headers, json=data)
        
        if response.status_code == 200:
            return response.json().get("choices", [])[0].get("text", "").strip()
        else:
            print("Error:", response.status_code, response.text)
            return None

# 配置参数
key = "sk-..."  # 替换为你的实际 API 密钥
endpoint = "https://api.siliconflow.cn"
model = "Qwen/Qwen2-7B-Instruct"

# 创建聊天实例
chat_llm = SimpleChatLLM(key, endpoint, model)

# 测试简单对话
response = chat_llm.chat("how are you?")
print("AI Response:", response)

问答的时候记得用英文,因为ollama没有中文能力

明天去研究研究怎么进行微调,以及调用自己的微调大模型

或者学一下docker部署