-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathanthropic_api_function_calling.py
More file actions
70 lines (62 loc) · 1.8 KB
/
Copy pathanthropic_api_function_calling.py
File metadata and controls
70 lines (62 loc) · 1.8 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
import os
from dotenv import load_dotenv
from highflame import Highflame, Config
load_dotenv()
# Config setup
config = Config(
base_url=os.getenv("HIGHFLAME_BASE_URL") or os.getenv("JAVELIN_BASE_URL"),
api_key=os.getenv("HIGHFLAME_API_KEY") or os.getenv("JAVELIN_API_KEY"),
llm_api_key=os.getenv("ANTHROPIC_API_KEY"),
timeout=120,
)
client = Highflame(config)
# Headers
headers = {
"Content-Type": "application/json",
"x-javelin-route": "anthropic_univ", # add your universal route
"x-javelin-model": "claude-3-5-sonnet-20240620", # add any supported model
"x-javelin-provider": "https://api.anthropic.com/v1",
"x-api-key": os.getenv("ANTHROPIC_API_KEY"),
"anthropic-version": "2023-06-01",
}
client.set_headers(headers)
# Tool definition — using `input_schema` instead of OpenAI's `parameters`
functions = [
{
"name": "get_weather",
"description": "Get the current weather in a city",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
]
# Messages
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "What's the weather like in Mumbai in celsius?"}
],
}
]
# Request payload
query_body = {
"model": "claude-3-5-sonnet-20240620",
"temperature": 0.7,
"max_tokens": 300,
"messages": messages,
"tools": functions,
"tool_choice": {"type": "auto"}, # Important: dict, not string
}
# Call
response = client.query_unified_endpoint(
provider_name="anthropic",
endpoint_type="messages",
query_body=query_body,
)
print(response)