python后端调用Deep Seek API

python后端调用Deep Seek API

需要依次下载
●Ollama
●Deepseek R1 LLM模型
●嵌入模型nomic-embed-text / bge-m3
●AnythingLLM

参考教程:
Deepseek R1打造本地化RAG知识库:安装部署使用详细教程
手把手教你:deepseek R1基于 AnythingLLM API 调用本地知识库

python调用anythingllm的API

python 复制代码
import requests
# import jsondef
def ask_anythingllm(question, workspace_name, api_key):
    url = f"http://localhost:3001/api/v1/workspace/{workspace_name}/chat"

    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
        "accept": "application/json"
    }
    data = {
        "message": question,
        "mode": "query"  # 可选chat/query模式
    }
    response = requests.post(url, headers=headers, json=data)
    if response.status_code == 200:
        result = response.json()
        # 提取有效回答(去除思考过程)
        answer = result['textResponse'].split('</think>')[-1].strip()
        sources = result.get('sources', [])
        return answer, sources
    else:
        return f"Error: {response.text}", []

# 示例调用
workspace = "" # 填入workspace 名字,注意要全小写
api_key = "" # api_key
question = "你是谁?"
print(question)
answer, sources = ask_anythingllm(question, workspace, api_key)
print("回答:", answer)
print("来源:", [src['title'] for src in sources])

注意workspace_name与anythingllm平台上的显示有所出入,要全部小写,且空格要改成连字符,比如说
MY Workspaces 要写成my-workspaces
DeepSeek 要写成deepseek

结果:

相关推荐
CoderJia程序员甲8 分钟前
GitHub 热榜项目 - 日榜(2026-03-18)
ai·大模型·llm·github·ai教程
前进的李工9 分钟前
LangChain使用之Model IO(提示词模版之PromptTemplate)
开发语言·人工智能·python·langchain
Storynone24 分钟前
【Day27】LeetCode:56. 合并区间,738. 单调递增的数字
python·算法·leetcode
feng一样的男子1 小时前
住在手机里的“小龙虾” (OpenClaw):接入本地模型,解决记忆“装死”顽疾
android·ai·智能手机·openclaw
勇敢牛牛_1 小时前
【aiway】基于 Rust 开发的 API + AI 网关
开发语言·后端·网关·ai·rust
叶子2024221 小时前
承认错误才能成长
python
鲸鱼在dn1 小时前
【CS336】Lecture1课程讲义-语言模型发展历程&Tokenization概念
人工智能·语言模型·自然语言处理
FC皇家慕尼黑1 小时前
Claude Code学习--从搭建Nano Claude Code学习CC机制的底层原理
学习·ai·claude code
console.log('npc')1 小时前
Cursor,Trae,Claude Code如何协作生产出一套前后台app?
前端·人工智能·react.js·设计模式·ai·langchain·ai编程
FC皇家慕尼黑1 小时前
Claude Code学习--从搭建Nano Claude Code学习CC机制的底层原理pt2
学习·ai·claude code