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

结果:

相关推荐
悟空爬虫1 小时前
UV实战教程,我啥要从Anaconda切换到uv来管理包?
python
dev派1 小时前
AI Agent 系统中的常用 Workflow 模式(1)
python·langchain
JohnCHsu2 小时前
性能干翻235B,单卡私有化部署OpenClaw
ai·agent·llama.cpp·openclaw
牧马人win2 小时前
Cursor 四种交互模式
ai·cursor
明月_清风3 小时前
从“能用”到“专业”:构建生产级装饰器与三层逻辑拆解
后端·python
曲幽13 小时前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic
warm3snow17 小时前
AI 重塑产品管理工具:从 Jira 到智能体项目经理的终极演进
人工智能·ai·excel·项目管理·飞书·产品经理·jira·协同·tapd
用户83562907805117 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon19 小时前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly19 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent