Python attachment documentation, plus fixed a mimetype detection bug

Refs #587
This commit is contained in:
Simon Willison 2024-10-28 14:37:11 -07:00
parent 286cf9fcd9
commit 570a3eccae
2 changed files with 31 additions and 1 deletions

View file

@ -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
@ -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

View file

@ -45,7 +45,9 @@ class Attachment:
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")