Skip to content
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

feat: #1 add supports complex struct parsing with streaming #7

Merged
merged 7 commits into from
May 14, 2024

Conversation

AlmogBaku
Copy link
Owner

@AlmogBaku AlmogBaku commented Mar 15, 2024

This PR supports parsing JSON/YAML from the stream and processing it as a model.
Allows you to implement features such as #1

@AlmogBaku
Copy link
Owner Author

AlmogBaku commented Mar 16, 2024

I would love to get some feedback on the API:

class MathProblem(BaseModel):
    steps: List[str]
    answer: Optional[int] = None


# Define handler
class Handler(BaseHandler):
    def model(self):
        return MathProblem

    async def handle_partially_parsed(self, data: MathProblem) -> Optional[Terminate]:
        if len(data.steps) == 0 and data.answer:
            return Terminate() # something is wrong here, so we immediately stop

        if data.answer:
            self.ws.send(data.answer) #show to the user with WebSocket

    async def terminated(self):
        ws.close()




# Invoke OpenAI request
async def main():
    resp = await client.chat.completions.create(
        messages=[{
            "role": "system",
            "content":
                "For every question asked, you must first state the steps, and then the answer."
                "Your response should be in the following format: \n"
                " steps: List[str]\n"
                " answer: int\n"
                "ONLY write the YAML, without any other text or wrapping it in a code block."
                "YAML should be VALID, and strings must be in double quotes."
        }, {"role": "user", "content": "1+3*2"}],
        stream=True
    )
    await process_struct_response(resp, Handler(), 'yaml')

@AlmogBaku
Copy link
Owner Author

cc @jogardi @MRYingLEE @yotammanor
Any chance you can help with feedback here?

@yotammanor
Copy link
Contributor

Hi @AlmogBaku, hope all is well!

I read through the PR, but haven't found the time to try out the api. I can give some bikeshedding comments right away, if that helps let me kno. Either way I'll try to find time to address the core issues (but can't promise my time will allow, unfortunately).

@AlmogBaku
Copy link
Owner Author

Right away feedback is most welcome:)
Thabks

@AlmogBaku AlmogBaku merged commit b9542ff into master May 14, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants