import json import os import gradio as gr os.environ['OPENAI_API_KEY']=os.getenv('OpenAI_KEY') os.environ["LANGCHAIN_API_KEY"]=os.getenv('LANGCHAIN_API_KEY') os.environ["LANGCHAIN_TRACING_V2"]="true" os.environ["LANGCHAIN_ENDPOINT"]="https://api.smith.langchain.com" os.environ["LANGCHAIN_PROJECT"]=os.getenv('LANGCHAIN_PROJECT') import openai from langsmith.wrappers import wrap_openai from langsmith import traceable # Auto-trace LLM calls in-context client = wrap_openai(openai.Client()) FakeNewsAggregator = client.beta.assistants.retrieve(os.getenv('OPENAI_ASSISTANT_ID')) thread= client.beta.threads.create() @traceable # Auto-trace this function def IntroPythonTutorRequest(text): global FakeNewsAggregator global thread message = client.beta.threads.messages.create( thread_id=thread.id, role="user", content=text ) run = client.beta.threads.runs.create_and_poll( thread_id=thread.id, assistant_id=FakeNewsAggregator.id ) if run.status == 'completed': messages = client.beta.threads.messages.list( thread_id=thread.id ) gpt_response=messages.data[0].content[0].text.value return gpt_response else: return run.status gr.Interface(fn=IntroPythonTutorRequest, inputs=["text"], outputs=["text"], flagging_mode='never', title="Intro to Python Tutor", description="I am virtual assistant to help you practice basic Python ", theme="soft", examples=['Brief intro to Python','Variables in Python', '5 Coding exercises', '3 Coding exercises involving booleans'] ).launch(share=False)