本帖最后由 江小举 于 2025-4-17 17:34 编辑
一、如何获取鉴权的token?1. 首先登录aipaas平台 2. 进入系统设置/API Key这里,点击新建,创建一个apikey。这个apikey将用于后续调用api接口时做认证鉴权使用的。
二、如何进行接口调用,下面以python代码调用应用对话接口为例,用其他语言或者客户端工具调用的话,方法类似。 1. 首先根据第一步获取到一个apikey,在请求的时候,需要带上apikey,在请求头部分设置的Authorization字段添加上,把{apikey}替换成实际的apikey。(注意:Bearer后面有个空格) - headers = {
- "Content-Type": "application/json",
- "Authorization": "Bearer {apikey}",
- }
复制代码
2. 根据openapi文档,填上接口所需要的参数,最后发送请求进行调用即可。 - import json
- import sys
- import requests
- def main():
- url = "http://127.0.0.1:8000/api/v1/conversation"
- headers = {
- "Content-Type": "application/json",
- "Authorization": "Bearer sk-xxxx",
- }
- data = {
- "app_id": "959c291f-d0fb-4b1f-83c8-da1decb6fa6a",
- "stream": True,
- "query": "Why is the Sky Blue?",
- }
- with requests.post(url, headers=headers, json=data, stream=True) as response:
- if response.status_code != 200:
- print(f"请求失败,状态码: {response.status_code}")
- return
- for line in response.iter_lines():
- if line:
- try:
- line = line.decode("utf-8")
- if line.startswith("data:"):
- json_data = json.loads(line[6:])
- sys.stdout.write(json_data.get("answer", ""))
- sys.stdout.flush()
- except json.JSONDecodeError:
- print(f"无法解析的JSON数据: {line}")
- except Exception as e:
- print(f"处理SSE流时出错: {str(e)}")
- if __name__ == "__main__":
- main()
复制代码 |