Skip to content

Commit 2978e70

Browse files
author
潘婉宁
committed
fix: support fc
1 parent bf08f11 commit 2978e70

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import asyncio
2+
import volcenginesdkarkruntime as ark
3+
from pydantic import BaseModel
4+
import rich
5+
from volcenginesdkarkruntime import AsyncArk
6+
7+
# Authentication
8+
# 1.If you authorize your endpoint using an API key, you can set your api key to environment variable "ARK_API_KEY"
9+
# or specify api key by Ark(api_key="${YOUR_API_KEY}").
10+
# Note: If you use an API key, this API key will not be refreshed.
11+
# To prevent the API from expiring and failing after some time, choose an API key with no expiration date.
12+
13+
# 2.If you authorize your endpoint with Volcengine Identity and Access Management(IAM), set your api key to environment variable "VOLC_ACCESSKEY", "VOLC_SECRETKEY"
14+
# or specify ak&sk by Ark(ak="${YOUR_AK}", sk="${YOUR_SK}").
15+
# To get your ak&sk, please refer to this document(https://www.volcengine.com/docs/6291/65568)
16+
# For more information,please check this document(https://www.volcengine.com/docs/82379/1263279)
17+
18+
client = AsyncArk()
19+
20+
21+
class GetWeather(BaseModel):
22+
city: str
23+
country: str
24+
25+
26+
async def main():
27+
# Non-stream parsing
28+
print("----- standard request -----")
29+
completion = await client.beta.chat.completions.parse(
30+
model="${YOUR_ENDPOINT_ID}",
31+
messages=[
32+
{"role": "user", "content": "What's the weather like in hangzhou?"}
33+
],
34+
tools=[
35+
ark.pydantic_function_tool(GetWeather),
36+
],
37+
)
38+
39+
tool_call = (completion.choices[0].message.tool_calls or [])[0]
40+
rich.print(tool_call.function)
41+
assert isinstance(tool_call.function.parsed_arguments, GetWeather)
42+
print(tool_call.function.parsed_arguments.city)
43+
print("----\n")
44+
45+
# stream parsing
46+
print("----- streaming request -----")
47+
async with client.beta.chat.completions.stream(
48+
model="${YOUR_ENDPOINT_ID}",
49+
messages=[
50+
{"role": "user", "content": "What's the weather like in hangzhou?"}
51+
],
52+
tools=[
53+
ark.pydantic_function_tool(GetWeather, name="get_weather"),
54+
],
55+
) as stream:
56+
async for event in stream:
57+
if event.type == "tool_calls.function.arguments.delta" or event.type == "tool_calls.function.arguments.done":
58+
rich.get_console().print(event, width=80)
59+
print("----\n")
60+
rich.print(stream.get_final_completion())
61+
62+
63+
if __name__ == "__main__":
64+
asyncio.run(main())

0 commit comments

Comments
 (0)