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

结果:

相关推荐
apocelipes8 小时前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
用户83562907805110 小时前
使用 Python 在 PDF 中创建与管理书签
后端·python
MeixianAgent15 小时前
Python 回测数据入口怎么验?历史 K 线入库前先做 5 个检查
后端·python
咕白m62518 小时前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python
doiito20 小时前
【Agent Harness】Gliding Horse L2 作战地图深度优化:给多 Agent 上下文装上“精准导航”
ai·rust·架构设计·系统设计·ai agent
SelectDB1 天前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
妙妙屋(zy)1 天前
Claude Code+CC-Switch+CC-Connect+飞书使用教程
ai
小七-七牛开发者2 天前
Coding Agent 规则管理:CLAUDE.md、Skills、Hooks、Subagents 到底怎么选?
ai·大模型·agent·claude·token·loop·mcp·claudecode·ai coding
荣码2 天前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
doiito2 天前
左脚踩右脚:让 LLM 自进化的 Agent 轨迹训练法——为什么它能补上主流范式的最后一块拼图
ai·系统设计