forked from meta-llama/llama-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructured.py
More file actions
58 lines (46 loc) · 1.36 KB
/
Copy pathstructured.py
File metadata and controls
58 lines (46 loc) · 1.36 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
# type: ignore
from pydantic import BaseModel
from llama_api_client import LlamaAPIClient
client = LlamaAPIClient()
class Address(BaseModel):
street: str
city: str
state: str
zip: str
def run(stream: bool = False) -> None:
response = client.chat.completions.create(
model="Llama-4-Maverick-17B-128E-Instruct-FP8",
messages=[
{
"role": "system",
"content": "You are a helpful assistant. Summarize the address in a JSON object.",
},
{
"role": "user",
"content": "123 Main St, Anytown, USA",
},
],
temperature=0.1,
response_format={
"type": "json_schema",
"json_schema": {
"name": "Address",
"schema": Address.model_json_schema(),
},
},
stream=stream,
)
if stream:
maybe_json = ""
for chunk in response:
maybe_json += chunk.event.delta.text
print(chunk.event.delta.text, flush=True, end="")
print()
address = Address.model_validate_json(maybe_json)
print(address)
else:
address = Address.model_validate_json(response.completion_message.content.text)
print(address)
if __name__ == "__main__":
run(stream=True)
run(stream=False)