Skip to content

Commit 9c4860a

Browse files
authored
feat: chatCompletion with mcp example (#27)
1 parent 2645c01 commit 9c4860a

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

examples/basic_usage.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,61 @@ def completion_with_websearch():
5858

5959
print(response)
6060

61+
def completion_with_mcp_server_url():
62+
import os
63+
64+
# Initialize client
65+
client = ZaiClient()
66+
67+
# Create chat completion with MCP server URL
68+
response = client.chat.completions.create(
69+
model='glm-4',
70+
stream=False,
71+
messages=[{'role': 'user', 'content': 'Hello, please introduce GPT?'}],
72+
tools=[
73+
{
74+
'type': 'mcp',
75+
'mcp': {
76+
'server_url': 'https://open.bigmodel.cn/api/mcp/sogou/sse',
77+
'server_label': 'sougou',
78+
'transport_type': 'sse',
79+
'headers': {
80+
'Authorization': f'Bearer {os.getenv("ZAI_API_KEY")}'
81+
}
82+
}
83+
}
84+
],
85+
temperature=0.7,
86+
)
87+
88+
print(response)
89+
90+
def completion_with_mcp_server_label():
91+
import os
92+
93+
# Initialize client
94+
client = ZaiClient()
95+
96+
# Create chat completion with MCP server label
97+
response = client.chat.completions.create(
98+
model='glm-4',
99+
stream=False,
100+
messages=[{'role': 'user', 'content': 'Hello, please introduce GPT?'}],
101+
tools=[
102+
{
103+
'type': 'mcp',
104+
'mcp': {
105+
'server_label': 'sougou_search',
106+
'headers': {
107+
'Authorization': f'Bearer {os.getenv("ZAI_API_KEY")}'
108+
}
109+
}
110+
}
111+
],
112+
temperature=0.7,
113+
)
114+
115+
print(response)
61116

62117
def multi_modal_chat():
63118
import base64

0 commit comments

Comments
 (0)