【AnythingLLM】安装、使用API调用

一、docker安装

- docker安装和exe安装的区别?
在这里插入图片描述

docker pull mintplexlabs/anythingllm
docker images

windows:

$env:STORAGE_LOCATION="$HOME\Documents\anythingllm"; `
If(!(Test-Path $env:STORAGE_LOCATION)) {New-Item $env:STORAGE_LOCATION -ItemType Directory}; `
If(!(Test-Path "$env:STORAGE_LOCATION\.env")) {New-Item "$env:STORAGE_LOCATION\.env" -ItemType File}; `
docker run -d -p 3001:3001 `
--cap-add SYS_ADMIN `
-v "$env:STORAGE_LOCATION`:/app/server/storage" `
-v "$env:STORAGE_LOCATION\.env:/app/server/.env" `
-e STORAGE_DIR="/app/server/storage" `
mintplexlabs/anythingllm;

参考:https://docs.anythingllm.com/installation-docker/local-docker

二、配置AnythingLLM

在这里插入图片描述
在这里插入图片描述

三、使用API调用AnythingLLM

参考:https://blog.csdn.net/wlddhj/article/details/145713875

1. 生成API密钥

在这里插入图片描述

2. “Authorize”API密钥

复制API秘钥,点击“阅读API文档”打开API接口文档,点击右边的“Authorize”按钮,把API秘钥复制进去,然后就可以调试接口了。
在这里插入图片描述

在这里插入图片描述
运行第一个接口/v1/auth,如果返回截图中的结果,即调用成功。在这里插入图片描述
查看工作区信息:
在这里插入图片描述

3. python调用

import requests

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": "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()  
        answer = result  
        sources = result.get('sources', [])        
        return answer, sources    
    else:        
        return f"Error: {response.text}", []
    
# 示例调用
api_key = "Your-Api-Key"
slug = "Your-Slug"
question = "你是谁?"
answer, sources = ask_anythingllm(question, slug, api_key)
print("回答:", answer)
print("来源:", [src['title'] for src in sources])
### 如何调用硅基流动 API #### 调用准备 为了顺利调用硅基流动API,需确保已获取必要的访问凭证和配置好开发环境。通常这涉及到注册账号并获得API密钥等认证信息[^1]。 #### Python 示例代码 下面提供了一个Python脚本作为调用硅基流动平台上DeepSeek文本生成接口的例子: ```python import requests import json def call_silicon_flow_api(prompt, api_key): url = "https://api.siliconflow.com/v1/generate_text" headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {api_key}' } data = { 'prompt': prompt, 'max_tokens': 150 } response = requests.post(url=url, headers=headers, data=json.dumps(data)) if response.status_code == 200: result = response.json() return result['text'] elif response.status_code == 429 or response.status_code == 503: error_message = f"Error occurred while calling the API: HTTP Status Code {response.status_code}" print(error_message) return None else: raise Exception(f"Unexpected status code received from server: {response.status_code}") # Example usage of function with a given API key and input text. if __name__ == "__main__": user_input = "请描述一下未来城市的样子." my_api_key = "<Your_API_Key_Here>" generated_text = call_silicon_flow_api(user_input, my_api_key) if generated_text is not None: print(generated_text) ``` 此段程序展示了怎样构建HTTP POST请求发送给硅基流动API,并处理返回的数据以及可能发生的错误状况,比如限流(Too Many Requests)[^5] 或者服务不可用(Service Unavailable)等问题。 #### 错误处理机制 当遇到诸如`429 Too Many Requests`或`503 Service Unavailable`这类HTTP响应码时,应该采取适当措施减少请求频次或是考虑升级至更高级别的服务计划;另外也可以尝试查询官方提供的模型状态页面来确认当前的服务可用性情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值