import openai
def _separate_thinking_if_present(text):
tag = "</thinking>"
tag_position = text.find(tag)
if tag_position != -1:
# Split the string into two parts
part1 = text[: tag_position + len(tag)]
part2 = text[tag_position + len(tag):]
return part2, part1
else:
return text, None
def print_response(chat_completion):
print("Response:")
(content, thinking) = _separate_thinking_if_present(
chat_completion.choices[0].message.content)
if thinking:
print(f"Thinking: {thinking}")
if content:
print(f"Content: {content}")
if chat_completion.choices[0].message.tool_calls:
print("Tool calls:")
for tool_call in chat_completion.choices[0].message.tool_calls:
print(
f"name: {tool_call.function.name}, arguments: {tool_call.function.arguments}")
client = openai.OpenAI(
base_url="https://app.empower.dev/api/v1",
api_key="YOUR_API_KEY"
)
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g., San Francisco, CA"
}
},
"required": ["location"]
}
}
}
]
chat_completion = client.chat.completions.create(
model="empower-functions",
messages=[
{"role": "user", "content": "What's the weather in San Francisco and Tokyo?"}
],
tools=tools,
temperature=0,
tool_choice="auto",
extra_body={
"include_thinking": True
}
)
print_response(chat_completion)
print()
chat_completion = client.chat.completions.create(
model="empower-functions",
messages=[
{"role": "user", "content": "Can you help me order a pizza"}
],
tools=tools,
temperature=0,
tool_choice="auto",
extra_body={
"include_thinking": True
}
)
print_response(chat_completion)