Comments (4)
here works fine.
example code
import openai
import instructor
from pydantic import BaseModel
import asyncio
client = instructor.from_openai(openai.AsyncOpenAI())
class User(BaseModel):
name: str
age: int
async def extract():
return await client.chat.completions.create(
model="gpt-4-turbo-preview",
messages=[
{"role": "user", "content": "Create a user"},
],
response_model=User,
)
async def create_users(num_users: int):
tasks = [extract() for _ in range(num_users)]
users = await asyncio.gather(*tasks)
return users
async def main():
users = await create_users(3)
for user in users:
print(user)
if __name__ == "__main__":
asyncio.run(main())
output
name='John Doe' age=30
name='John Doe' age=30
name='John Doe' age=25
maybe your error is because you are doing this on jupyter notebook?
if this is the case, add this on the beginning of your notebook
nest_asyncio.apply()
from instructor.
Interesting! I was not using notebooks. But it was part of a class, so something like this:
class X
async def start
await aynscio.gather(*self.call(....))
async def call
... call instructor from here ...
I wonder if something like this would work?
from instructor.
did you try
nest_asyncio.apply()?
https://pypi.org/project/nest-asyncio/
from instructor.
@bryanhpchiang i used your class to reimplement some of @kevin-weitgenant 's code
import openai
import instructor
from pydantic import BaseModel
import asyncio
client = instructor.from_openai(openai.AsyncOpenAI())
class User(BaseModel):
name: str
age: int
class UserFactory:
def __init__(self, num_users: int):
self.num_users = num_users
async def start(self):
coros = [self.call() for _ in range(self.num_users)]
return await asyncio.gather(*coros)
async def call(self):
return await client.chat.completions.create(
model="gpt-4-turbo-preview",
messages=[
{"role": "user", "content": "Create a user"},
],
response_model=User,
)
if __name__ == "__main__":
userFactory = UserFactory(10)
print(asyncio.run(userFactory.start()))
This works pretty nicely out of the box if you run it. Closing this issue for now since there's been no activity for a while too.
from instructor.
Related Issues (20)
- How to fall back to normal completion when no tool is needed HOT 1
- Error handling broken in `retry.py` under certain circumstances
- ModuleNotFoundError: No module named 'google' HOT 3
- Token usage irregularities HOT 1
- create & create_with_completion fails on non-BaseModel classes HOT 3
- 'List' response models raise exception when using create_with_completion HOT 2
- Count tokens used in retried requests due to Pydantic validation failures HOT 4
- TOOLS mode fails on retries HOT 6
- Is it possible to get the inputs that Instructor uses? HOT 1
- Failing with Ollama HOT 11
- AnthropicBedrock Tools is already out of beta HOT 1
- "Not the same number of function calls and responses" when using Mistral API
- Anthropic client is buggy HOT 1
- general way or tutorial to support any LLM provider HOT 2
- Incorrect error message in multiple tool calls assertion HOT 1
- Cannot retrieve log probabilities for generated tokens
- Manage shots for better accuracy HOT 1
- Request: Add Support for VertexAI Gemini Models in Litellm with Instructor. HOT 1
- Overload for `response_model=None` for `create(...)` HOT 3
- "Maybe" pattern spoils categorization? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from instructor.