-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding docs for instrument_openai
#18
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# OpenAI | ||
|
||
## Introduction | ||
|
||
Logfire supports instrumenting calls to OpenAI with one extra line of code. | ||
|
||
```python hl_lines="6" | ||
import openai | ||
import logfire | ||
|
||
client = openai.Client() | ||
|
||
logfire.instrument_openai(client) # (1)! | ||
|
||
response = client.chat.completions.create( | ||
model='gpt-4', | ||
messages=[ | ||
{'role': 'system', 'content': 'You are a helpful assistant.'}, | ||
{'role': 'user', 'content': 'Please write me a limerick about Python logging.'}, | ||
], | ||
) | ||
print(response.choices[0].text) | ||
``` | ||
|
||
1. In general, `logfire.instrument_openai()` should be all you need. | ||
|
||
_For more information, see the [`instrument_openai()` API reference][logfire.Logfire.instrument_openai]._ | ||
|
||
With that you get: | ||
|
||
* a span around the call to OpenAI which records duration and captures any exceptions that might occur | ||
* Human-readable display of the conversation with the agent | ||
* details of the response, including the number of tokens used | ||
|
||
<figure markdown="span"> | ||
![Logfire OpenAI](../images/logfire-screenshot-openai.png){ width="500" } | ||
<figcaption>OpenAI span and conversation</figcaption> | ||
</figure> | ||
|
||
<figure markdown="span"> | ||
![Logfire OpenAI Arguments](../images/logfire-screenshot-openai-arguments.png){ width="500" } | ||
<figcaption>Span arguments including response details</figcaption> | ||
</figure> | ||
|
||
## Methods covered | ||
|
||
The following OpenAI methods are covered: | ||
|
||
- [`client.chat.completions.create`](https://platform.openai.com/docs/guides/text-generation/chat-completions-api) — with and without `stream=True` | ||
- [`client.completions.create`](https://platform.openai.com/docs/guides/text-generation/completions-api) — with and without `stream=True` | ||
- [`client.embeddings.create`](https://platform.openai.com/docs/guides/embeddings/how-to-get-embeddings) | ||
- [`client.images.generate`](https://platform.openai.com/docs/guides/images/generations) | ||
|
||
All methods are covered with both `openai.Client` and `openai.AsyncClient`. | ||
|
||
For example, here's instrumentation of an image generation call: | ||
|
||
```python | ||
import openai | ||
import logfire | ||
|
||
async def main(): | ||
client = openai.AsyncClient() | ||
logfire.instrument_openai(client) | ||
|
||
response = await client.images.generate( | ||
prompt='Image of R2D2 running through a desert in the style of cyberpunk.', | ||
model='dall-e-3', | ||
) | ||
url = response.data[0].url | ||
import webbrowser | ||
webbrowser.open(url) | ||
|
||
if __name__ == '__main__': | ||
import asyncio | ||
asyncio.run(main()) | ||
``` | ||
|
||
Gives: | ||
|
||
<figure markdown="span"> | ||
![Logfire OpenAI Image Generation](../images/logfire-screenshot-openai-image-gen.png){ width="500" } | ||
<figcaption>OpenAI image generation span</figcaption> | ||
</figure> | ||
|
||
## Streaming Responses | ||
|
||
When instrumenting streaming responses, Logfire creates two spans — one around the initial request and one | ||
around the streamed response. | ||
|
||
Here we also use Rich's [`Live`][rich.live.Live] and [`Markdown`][rich.markdown.Markdown] types to render the response in the terminal in real-time. :dancer: | ||
|
||
```python | ||
import openai | ||
import logfire | ||
from rich.console import Console | ||
from rich.live import Live | ||
from rich.markdown import Markdown | ||
|
||
client = openai.AsyncClient() | ||
logfire.instrument_openai(client) | ||
|
||
async def main(): | ||
console = Console() | ||
with logfire.span('Asking OpenAI to write some code'): | ||
response = await client.chat.completions.create( | ||
model='gpt-4', | ||
messages=[ | ||
{'role': 'system', 'content': 'Reply in markdown one.'}, | ||
{'role': 'user', 'content': 'Write Python to show a tree of files 🤞.'}, | ||
], | ||
stream=True | ||
) | ||
content = '' | ||
with Live('', refresh_per_second=15, console=console) as live: | ||
async for chunk in response: | ||
if chunk.choices[0].delta.content is not None: | ||
content += chunk.choices[0].delta.content | ||
live.update(Markdown(content)) | ||
|
||
if __name__ == '__main__': | ||
import asyncio | ||
asyncio.run(main()) | ||
``` | ||
|
||
Shows up like this in Logfire: | ||
|
||
<figure markdown="span"> | ||
![Logfire OpenAI Streaming](../images/logfire-screenshot-openai-stream.png){ width="500" } | ||
<figcaption>OpenAI streaming response</figcaption> | ||
</figure> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🕺