Support bard proxy server. (#386)

support bard proxy server for DB-GPT so that you can deploy in your PC
easily.
This commit is contained in:
magic.chen
2023-07-31 20:42:05 +08:00
committed by GitHub
2 changed files with 27 additions and 7 deletions

View File

@@ -128,9 +128,17 @@ PROXY_SERVER_URL={your-openai-proxy-server/v1/chat/completions}
```
### 2. Bard Proxy
- If your environment deploying DB-GPT has access to https://bard.google.com/ (F12-> application-> __Secure-1PSID), then modify the .env configuration file as below will work.
- If your environment deploying DB-GPT has access to <a href="https://bard.google.com/">Bard</a> (F12-> application-> __Secure-1PSID), then modify the .env configuration file as below will work.
```
LLM_MODEL=bard_proxyllm
MODEL_SERVER=127.0.0.1:8000
BARD_PROXY_API_KEY={your-bard-key}
# PROXY_SERVER_URL={your-bard-proxy-server/v1/chat/completions}
```
- If you want to use your own bard proxy server like <a href="https://github.com/eosphoros-ai/Bard-Proxy">Bard-Proxy</a>, so that you can deploy DB-GPT on your PC easily.
```
LLM_MODEL=bard_proxyllm
MODEL_SERVER=127.0.0.1:8000
PROXY_SERVER_URL={your-bard-proxy-server/v1/chat/completions}
```

View File

@@ -1,4 +1,5 @@
import bardapi
import requests
from typing import List
from pilot.configs.config import Config
from pilot.scene.base_message import ModelMessage, ModelMessageRoleType
@@ -7,8 +8,6 @@ CFG = Config()
def bard_generate_stream(model, tokenizer, params, device, context_len=2048):
token = CFG.bard_proxy_api_key
history = []
messages: List[ModelMessage] = params["messages"]
for message in messages:
@@ -35,7 +34,20 @@ def bard_generate_stream(model, tokenizer, params, device, context_len=2048):
for msg in history:
if msg.get("content"):
msgs.append(msg["content"])
response = bardapi.core.Bard(token).get_answer("\n".join(msgs))
if CFG.proxy_server_url is not None:
headers = {"Content-Type": "application/json"}
payloads = {"input": "\n".join(msgs)}
response = requests.post(
CFG.proxy_server_url, headers=headers, json=payloads, stream=False
)
if response.ok:
yield response.text
else:
yield f"bard proxy url request failed!, response = {str(response)}"
else:
response = bardapi.core.Bard(CFG.bard_proxy_api_key).get_answer("\n".join(msgs))
if response is not None and response.get("content") is not None:
yield str(response["content"])
else: