Skip to content

Commit

Permalink
Python attachment documentation, plus fixed a mimetype detection bug
Browse files Browse the repository at this point in the history
Refs #587
  • Loading branch information
simonw committed Oct 28, 2024
1 parent 286cf9f commit 570a3ec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
28 changes: 28 additions & 0 deletions docs/python-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ response = model.prompt(
system="Answer like GlaDOS"
)
```
### Attachments

Model that accept multi-modal input (images, audio, video etc) can be passed attachments using the `attachments=` keyword argument. This accepts a list of `llm.Attachment()` instances.

This example shows two attachments - one from a file path and one from a URL:
```python
import llm

model = llm.get_model("gpt-4o-mini")
response = model.prompt(
"Describe these images",
attachments=[
llm.Attachment(path="pelican.jpg"),
llm.Attachment(url="https://static.simonwillison.net/static/2024/pelicans.jpg"),
]
)
```
Use `llm.Attachment(content=b"binary image content here")` to pass binary content directly.

### Model options

Expand Down Expand Up @@ -114,6 +132,16 @@ print(response2.text())
```
You will get back five fun facts about skunks.

The `conversation.prompt()` method supports attachments as well:
```python
response = conversation.prompt(
"Describe these birds",
attachments=[
llm.Attachment(url="https://static.simonwillison.net/static/2024/pelicans.jpg")
]
)
```

Access `conversation.responses` for a list of all of the responses that have so far been returned during the conversation.

## Other functions
Expand Down
4 changes: 3 additions & 1 deletion llm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def resolve_type(self):
if self.path:
return puremagic.from_file(self.path, mime=True)
if self.url:
return puremagic.from_url(self.url, mime=True)
response = httpx.head(self.url)
response.raise_for_status()
return response.headers.get("content-type")
if self.content:
return puremagic.from_string(self.content, mime=True)
raise ValueError("Attachment has no type and no content to derive it from")
Expand Down

0 comments on commit 570a3ec

Please sign in to comment.