-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* poetry add mkdocs * Add mkdocs config and index page * Automatic light/dark mode. Add features and extentions. * Add remaining README sections as pages * poetry add mkdocs-jupyter * Add mkdocs-jupyter extension. Sort mkdocs.yml fields * Add example notebooks under Examples * Move config and type checking pages to top level * Add Google Analytics * Add custom css to tidy jupyter pages * Remove examples commented link * Add deploy_docs github workflow * ruff ignore docs/examples * TEMP: Deploy docs on push * Add CNAME to docs dir * Revert "TEMP: Deploy docs on push" This reverts commit 0ac66e2.
- Loading branch information
1 parent
cc05a3b
commit ea67293
Showing
15 changed files
with
863 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Deploy Docs | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: write | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- run: pipx install poetry | ||
- name: Set up Python | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.10" | ||
cache: "poetry" | ||
- run: poetry install | ||
- run: poetry run mkdocs build | ||
- name: Deploy to GitHub Pages | ||
uses: peaceiris/actions-gh-pages@v3 | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
publish_dir: ./site |
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 @@ | ||
magentic.dev |
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,47 @@ | ||
# Asyncio | ||
|
||
Asynchronous functions / coroutines can be used to concurrently query the LLM. This can greatly increase the overall speed of generation, and also allow other asynchronous code to run while waiting on LLM output. In the below example, the LLM generates a description for each US president while it is waiting on the next one in the list. Measuring the characters generated per second shows that this example achieves a 7x speedup over serial processing. | ||
|
||
```python | ||
import asyncio | ||
from time import time | ||
from typing import AsyncIterable | ||
|
||
from magentic import prompt | ||
|
||
|
||
@prompt("List ten presidents of the United States") | ||
async def iter_presidents() -> AsyncIterable[str]: | ||
... | ||
|
||
|
||
@prompt("Tell me more about {topic}") | ||
async def tell_me_more_about(topic: str) -> str: | ||
... | ||
|
||
|
||
# For each president listed, generate a description concurrently | ||
start_time = time() | ||
tasks = [] | ||
async for president in await iter_presidents(): | ||
# Use asyncio.create_task to schedule the coroutine for execution before awaiting it | ||
# This way descriptions will start being generated while the list of presidents is still being generated | ||
task = asyncio.create_task(tell_me_more_about(president)) | ||
tasks.append(task) | ||
|
||
descriptions = await asyncio.gather(*tasks) | ||
|
||
# Measure the characters per second | ||
total_chars = sum(len(desc) for desc in descriptions) | ||
time_elapsed = time() - start_time | ||
print(total_chars, time_elapsed, total_chars / time_elapsed) | ||
# 24575 28.70 856.07 | ||
|
||
|
||
# Measure the characters per second to describe a single president | ||
start_time = time() | ||
out = await tell_me_more_about("George Washington") | ||
time_elapsed = time() - start_time | ||
print(len(out), time_elapsed, len(out) / time_elapsed) | ||
# 2206 18.72 117.78 | ||
``` |
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,34 @@ | ||
# Chat Prompting | ||
|
||
The `@chatprompt` decorator works just like `@prompt` but allows you to pass chat messages as a template rather than a single text prompt. This can be used to provide a system message or for few-shot prompting where you provide example responses to guide the model's output. Format fields denoted by curly braces `{example}` will be filled in all messages - use the `escape_braces` function to prevent a string being used as a template. | ||
|
||
```python | ||
from magentic import chatprompt, AssistantMessage, SystemMessage, UserMessage | ||
from magentic.chatprompt import escape_braces | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Quote(BaseModel): | ||
quote: str | ||
character: str | ||
|
||
|
||
@chatprompt( | ||
SystemMessage("You are a movie buff."), | ||
UserMessage("What is your favorite quote from Harry Potter?"), | ||
AssistantMessage( | ||
Quote( | ||
quote="It does not do to dwell on dreams and forget to live.", | ||
character="Albus Dumbledore", | ||
) | ||
), | ||
UserMessage("What is your favorite quote from {movie}?"), | ||
) | ||
def get_movie_quote(movie: str) -> Quote: | ||
... | ||
|
||
|
||
get_movie_quote("Iron Man") | ||
# Quote(quote='I am Iron Man.', character='Tony Stark') | ||
``` |
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,59 @@ | ||
# LLM Configuration | ||
|
||
Currently two backends are available | ||
|
||
- `openai` : the default backend that uses the `openai` Python package. Supports all features. | ||
- `litellm` : uses the `litellm` Python package to enable querying LLMs from [many different providers](https://docs.litellm.ai/docs/providers). Install this with `pip install magentic[litellm]`. Note: some models may not support all features of `magentic` e.g. function calling/structured output and streaming. | ||
|
||
The backend and LLM used by `magentic` can be configured in several ways. The order of precedence of configuration is | ||
|
||
1. Arguments explicitly passed when initializing an instance in Python | ||
1. Values set using a context manager in Python | ||
1. Environment variables | ||
1. Default values from [src/magentic/settings.py](https://github.com/jackmpcollins/magentic/src/magentic/settings.py) | ||
|
||
```python | ||
from magentic import OpenaiChatModel, prompt | ||
from magentic.chat_model.litellm_chat_model import LitellmChatModel | ||
|
||
|
||
@prompt("Say hello") | ||
def say_hello() -> str: | ||
... | ||
|
||
|
||
@prompt( | ||
"Say hello", | ||
model=LitellmChatModel("ollama/llama2"), | ||
) | ||
def say_hello_litellm() -> str: | ||
... | ||
|
||
|
||
say_hello() # Uses env vars or default settings | ||
|
||
with OpenaiChatModel("gpt-3.5-turbo", temperature=1): | ||
say_hello() # Uses openai with gpt-3.5-turbo and temperature=1 due to context manager | ||
say_hello_litellm() # Uses litellm with ollama/llama2 because explicitly configured | ||
``` | ||
|
||
The following environment variables can be set. | ||
|
||
| Environment Variable | Description | Example | | ||
| ---------------------------- | -------------------------------------- | ---------------------- | | ||
| MAGENTIC_BACKEND | The package to use as the LLM backend | openai | | ||
| MAGENTIC_LITELLM_MODEL | LiteLLM model | claude-2 | | ||
| MAGENTIC_LITELLM_API_BASE | The base url to query | http://localhost:11434 | | ||
| MAGENTIC_LITELLM_MAX_TOKENS | LiteLLM max number of generated tokens | 1024 | | ||
| MAGENTIC_LITELLM_TEMPERATURE | LiteLLM temperature | 0.5 | | ||
| MAGENTIC_OPENAI_MODEL | OpenAI model | gpt-4 | | ||
| MAGENTIC_OPENAI_API_KEY | OpenAI API key to be used by magentic | sk-... | | ||
| MAGENTIC_OPENAI_API_TYPE | Allowed options: "openai", "azure" | azure | | ||
| MAGENTIC_OPENAI_BASE_URL | Base URL for an OpenAI-compatible API | http://localhost:8080 | | ||
| MAGENTIC_OPENAI_MAX_TOKENS | OpenAI max number of generated tokens | 1024 | | ||
| MAGENTIC_OPENAI_SEED | Seed for deterministic sampling | 42 | | ||
| MAGENTIC_OPENAI_TEMPERATURE | OpenAI temperature | 0.5 | | ||
|
||
When using the `openai` backend, setting the `MAGENTIC_OPENAI_BASE_URL` environment variable or using `OpenaiChatModel(..., base_url="http://localhost:8080")` in code allows you to use `magentic` with any OpenAI-compatible API e.g. [Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/ai-services/openai/quickstart?tabs=command-line&pivots=programming-language-python#create-a-new-python-application), [LiteLLM OpenAI Proxy Server](https://docs.litellm.ai/docs/proxy_server), [LocalAI](https://localai.io/howtos/easy-request-openai/). Note that if the API does not support function calling then you will not be able to create prompt-functions that return Python objects, but other features of `magentic` will still work. | ||
|
||
To use Azure with the openai backend you will need to set the `MAGENTIC_OPENAI_API_TYPE` environment variable to "azure" or use `OpenaiChatModel(..., api_type="azure")`, and also set the environment variables needed by the openai package to access Azure. See https://github.com/openai/openai-python#microsoft-azure-openai |
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,82 @@ | ||
/* Jupyter Notebook Custom CSS */ | ||
|
||
/* Hide "In" labels */ | ||
.jp-InputPrompt, | ||
.jp-InputArea-prompt { | ||
display: none !important; | ||
} | ||
|
||
/* Hide "Out" labels */ | ||
.jp-OutputPrompt, | ||
.jp-OutputArea-prompt { | ||
display: none !important; | ||
} | ||
|
||
/* Add background to cell outputs */ | ||
.jp-OutputArea.jp-Cell-outputArea { | ||
background: var(--md-code-bg-color) !important; | ||
} | ||
|
||
/* Make dataframes match code background color */ | ||
.dataframe { | ||
background: var(--md-code-bg-color); | ||
} | ||
.dataframe tbody tr:nth-child(odd) { | ||
background-color: var(--md-code-bg-color) !important; | ||
} | ||
|
||
/* Make Jupyter code highlighting the same as regular code highlighting */ | ||
|
||
.highlight-ipynb, | ||
.highlight-ipynb :is(.o, .ow) { | ||
background: var(--md-code-bg-color) !important; | ||
color: var(--md-code-hl-operator-color) !important; | ||
} | ||
|
||
.highlight-ipynb .p { | ||
color: var(--md-code-hl-punctuation-color) !important; | ||
} | ||
|
||
.highlight-ipynb :is(.cpf, .l, .s, .sb, .sc, .s2, .si, .s1, .ss) { | ||
color: var(--md-code-hl-string-color) !important; | ||
} | ||
|
||
.highlight-ipynb :is(.cp, .se, .sh, .sr, .sx) { | ||
color: var(--md-code-hl-special-color) !important; | ||
} | ||
|
||
.highlight-ipynb :is(.m, .mb, .mf, .mh, .mi, .il, .mo) { | ||
color: var(--md-code-hl-number-color) !important; | ||
} | ||
|
||
.highlight-ipynb :is(.k, .kd, .kn, .kp, .kr, .kt) { | ||
color: var(--md-code-hl-keyword-color) !important; | ||
} | ||
|
||
.highlight-ipynb :is(.kc, .n) { | ||
color: var(--md-code-hl-name-color) !important; | ||
} | ||
|
||
.highlight-ipynb :is(.no, .nb, .bp) { | ||
color: var(--md-code-hl-constant-color) !important; | ||
} | ||
|
||
.highlight-ipynb :is(.nc, .ne, .nf, .nn) { | ||
color: var(--md-code-hl-function-color) !important; | ||
} | ||
|
||
.highlight-ipynb :is(.nd, .ni, .nl, .nt) { | ||
color: var(--md-code-hl-keyword-color) !important; | ||
} | ||
|
||
.highlight-ipynb :is(.c, .cm, .c1, .ch, .cs, .sd) { | ||
color: var(--md-code-hl-comment-color) !important; | ||
} | ||
|
||
.highlight-ipynb :is(.na, .nv, .vc, .vg, .vi) { | ||
color: var(--md-code-hl-variable-color) !important; | ||
} | ||
|
||
.highlight-ipynb :is(.ge, .gr, .gh, .go, .gp, .gs, .gu, .gt) { | ||
color: var(--md-code-hl-generic-color) !important; | ||
} |
File renamed without changes.
File renamed without changes.
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,125 @@ | ||
# Overview | ||
|
||
Easily integrate Large Language Models into your Python code. Simply use the `@prompt` decorator to create functions that return structured output from the LLM. Mix LLM queries and function calling with regular Python code to create complex logic. | ||
|
||
`magentic` is | ||
|
||
- **Compact:** Query LLMs without duplicating boilerplate code. | ||
- **Atomic:** Prompts are functions that can be individually tested and reasoned about. | ||
- **Transparent:** Create "chains" using regular Python code. Define all of your own prompts. | ||
- **Compatible:** Use `@prompt` functions as normal functions, including with decorators like `@lru_cache`. | ||
- **Type Annotated:** Works with linters and IDEs. | ||
|
||
## Installation | ||
|
||
```sh | ||
pip install magentic | ||
``` | ||
|
||
or using poetry | ||
|
||
```sh | ||
poetry add magentic | ||
``` | ||
|
||
Configure your OpenAI API key by setting the `OPENAI_API_KEY` environment variable or using `openai.api_key = "sk-..."`. See the [OpenAI Python library documentation](https://github.com/openai/openai-python#usage) for more information. | ||
|
||
## Usage | ||
|
||
The `@prompt` decorator allows you to define a template for a Large Language Model (LLM) prompt as a Python function. When this function is called, the arguments are inserted into the template, then this prompt is sent to an LLM which generates the function output. | ||
|
||
```python | ||
from magentic import prompt | ||
|
||
|
||
@prompt('Add more "dude"ness to: {phrase}') | ||
def dudeify(phrase: str) -> str: | ||
... # No function body as this is never executed | ||
|
||
|
||
dudeify("Hello, how are you?") | ||
# "Hey, dude! What's up? How's it going, my man?" | ||
``` | ||
|
||
The `@prompt` decorator will respect the return type annotation of the decorated function. This can be [any type supported by pydantic](https://docs.pydantic.dev/latest/usage/types/types/) including a `pydantic` model. | ||
|
||
```python | ||
from magentic import prompt | ||
from pydantic import BaseModel | ||
|
||
|
||
class Superhero(BaseModel): | ||
name: str | ||
age: int | ||
power: str | ||
enemies: list[str] | ||
|
||
|
||
@prompt("Create a Superhero named {name}.") | ||
def create_superhero(name: str) -> Superhero: | ||
... | ||
|
||
|
||
create_superhero("Garden Man") | ||
# Superhero(name='Garden Man', age=30, power='Control over plants', enemies=['Pollution Man', 'Concrete Woman']) | ||
``` | ||
|
||
An LLM can also decide to call functions. In this case the `@prompt`-decorated function returns a `FunctionCall` object which can be called to execute the function using the arguments provided by the LLM. | ||
|
||
```python | ||
from typing import Literal | ||
|
||
from magentic import prompt, FunctionCall | ||
|
||
|
||
def activate_oven(temperature: int, mode: Literal["broil", "bake", "roast"]) -> str: | ||
"""Turn the oven on with the provided settings.""" | ||
return f"Preheating to {temperature} F with mode {mode}" | ||
|
||
|
||
@prompt( | ||
"Prepare the oven so I can make {food}", | ||
functions=[activate_oven], | ||
) | ||
def configure_oven(food: str) -> FunctionCall[str]: | ||
... | ||
|
||
|
||
output = configure_oven("cookies!") | ||
# FunctionCall(<function activate_oven at 0x1105a6200>, temperature=350, mode='bake') | ||
output() | ||
# 'Preheating to 350 F with mode bake' | ||
``` | ||
|
||
Sometimes the LLM requires making one or more function calls to generate a final answer. The `@prompt_chain` decorator will resolve `FunctionCall` objects automatically and pass the output back to the LLM to continue until the final answer is reached. | ||
|
||
In the following example, when `describe_weather` is called the LLM first calls the `get_current_weather` function, then uses the result of this to formulate its final answer which gets returned. | ||
|
||
```python | ||
from magentic import prompt_chain | ||
|
||
|
||
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"], | ||
} | ||
|
||
|
||
@prompt_chain( | ||
"What's the weather like in {city}?", | ||
functions=[get_current_weather], | ||
) | ||
def describe_weather(city: str) -> str: | ||
... | ||
|
||
|
||
describe_weather("Boston") | ||
# 'The current weather in Boston is 72°F and it is sunny and windy.' | ||
``` | ||
|
||
LLM-powered functions created using `@prompt` and `@prompt_chain` can be supplied as `functions` to other `@prompt`/`@prompt_chain` decorators, just like regular python functions. This enables increasingly complex LLM-powered functionality, while allowing individual components to be tested and improved in isolation. |
Oops, something went wrong.