Spaces:
Runtime error
Runtime error
Create main.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186import uuidfrom fastapi import FastAPIfrom fastapi.responses import StreamingResponsefrom fastapi.middleware.cors import CORSMiddlewarefrom langchain_core.messages import BaseMessage, HumanMessage, trim_messagesfrom langchain_core.tools import toolfrom langchain_openai import ChatOpenAIfrom langgraph.checkpoint.memory import MemorySaverfrom langgraph.prebuilt import create_react_agentfrom pydantic import BaseModelfrom typing import Optionalimport jsonfrom sse_starlette.sse import EventSourceResponseimport ioimport sysfrom contextlib import redirect_stdout, redirect_stderrfrom langchain_core.runnables import RunnableConfigimport requestsimport uvicornimport reapp = FastAPI()
app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"],)
class CodeExecutionResult: def __init__(self, output: str, error: str = None): self.output = output self.error = error
API_URL = "https://pvanand-code-execution-files-v4.hf.space"
@tooldef execute_python(code: str) -> str: """Execute Python code in an IPython interactiveshell and return the output. Args: code: The Python code to execute
Available Libraries: # Use plotly as the default charting library # While using yfinance to pull stock data, Always clean the multiindex columns as this might cause issues in plotting plotly charts # Remove the ticker level from columns if it exists yf_data = yf.download(symbol, start=start_date, end=end_date) if isinstance(yf_data.columns, pd.MultiIndex): yf_data.columns = yf_data.columns.get_level_values(0)
matplotlib pandas plotly groq yfinance numpy seaborn numpy scikit-learn statsmodels geopandas folium fpdf kaleido scipy geopy mapbox
Artifacts are automatically rendered in the UI hence no need to provide links to them. """
#print(config)
headers = { 'accept': 'application/json', 'Content-Type': 'application/json' } data = { "session_token": "test12345", #config.configurable.get("thread_id", "test"), "code": code } response = requests.post( f'{API_URL}/v0/execute', headers=headers, data=json.dumps(data) )
if response.status_code != 200: return f"Error: Request failed with status code {response.status_code}. Response: {response.text}" else: response_json = response.json() return f"data: {json.dumps(response_json)} \ndata:"
# Configure the memory and model"memory = MemorySaver()model = ChatOpenAI(model="gpt-4o-mini", streaming=True)
def state_modifier(state) -> list[BaseMessage]: return trim_messages( state["messages"], token_counter=len, max_tokens=16000, strategy="last", start_on="human", include_system=True, allow_partial=False, )
# Create the agent with the Python execution toolagent = create_react_agent( model, tools=[execute_python], checkpointer=memory, state_modifier=state_modifier,)
class ChatInput(BaseModel): message: str thread_id: Optional[str] = None
@app.post("/chat")async def chat(input_data: ChatInput): thread_id = input_data.thread_id or str(uuid.uuid4()) config = { "configurable": { "thread_id": thread_id } } input_message = HumanMessage(content=input_data.message) async def generate(): async for event in agent.astream_events( {"messages": [input_message]}, config, version="v2" ): kind = event["event"] if kind == "on_chat_model_stream": content = event["data"]["chunk"].content if content: yield f"{json.dumps({'type': 'token', 'content': content})}\n"
elif kind == "on_tool_start": tool_input = event['data'].get('input', '') yield f"{json.dumps({'type': 'tool_start', 'tool': event['name'], 'input': tool_input})}\n" elif kind == "on_tool_end": tool_output = event['data'].get('output', '').content #print(type(tool_output)) #print(dir(tool_output)) #print the keys pattern = r'data: (.*?)\ndata:' match = re.search(pattern, tool_output) print(tool_output)
if match: tool_output_json = match.group(1).strip() try: tool_output = json.loads(tool_output_json) if "artifacts" in tool_output: for artifact in tool_output["artifacts"]: artifact_content = requests.get(f"{API_URL}/artifact/{artifact['artifact_id']}").content print(artifact_content) tool_output["artifacts"][artifact["artifact_id"]] = artifact_content except Exception as e: print(e) print("Error parsing tool output as json: ", tool_output) else: print("No match found in tool output") yield f"{json.dumps({'type': 'tool_end', 'tool': event['name'], 'output': tool_output})}\n" return EventSourceResponse( generate(), media_type="text/event-stream" )
@app.get("/health")async def health_check(): return {"status": "healthy"}
if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=9000)main.pyAdded+186