pvanand commited on
Commit
9da8a12
·
verified ·
1 Parent(s): 3644f88

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +23 -12
main.py CHANGED
@@ -19,6 +19,9 @@ import requests
19
  import uvicorn
20
  import re
21
  from fastapi.staticfiles import StaticFiles
 
 
 
22
 
23
  app = FastAPI()
24
 
@@ -40,23 +43,17 @@ class CodeExecutionResult:
40
  API_URL = "https://pvanand-code-execution-files-v5.hf.space"
41
 
42
  @tool
43
- def execute_python(code: str) -> str:
44
  """Execute Python code in an IPython interactiveshell and return the output.
45
  The returned artifacts (if present) are automatically rendered in the UI and visible to the user.
46
  Args:
47
- code: The Python code to execute
48
 
49
  Available Libraries:
50
- # Use plotly as the default charting library
51
- # While using yfinance to pull stock data, Always clean the multiindex columns as this might cause issues in plotting plotly charts
52
- # Remove the ticker level from columns if it exists
53
- yf_data = yf.download(symbol, start=start_date, end=end_date)
54
- if isinstance(yf_data.columns, pd.MultiIndex):
55
- yf_data.columns = yf_data.columns.get_level_values(0)
56
-
57
- matplotlib
58
- pandas
59
  plotly
 
60
  groq
61
  yfinance
62
  numpy
@@ -98,10 +95,24 @@ def execute_python(code: str) -> str:
98
  # Configure the memory and model"
99
  memory = MemorySaver()
100
  model = ChatOpenAI(model="gpt-4o-mini", streaming=True)
 
 
 
 
 
 
101
 
102
  def state_modifier(state) -> list[BaseMessage]:
 
 
 
 
 
 
 
 
103
  return trim_messages(
104
- state["messages"],
105
  token_counter=len,
106
  max_tokens=16000,
107
  strategy="last",
 
19
  import uvicorn
20
  import re
21
  from fastapi.staticfiles import StaticFiles
22
+ from langchain_core.runnables import RunnableConfig
23
+ from langchain_core.prompts import ChatPromptTemplate
24
+ from datetime import datetime
25
 
26
  app = FastAPI()
27
 
 
43
  API_URL = "https://pvanand-code-execution-files-v5.hf.space"
44
 
45
  @tool
46
+ def execute_python(code: str, config: RunnableConfig):
47
  """Execute Python code in an IPython interactiveshell and return the output.
48
  The returned artifacts (if present) are automatically rendered in the UI and visible to the user.
49
  Args:
50
+ code: Valid Python code with correct indentation and syntax including necessary imports.
51
 
52
  Available Libraries:
53
+ # Use Plotly for creating visualizations
54
+
 
 
 
 
 
 
 
55
  plotly
56
+ pandas
57
  groq
58
  yfinance
59
  numpy
 
95
  # Configure the memory and model"
96
  memory = MemorySaver()
97
  model = ChatOpenAI(model="gpt-4o-mini", streaming=True)
98
+ prompt = ChatPromptTemplate.from_messages([
99
+ ("system", f"You are a Data Visualization assistant.You have access to a jupyter client with access to internet for python code execution.\
100
+ Your taks is to assist users with your data analysis and visualization expertise. Use Plotly for creating visualizations. Today's date is \
101
+ {datetime.now().strftime('%Y-%m-%d')}. The current folder contains the following files: {{collection_files}}"),
102
+ ("placeholder", "{messages}"),
103
+ ])
104
 
105
  def state_modifier(state) -> list[BaseMessage]:
106
+ collection_files = "None"
107
+ # Format the prompt with the current state
108
+ formatted_prompt = prompt.invoke({
109
+ "collection_files": collection_files,
110
+ "messages": state["messages"]
111
+ })
112
+
113
+ # Trim the messages
114
  return trim_messages(
115
+ formatted_prompt,
116
  token_counter=len,
117
  max_tokens=16000,
118
  strategy="last",