client_openai_chat.py 3.8 KB
Newer Older
chenzk's avatar
v1.0  
chenzk committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# /// script
# dependencies = [
#   "openai",
# ]
# [tool.uv]
# exclude-newer = "2025-03-07T00:00:00Z"
# ///
"""Chat With Your DB-GPT's API by OpenAI Client

Sample Usage:
```bash
uv run examples/client/client_openai_chat.py -m Qwen/QwQ-32B --input "Hello"
```

More examples:

1. Chat Normal Mode:
```bash
uv run examples/client/client_openai_chat.py -m Qwen/QwQ-32B \
    --input "Which is bigger, 9.8 or 9.11?"
```

2. Chat Database Mode(chat_with_db_qa):

```bash
uv run examples/client/client_openai_chat.py -m Qwen/QwQ-32B \
    --chat-mode chat_with_db_qa \
    --param "sqlite_dbgpt" \
    --input "Which table stores database connection information?"
```

3. Chat With Your Data(chat_data):
```bash
uv run examples/client/client_openai_chat.py -m Qwen/QwQ-32B \
    --chat-mode chat_data \
    --param "sqlite_dbgpt" \
    --input "Which database can I currently connect to? What is its name and type?"
```

4. Chat With Knowledge(chat_knowledge):
```bash
uv run examples/client/client_openai_chat.py -m Qwen/QwQ-32B \
    --chat-mode chat_knowledge \
    --param "awel" \
    --input "What is AWEL?"
```   


5. Chat With Third-party API(chat_third_party):
```bash
uv run examples/client/client_openai_chat.py -m deepseek-chat \
    --input "Which is bigger, 9.8 or 9.11?" \
    --chat-mode none \
    --api-key $DEEPSEEK_API_KEY \
    --api-base https://api.deepseek.com/v1
```

"""  # noqa

import argparse

from openai import OpenAI

DBGPT_API_KEY = "dbgpt"


def handle_output(response):
    has_thinking = False
    print("=" * 80)
    reasoning_content = ""
    for chunk in response:
        delta_content = chunk.choices[0].delta.content
        if hasattr(chunk.choices[0].delta, "reasoning_content"):
            reasoning_content = chunk.choices[0].delta.reasoning_content
        if reasoning_content:
            if not has_thinking:
                print("<thinking>", flush=True)
            print(reasoning_content, end="", flush=True)
            has_thinking = True
        if delta_content:
            if has_thinking:
                print("</thinking>", flush=True)
            print(delta_content, end="", flush=True)
            has_thinking = False


def main():
    parser = argparse.ArgumentParser(description="OpenAI Chat Client")
    parser.add_argument(
        "-m",
        "--model",
        type=str,
        default="deepseek-chat",
        help="Model name",
    )
    parser.add_argument(
        "-c",
        "--chat-mode",
        type=str,
        default="chat_normal",
        help="Chat mode. Default is chat_normal",
    )
    parser.add_argument(
        "-p",
        "--param",
        type=str,
        default=None,
        help="Chat param",
    )
    parser.add_argument(
        "--input",
        type=str,
        default="Hello, how are you?",
        help="User input",
    )
    parser.add_argument(
        "--api-key",
        type=str,
        default=DBGPT_API_KEY,
        help="API key",
    )
    parser.add_argument(
        "--api-base",
        type=str,
        default="http://localhost:5670/api/v2",
        help="Base URL",
    )
    parser.add_argument(
        "--max-tokens",
        type=int,
        default=4096,
        help="Max tokens",
    )
    args = parser.parse_args()

    client = OpenAI(
        api_key=args.api_key,
        base_url=args.api_base,
    )

    messages = [
        {
            "role": "user",
            "content": args.input,
        },
    ]

    extra_body = {}
    if args.chat_mode != "none":
        extra_body["chat_mode"] = args.chat_mode
    if args.param:
        extra_body["chat_param"] = args.param

    response = client.chat.completions.create(
        model=args.model,
        messages=messages,
        extra_body=extra_body,
        stream=True,
        max_tokens=args.max_tokens,
    )
    handle_output(response)


if __name__ == "__main__":
    main()