mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 19:24:27 +00:00
Update instructor tutorial (#7784)
This commit is contained in:
parent
3c47d6599c
commit
b51c46200c
1 changed files with 17 additions and 32 deletions
|
@ -1,32 +1,22 @@
|
||||||
# Instructor - Function Calling
|
# Instructor - Function Calling
|
||||||
|
|
||||||
Use LiteLLM Router with [jxnl's instructor library](https://github.com/jxnl/instructor) for function calling in prod.
|
Use LiteLLM with [jxnl's instructor library](https://github.com/jxnl/instructor) for function calling in prod.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import litellm
|
import os
|
||||||
from litellm import Router
|
|
||||||
import instructor
|
import instructor
|
||||||
|
from litellm import completion
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
litellm.set_verbose = True # 👈 print DEBUG LOGS
|
os.environ["LITELLM_LOG"] = "DEBUG" # 👈 print DEBUG LOGS
|
||||||
|
|
||||||
client = instructor.patch(
|
client = instructor.from_litellm(completion)
|
||||||
Router(
|
|
||||||
model_list=[
|
# import dotenv
|
||||||
{
|
# dotenv.load_dotenv()
|
||||||
"model_name": "gpt-3.5-turbo", openai model name
|
|
||||||
"litellm_params": { # params for litellm completion/embedding call - e.g.: https://github.com/BerriAI/litellm/blob/62a591f90c99120e1a51a8445f5c3752586868ea/litellm/router.py#L111
|
|
||||||
"model": "azure/chatgpt-v-2",
|
|
||||||
"api_key": os.getenv("AZURE_API_KEY"),
|
|
||||||
"api_version": os.getenv("AZURE_API_VERSION"),
|
|
||||||
"api_base": os.getenv("AZURE_API_BASE"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class UserDetail(BaseModel):
|
class UserDetail(BaseModel):
|
||||||
|
@ -35,7 +25,7 @@ class UserDetail(BaseModel):
|
||||||
|
|
||||||
|
|
||||||
user = client.chat.completions.create(
|
user = client.chat.completions.create(
|
||||||
model="gpt-3.5-turbo",
|
model="gpt-4o-mini",
|
||||||
response_model=UserDetail,
|
response_model=UserDetail,
|
||||||
messages=[
|
messages=[
|
||||||
{"role": "user", "content": "Extract Jason is 25 years old"},
|
{"role": "user", "content": "Extract Jason is 25 years old"},
|
||||||
|
@ -52,25 +42,20 @@ print(f"user: {user}")
|
||||||
## Async Calls
|
## Async Calls
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import litellm
|
import asyncio
|
||||||
|
import instructor
|
||||||
from litellm import Router
|
from litellm import Router
|
||||||
import instructor, asyncio
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
aclient = instructor.apatch(
|
aclient = instructor.patch(
|
||||||
Router(
|
Router(
|
||||||
model_list=[
|
model_list=[
|
||||||
{
|
{
|
||||||
"model_name": "gpt-3.5-turbo",
|
"model_name": "gpt-4o-mini",
|
||||||
"litellm_params": {
|
"litellm_params": {"model": "gpt-4o-mini"},
|
||||||
"model": "azure/chatgpt-v-2",
|
|
||||||
"api_key": os.getenv("AZURE_API_KEY"),
|
|
||||||
"api_version": os.getenv("AZURE_API_VERSION"),
|
|
||||||
"api_base": os.getenv("AZURE_API_BASE"),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
default_litellm_params={"acompletion": True}, # 👈 IMPORTANT - tells litellm to route to async completion function.
|
default_litellm_params={"acompletion": True}, # 👈 IMPORTANT - tells litellm to route to async completion function.
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -82,7 +67,7 @@ class UserExtract(BaseModel):
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
model = await aclient.chat.completions.create(
|
model = await aclient.chat.completions.create(
|
||||||
model="gpt-3.5-turbo",
|
model="gpt-4o-mini",
|
||||||
response_model=UserExtract,
|
response_model=UserExtract,
|
||||||
messages=[
|
messages=[
|
||||||
{"role": "user", "content": "Extract jason is 25 years old"},
|
{"role": "user", "content": "Extract jason is 25 years old"},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue