|
| 1 | +# type: ignore |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +from llama_api_client import LlamaAPIClient |
| 6 | + |
| 7 | +client = LlamaAPIClient() |
| 8 | + |
| 9 | +MODEL = "Llama-4-Maverick-17B-128E-Instruct-FP8" |
| 10 | + |
| 11 | + |
| 12 | +def get_weather(location: str) -> str: |
| 13 | + return f"The weather in {location} is sunny." |
| 14 | + |
| 15 | + |
| 16 | +def run(stream: bool = False) -> None: |
| 17 | + tools = [ |
| 18 | + { |
| 19 | + "type": "function", |
| 20 | + "function": { |
| 21 | + "name": "get_weather", |
| 22 | + "description": "Get current weather for a given location.", |
| 23 | + "parameters": { |
| 24 | + "type": "object", |
| 25 | + "properties": { |
| 26 | + "location": { |
| 27 | + "type": "string", |
| 28 | + "description": "City and country e.g. Bogotá, Colombia", |
| 29 | + } |
| 30 | + }, |
| 31 | + "required": ["location"], |
| 32 | + "additionalProperties": False, |
| 33 | + }, |
| 34 | + "strict": True, |
| 35 | + }, |
| 36 | + } |
| 37 | + ] |
| 38 | + messages = [ |
| 39 | + {"role": "user", "content": "Is it raining in Bellevue?"}, |
| 40 | + ] |
| 41 | + |
| 42 | + response = client.chat.completions.create( |
| 43 | + model=MODEL, |
| 44 | + messages=messages, |
| 45 | + tools=tools, |
| 46 | + max_completion_tokens=2048, |
| 47 | + temperature=0.6, |
| 48 | + stream=stream, |
| 49 | + ) |
| 50 | + |
| 51 | + completion_message = None |
| 52 | + if stream: |
| 53 | + tool_call = {"function": {"arguments": ""}} |
| 54 | + |
| 55 | + stop_reason = None |
| 56 | + for chunk in response: |
| 57 | + if chunk.event.delta.type == "tool_call": |
| 58 | + if chunk.event.delta.id: |
| 59 | + tool_call["id"] = chunk.event.delta.id |
| 60 | + if chunk.event.delta.function.name: |
| 61 | + print( |
| 62 | + f"Using tool_id={chunk.event.delta.id} with name={chunk.event.delta.function.name}", |
| 63 | + ) |
| 64 | + tool_call["function"]["name"] = chunk.event.delta.function.name |
| 65 | + if chunk.event.delta.function.arguments: |
| 66 | + tool_call["function"][ |
| 67 | + "arguments" |
| 68 | + ] += chunk.event.delta.function.arguments |
| 69 | + print(chunk.event.delta.function.arguments, end="", flush=True) |
| 70 | + |
| 71 | + if chunk.event.stop_reason is not None: |
| 72 | + stop_reason = chunk.event.stop_reason |
| 73 | + |
| 74 | + completion_message = { |
| 75 | + "role": "assistant", |
| 76 | + "content": { |
| 77 | + "type": "text", |
| 78 | + "text": "", |
| 79 | + }, |
| 80 | + "tool_calls": [tool_call], |
| 81 | + "stop_reason": stop_reason, |
| 82 | + } |
| 83 | + else: |
| 84 | + print(response) |
| 85 | + completion_message = response.completion_message.model_dump() |
| 86 | + |
| 87 | + # Next Turn |
| 88 | + messages.append(completion_message) |
| 89 | + for tool_call in completion_message["tool_calls"]: |
| 90 | + if tool_call["function"]["name"] == "get_weather": |
| 91 | + parse_args = json.loads(tool_call["function"]["arguments"]) |
| 92 | + result = get_weather(**parse_args) |
| 93 | + |
| 94 | + messages.append( |
| 95 | + { |
| 96 | + "role": "tool", |
| 97 | + "tool_call_id": tool_call["id"], |
| 98 | + "content": result, |
| 99 | + }, |
| 100 | + ) |
| 101 | + |
| 102 | + response = client.chat.completions.create( |
| 103 | + model=MODEL, |
| 104 | + messages=messages, |
| 105 | + tools=tools, |
| 106 | + max_completion_tokens=2048, |
| 107 | + temperature=0.6, |
| 108 | + stream=stream, |
| 109 | + ) |
| 110 | + |
| 111 | + if stream: |
| 112 | + for chunk in response: |
| 113 | + print(chunk.event.delta.text, end="", flush=True) |
| 114 | + else: |
| 115 | + print(response) |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + run(stream=True) |
| 120 | + run(stream=False) |
0 commit comments