File size: 1,895 Bytes
b3cb0b5 | 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 | import os
from dotenv import load_dotenv
from openai import AsyncOpenAI
import asyncio
import json
load_dotenv()
async def main():
api_key = os.environ.get("GROQ_API_KEY")
client = AsyncOpenAI(
api_key=api_key,
base_url="https://api.groq.com/openai/v1"
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
},
"required": ["location"],
},
},
}
]
print("Testing Tool Call with openai/gpt-oss-20b...")
try:
response = await client.chat.completions.create(
model="openai/gpt-oss-20b",
messages=[
{"role": "user", "content": "What's the weather in San Francisco?"}
],
tools=tools,
tool_choice="auto"
)
message = response.choices[0].message
print(f"Initial Response Role: {message.role}")
if message.tool_calls:
print(f"Tool Calls: {len(message.tool_calls)}")
for tc in message.tool_calls:
print(f" - Function: {tc.function.name}")
print(f" - Args: {tc.function.arguments}")
else:
print("No tool calls triggered.")
except Exception as e:
print(f"Failed during tool call test: {e}")
if __name__ == "__main__":
asyncio.run(main())
|