A. Chat模式
# import asyncio
# from dbgpt.core import ModelRequest
# from dbgpt.model.proxy import OllamaLLMClient
# client=OllamaLLMClient()
# print(asyncio.run(client.generate(ModelRequest._build("qwen2:1.5b", "你是谁?"))))
import asyncio
from dbgpt.client import Client
from dbgpt.model.proxy import OllamaLLMClient
DBGPT_API_KEY = "dbgpt"
client = Client(
api_base='http://localhost:5670/api/v2',
api_key=DBGPT_API_KEY)
# client=OllamaLLMClient()
async def chat():
async for data in client.chat_stream(
model="ollama_proxyllm",
messages="请介绍名侦探柯南",
):
print(data.choices[0].delta.content)
# print(data.choices[0])
if __name__ == "__main__":
asyncio.run(chat())
B. Knowledge模式
import requests
import json
mode = 'example'
if mode=='example-1': # ⭐️ 对已存在的Space进行提问
## Question: 流式输出的方法,参考ChatGPT的回答
# DBGPT_API_KEY=dbgpt
# SPACE_NAME=Test-DBGPT
# curl -X POST "http://localhost:5670/api/v2/chat/completions" \
# -H "Authorization: Bearer $DBGPT_API_KEY" \
# -H "accept: application/json" \
# -H "Content-Type: application/json" \
# -d "{\"messages\":\"Hello\",\"model\":\"ollama_proxyllm\", \"chat_mode\": \"chat_knowledge\", \"chat_param\": \"$SPACE_NAME\"}"
DBGPT_API_KEY = "dbgpt"
SPACE_NAME = "Test-DBGPT"
url = "http://localhost:5670/api/v2/chat/completions"
headers = {
"Authorization": f"Bearer {DBGPT_API_KEY}",
"accept": "application/json",
"Content-Type": "application/json"
}
data = {
"messages": "Hello",
"model": "ollama_proxyllm",
"chat_mode": "chat_knowledge",
"chat_param": SPACE_NAME
}
# 打印要发送的 JSON 数据
print("Sending JSON data:", json.dumps(data, indent=4))
response = requests.post(url, headers=headers, json=data)
print(response.status_code) # 输出响应状态码
print("Raw response text:", response.text) # 打印原始响应内容
if mode=='example-2': # ⭐️ 同上:对已存在的Space进行提问
DBGPT_API_KEY = "dbgpt"
SPACE_NAME = "Test-DBGPT"
url = "http://localhost:5670/api/v2/chat/completions"
headers = {
"Authorization": f"Bearer {DBGPT_API_KEY}",
"accept": "application/json",
"Content-Type": "application/json"
}
data = {
"messages": "Hello",
"model": "ollama_proxyllm",
"chat_mode": "chat_knowledge",
"chat_param": SPACE_NAME
}
response = requests.post(url, headers=headers, json=data)
print(response.status_code)
print(response.json()) # 打印响应结果
if mode=='create': # ⭐️ 创建新的Space:CREATED_SPACE_NAME = "test_ollama1"
# DBGPT_API_KEY="dbgpt"
# curl --location --request POST 'http://localhost:5670/api/v2/serve/knowledge/spaces' \
# --header 'Authorization: Bearer $DBGPT_API_KEY' \
# --header 'Content-Type: application/json' \
# --data-raw '{"desc": "for client space desc", "name": "test_space_2", "owner": "dbgpt", "vector_type": "Chroma"
# }'
# Set API Key
DBGPT_API_KEY = "dbgpt"
CREATED_SPACE_NAME = "test_ollama1"
# Define the URL and headers
url = "http://localhost:5670/api/v2/serve/knowledge/spaces"
headers = {
"Authorization": f"Bearer {DBGPT_API_KEY}",
"Content-Type": "application/json"
}
# Define the payload
data = {
"desc": "for client space desc",
"name": CREATED_SPACE_NAME,
"owner": "dbgpt",
"vector_type": "Chroma"
}
# Make the POST request
response = requests.post(url, headers=headers, json=data)
# Print the response
print(response.status_code)
space = response.json()
print(space)
if mode=='update': # ⭐️ 更新指定的Space
# DBGPT_API_KEY="dbgpt"
# curl --location --request PUT 'http://localhost:5670/api/v2/serve/knowledge/spaces' \
# --header 'Authorization: Bearer $DBGPT_API_KEY' \
# --header 'Content-Type: application/json' \
# --data-raw '{"desc": "for client space desc v2", "id": "49", "name": "test_space_2", "owner": "dbgpt", "vector_type": "Chroma"
# }'
# Set API Key
DBGPT_API_KEY = "dbgpt"
Update_SPACE_NAME = "test_space_20"
# Define the URL and headers
url = "http://localhost:5670/api/v2/serve/knowledge/spaces"
headers = {
"Authorization": f"Bearer {DBGPT_API_KEY}",
"Content-Type": "application/json"
}
# Define the payload
data = {
"desc": "for client space desc v2",
"id": "2",
"name": Update_SPACE_NAME,
"owner": "null",
"vector_type": "Chroma"
}
# Make the PUT request
response = requests.put(url, headers=headers, json=data)
# Print the response
print(response.status_code)
print(response.json())
if mode=='delete': # ⭐️ 删除指定的Space,SPACE_ID可通过上面的list获得
# DBGPT_API_KEY=dbgpt
# SPACE_ID={YOUR_SPACE_ID}
# curl -X DELETE "http://localhost:5670/api/v2/serve/knowledge/spaces/$SPACE_ID" \
# -H "Authorization: Bearer $DBGPT_API_KEY" \
# -H "accept: application/json" \
# -H "Content-Type: application/json" \
DBGPT_API_KEY = 'dbgpt'
SPACE_ID = 1 # 这里需要指定Space的ID号码
url = f"http://localhost:5670/api/v2/serve/knowledge/spaces/{SPACE_ID}"
headers = {
"Authorization": f"Bearer {DBGPT_API_KEY}",
"accept": "application/json",
"Content-Type": "application/json"
}
response = requests.delete(url, headers=headers)
# 检查响应状态
if response.status_code == 200:
print("Space deleted successfully.")
else:
print(f"Failed to delete space. Status code: {response.status_code}, Response: {response.text}")
## Delete: 虽然响应报错,但是成功删除的对应的DBGPT中的Space
# 【非PDF-SPACE的删除信息】
# (dbgpt-env) (base) lgk@WIN-20240401VAM:~/Projects/DB-GPT-main$ python test_knowledge.py
# Failed to delete space. Status code: 400, Response: {"success":false,"err_code":"E0003","err_msg":"1 validation errors:\n {'type': 'none_required', 'loc': ('response', 'data'), 'msg': 'Input should be None', 'input': SpaceServeResponse(id=3, name='Test-Delete', vector_type='Chroma', desc='Test delete files', context=None, owner=None, user_id=None, user_ids=None, sys_code=None, domain_type='Normal')}\n","data":null}
# 【PDF-SPACE的删除信息】
# (dbgpt-env) (base) lgk@WIN-20240401VAM:~/Projects/DB-GPT-main$ python test_knowledge.py
# Failed to delete space. Status code: 400, Response: {"success":false,"err_code":"E0003","err_msg":"1 validation errors:\n {'type': 'none_required', 'loc': ('response', 'data'), 'msg': 'Input should be None', 'input': SpaceServeResponse(id=1, name='Test-DBGPT', vector_type='Chroma', desc='for client space desc v2', context='{\"embedding\":{\"topk\":5,\"recall_score\":\"0.1\",\"recall_type\":\"TopK\",\"model\":\"proxy_ollama\",\"chunk_size\":500,\"chunk_overlap\":50},\"prompt\":{\"scene\":\"A chat between a curious user and an artificial intelligence assistant, who very familiar with database related knowledge. \\\\nThe assistant gives helpful, detailed, professional and polite answers to the user\\'s questions. \",\"template\":\" Based on the known information below, provide users with professional and concise answers to their questions.\\\\nconstraints:\\\\n 1.Ensure to include original markdown formatting elements such as images, links, tables, or code blocks without alteration in the response if they are present in the provided information.\\\\n For example, image format should be ![image.png](xxx), link format [xxx](xxx), table format should be represented with |xxx|xxx|xxx|, and code format with xxx.\\\\n 2.If the information available in the knowledge base is insufficient to answer the question, state clearly: \\\\\"The content provided in the knowledge base is not enough to answer this question,\\\\\" and avoid making up answers.\\\\n 3.When responding, it is best to summarize the points in the order of 1, 2, 3, And displayed in markdwon format.\\\\n known information: \\\\n {context}\\\\n question:\\\\n {question},when answering, use the same language as the \\\\\"user\\\\\".\\\\n\",\"max_token\":2000},\"summary\":{\"max_iteration\":5,\"concurrency_limit\":3}}', owner='null', user_id=None, user_ids=None, sys_code=None, domain_type='Normal')}\n","data":null}
if mode=='get': # ⭐️ 获得执行的Space
# DBGPT_API_KEY=dbgpt
# SPACE_ID={YOUR_SPACE_ID}
# curl -X GET "http://localhost:5670/api/v2/serve/knowledge/spaces/$SPACE_ID" -H "Authorization: Bearer $DBGPT_API_KEY"
# Set API Key and Space ID
DBGPT_API_KEY = "dbgpt"
SPACE_ID = "Test-DBGPT" # Replace with your actual space ID
# Define the URL and headers
url = f"http://localhost:5670/api/v2/serve/knowledge/spaces/{SPACE_ID}"
headers = {
"Authorization": f"Bearer {DBGPT_API_KEY}"
}
# Make the GET request
response = requests.get(url, headers=headers)
# Print the response
print(response.status_code)
print(response.json())
if mode=='list': # ⭐️ 查看现有的所有Space
# DBGPT_API_KEY=dbgpt
# curl -X GET 'http://localhost:5670/api/v2/serve/knowledge/spaces' -H "Authorization: Bearer $DBGPT_API_KEY"
# Set API Key
DBGPT_API_KEY = "dbgpt"
# Define the URL and headers
url = "http://localhost:5670/api/v2/serve/knowledge/spaces"
headers = {
"Authorization": f"Bearer {DBGPT_API_KEY}"
}
# Make the GET request
response = requests.get(url, headers=headers)
# Print the response
print(response.status_code)
print(response.json())
C. List Knowledge Space 返回的结果
{
"success": true,
"err_code": null,
"err_msg": null,
"data": {
"items": [
{
"id": 1,
"name": "Test-DBGPT",
"vector_type": "Chroma",
"desc": "for client space desc v2",
"context": {
"embedding": {
"topk": 5,
"recall_score": "0.1",
"recall_type": "TopK",
"model": "proxy_ollama",
"chunk_size": 500,
"chunk_overlap": 50
},
"prompt": {
"scene": "A chat between a curious user and an artificial intelligence assistant, who very familiar with database related knowledge. \nThe assistant gives helpful, detailed, professional and polite answers to the user's questions.",
"template": " Based on the known information below, provide users with professional and concise answers to their questions.\nconstraints:\n 1.Ensure to include original markdown formatting elements such as images, links, tables, or code blocks without alteration in the response if they are present in the provided information.\n For example, image format should be ![image.png](xxx), link format [xxx](xxx), table format should be represented with |xxx|xxx|xxx|, and code format with xxx.\n 2.If the information available in the knowledge base is insufficient to answer the question, state clearly: \"The content provided in the knowledge base is not enough to answer this question,\" and avoid making up answers.\n 3.When responding, it is best to summarize the points in the order of 1, 2, 3, And displayed in markdwon format.\n known information: \n {context}\n question:\n {question},when answering, use the same language as the \"user\".\n",
"max_token": 2000
},
"summary": {
"max_iteration": 5,
"concurrency_limit": 3
}
},
"owner": "null",
"user_id": null,
"user_ids": null,
"sys_code": null,
"domain_type": "Normal"
},
{
"id": 2,
"name": "test_space_20",
"vector_type": "Chroma",
"desc": "for client space desc v2",
"context": null,
"owner": "null",
"user_id": null,
"user_ids": null,
"sys_code": null,
"domain_type": null
},
{
"id": 3,
"name": "Test-Delete",
"vector_type": "Chroma",
"desc": "Test delete files",
"context": null,
"owner": null,
"user_id": null,
"user_ids": null,
"sys_code": null,
"domain_type": "Normal"
}
],
"total_count": 3,
"total_pages": 1,
"page": 1,
"page_size": 20
}
}
D. 参考文献
- Knowledge | DB-GPT
- 工作流API