Deepseek AnythingLLM本地部署的API调用实现详解(Python版)

结合AnythingLLM官方api接口介绍:其中slug需要在workspace工作区查找本地新建的工作区在json文件中可以查找到对应的slug。

python:代码调用

import requests
import json
def ask_anythingllm(question, slug, api_key):    
    url = f"http://localhost:3001/api/v1/workspace/{slug}/chat"    
    headers = {        
        "Authorization": f"Bearer {api_key}",        
        "Content-Type": "application/json",        
        "accept": "application/json"    
    }    
    data = {        
        "message": question,        
        "mode": "chat"  # 可选chat/query模式,其中query为查询对话,默认chat聊天  
    }    
    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, result    
    else:        
        return f"Error: {response.text}", []
# 示例调用
api_key = "YOU_APIKEY"  #替换成你自己的apikey
slug = "YOU_slug" #替换成你自己的slug 
question = "请介绍你自己的版本和能做的事"
answer, sources, result     = ask_anythingllm(question, slug, api_key)
print("回答:", answer)
print("来源:", [src['title'] for src in sources])
print("JSON所有信息",result)

注意:此方法非流式回答,需等待ai说完后才可以显示信息

作者:心态69

物联沃分享整理
物联沃-IOTWORD物联网 » Deepseek AnythingLLM本地部署的API调用实现详解(Python版)

发表回复