Skip to content

Commit 1d11f38

Browse files
committed
v0.2.1 files refactored
1 parent 0d1fc36 commit 1d11f38

File tree

10 files changed

+197
-189
lines changed

10 files changed

+197
-189
lines changed

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
ignore = E203
3+
max-line-length = 100

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ ENV/
128128
env.bak/
129129
venv.bak/
130130

131+
.idea/
132+
131133
# Spyder project settings
132134
.spyderproject
133135
.spyproject

.idea/fastApiProject.iml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ repos:
3030
hooks:
3131
- id: isort
3232
args: [ "--profile", "black" ]
33-
# - repo: https://github.com/psf/black
34-
# rev: 22.3.0
35-
# hooks:
36-
# - id: black
37-
# args: [ --line-length=88 ]
33+
- repo: https://github.com/psf/black
34+
rev: 22.3.0
35+
hooks:
36+
- id: black
37+
args: [ --line-length=100 ]

src/chartsInfo.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def draw_charts(sql_result: dict) -> None:
2+
"""
3+
画图
4+
"""
5+
pass

src/getStreamInfo.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import json
2+
import os
3+
from typing import AsyncGenerator, List
4+
5+
from dotenv import load_dotenv
6+
from httpx import AsyncClient
7+
8+
load_dotenv()
9+
api_key = os.getenv("API_KEY")
10+
api_url = os.getenv("API_URL")
11+
12+
13+
GPT_MODEL = "gpt-4"
14+
GPT_TEMPERATURE = 0.3
15+
16+
17+
async def request(val: List[dict[str, str]]) -> AsyncGenerator[dict, None]:
18+
print("request")
19+
"""
20+
请求API
21+
"""
22+
url = api_url
23+
headers = {
24+
"Content-Type": "application/json",
25+
"Authorization": "Bearer " + api_key,
26+
}
27+
params = {
28+
"model": GPT_MODEL,
29+
"messages": val,
30+
"max_tokens": 3000,
31+
"temperature": GPT_TEMPERATURE,
32+
"top_p": 1,
33+
"n": 1,
34+
"stream": True,
35+
}
36+
# if call_function:
37+
# params["function_call"] = "auto"
38+
# params["function"] = [{"name": "chat_stream",
39+
# "description": "Generate the sql using
40+
# the given question",
41+
# "parameters": {
42+
# "type": "object",
43+
# "properties": {
44+
# "question": {
45+
# "type": "string",
46+
# "description":
47+
# "The paraphrased question"
48+
# },
49+
# },
50+
# "required": ["question"]
51+
# },
52+
# },
53+
# ]
54+
async with AsyncClient() as client:
55+
async with client.stream("POST", url, headers=headers, json=params, timeout=60) as response:
56+
async for line in response.aiter_lines():
57+
if not line.strip():
58+
continue
59+
line = line.replace("data: ", "")
60+
try:
61+
data = json.loads(line)
62+
except Exception:
63+
# finish_reason maybe function_call, but it doesn't matter
64+
data = {"choices": [{"finish_reason": "stop"}]}
65+
if data.get("choices")[0].get("finish_reason") is not None:
66+
return
67+
delta = data.get("choices")[0].get("delta")
68+
# 下面就不用get delta字段了
69+
if delta is None: # 跳过空值
70+
continue
71+
yield delta
72+
# pprint.pprint(response.json())

0 commit comments

Comments
 (0)