-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_call.py
More file actions
99 lines (76 loc) · 2.89 KB
/
Copy pathsingle_call.py
File metadata and controls
99 lines (76 loc) · 2.89 KB
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
import os
import asyncio
from datetime import datetime
from qreward.client import OpenAIChatProxy, OpenAIChatProxyManager
_messages = [{"role": "user", "content": "Hello, how are you?"}]
async def simple_call():
start_time = datetime.now()
proxy = OpenAIChatProxy(
base_url=os.getenv("OPENAI_API_BASE"),
api_key=os.getenv("OPENAI_API_KEY"),
)
call_result = await proxy.chat_completion(
messages=_messages,
model="DeepSeek-R1",
)
print(
f"{(datetime.now() - start_time).seconds} secs, " f"task result: {call_result}"
)
async def simple_call_by_context():
start_time = datetime.now()
async with OpenAIChatProxy(
base_url=os.getenv("OPENAI_API_BASE"),
api_key=os.getenv("OPENAI_API_KEY"),
) as proxy:
call_result = await proxy.chat_completion(
messages=_messages,
model="DeepSeek-R1",
)
print(
f"{(datetime.now() - start_time).seconds} secs, " f"task result: {call_result}"
)
async def call_single_with_proxy_manager():
start_time = datetime.now()
proxy_manager = OpenAIChatProxyManager()
proxy_manager.add_proxy(
"default",
OpenAIChatProxy(
base_url=os.getenv("OPENAI_API_BASE"),
api_key=os.getenv("OPENAI_API_KEY"),
),
)
# you can add more proxy with different base_url and api_key
# single call
call_result = await proxy_manager.proxy("default").chat_completion(
messages=_messages,
model="DeepSeek-R1",
)
print(
f"{(datetime.now() - start_time).seconds} secs, " f"task result: {call_result}"
)
async def call_single_embedding_with_custom_path_and_input():
sentences = ["你好", "再见"]
def update_path(request) -> None:
# CH: 默认 OpenAI 是 /embeddings 路径,这里重写为 /embed
# EN: default OpenAI is /embeddings path, here rewrite to /embed
if request.url.path in ["/embeddings"]:
request.url = request.url.copy_with(path="/embed")
async with OpenAIChatProxy(
base_url="http://custom_url:8000",
api_key=os.getenv("OPENAI_API_KEY"),
httpx_request_hook=update_path,
) as proxy:
embedding_result = await proxy.embeddings(
model="embedding-model",
sentences=[],
extra_body={"sentences": sentences},
# CH: 上面这么写是因为 OpenAI 默认 key 是 input 而不是 sentences
# EN: above is because OpenAI default key is input instead of sentences
# CH: 如果用户自己自定义了 key,那么需要使用 extra_body 参数传递
# EN: if user custom key, need to pass through extra_body parameter
)
print(embedding_result)
if __name__ == "__main__":
asyncio.run(simple_call())
asyncio.run(simple_call_by_context())
asyncio.run(call_single_with_proxy_manager())