Giter Site home page Giter Site logo

Comments (4)

kevin-weitgenant avatar kevin-weitgenant commented on June 15, 2024

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.

bryanhpchiang avatar bryanhpchiang commented on June 15, 2024

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.

kevin-weitgenant avatar kevin-weitgenant commented on June 15, 2024

did you try
nest_asyncio.apply()?

https://pypi.org/project/nest-asyncio/

from instructor.

ivanleomk avatar ivanleomk commented on June 15, 2024

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.