-
-
Save ducva/fe9cd4cd14409c949d012f5a0ab99ab0 to your computer and use it in GitHub Desktop.
Simple Assitant Script
This file contains hidden or 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
| OPENAI_API_KEY= |
This file contains hidden or 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
| from typing_extensions import override | |
| from openai import AssistantEventHandler | |
| from dotenv import load_dotenv | |
| from openai import OpenAI | |
| load_dotenv() | |
| client = OpenAI() | |
| class EventHandler(AssistantEventHandler): | |
| @override | |
| def on_text_created(self, text) -> None: | |
| print(f"\nassistant > ", end="", flush=True) | |
| @override | |
| def on_text_delta(self, delta, snapshot): | |
| print(delta.value, end="", flush=True) | |
| def on_tool_call_created(self, tool_call): | |
| print(f"\nassistant > {tool_call.type}\n", flush=True) | |
| def on_tool_call_delta(self, delta, snapshot): | |
| if delta.type == "code_interpreter": | |
| if delta.code_interpreter.input: | |
| print(delta.code_interpreter.input, end="", flush=True) | |
| if delta.code_interpreter.outputs: | |
| print(f"\n\noutput >", flush=True) | |
| for output in delta.code_interpreter.outputs: | |
| if output.type == "logs": | |
| print(f"\n{output.logs}", flush=True) | |
| # Then, we use the `create_and_stream` SDK helper | |
| # with the `EventHandler` class to create the Run | |
| # and stream the response. | |
| def main_task(): | |
| thread = client.beta.threads.create() | |
| while True: | |
| print("=" * 40) | |
| query = input("Type your question:") | |
| client.beta.threads.messages.create( | |
| thread_id=thread.id, | |
| role="user", | |
| content=query, | |
| ) | |
| with client.beta.threads.runs.create_and_stream( | |
| thread_id=thread.id, | |
| # assistant_id=assistant.id, | |
| # assistant_id="asst_mmCNutg3YwHlqd1lzWerMCiu", | |
| assistant_id="asst_ezwlYaYapZ9nX2aPX4J6s1aM", | |
| # instructions="You are helpful assistant. Help to build command line to do provided tasks. Just provide the command to run.", | |
| event_handler=EventHandler(), | |
| ) as stream: | |
| stream.until_done() | |
| if __name__ == "__main__": | |
| main_task() |
This file contains hidden or 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
| openai===1.14.0 | |
| typing-extensions===4.10.0 | |
| python-dotenv===1.0.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment