Skip to content

Commit

Permalink
Add Agent section to Chat docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmpcollins committed Jan 12, 2025
1 parent 077f481 commit 55e1783
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/chat.md
Original file line number Diff line number Diff line change
@@ -119,3 +119,33 @@ The `Chat` class also support asynchronous usage through the following methods:

- `asubmit`: Asynchronously submit the chat to the LLM model.
- `aexec_function_call`: Asynchronously execute the function call in the chat. This is required to handle the `AsyncParallelFunctionCall` output type.

## Agent

A very basic form of an agent can be created by running a loop that submits the chat to the LLM and executes function calls until some stop condition is met.

```python
from magentic import Chat, FunctionCall, ParallelFunctionCall, UserMessage


def get_current_weather(location, unit="fahrenheit"):
"""Get the current weather in a given location"""
# Pretend to query an API
return {
"location": location,
"temperature": "72",
"unit": unit,
"forecast": ["sunny", "windy"],
}


chat = Chat(
messages=[UserMessage("What's the weather like in Boston?")],
functions=[get_current_weather],
output_types=[FunctionCall, str],
).submit()
while isinstance(chat.last_message.content, FunctionCall | ParallelFunctionCall):
chat = chat.exec_function_call().submit()
print(chat.last_message.content)
# 'The current weather in Boston is 72°F, with sunny and windy conditions.'
```

0 comments on commit 55e1783

Please sign in to comment.