seansullivan commited on
Commit
cdb6e0f
·
verified ·
1 Parent(s): 96940db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py CHANGED
@@ -17,6 +17,32 @@ from langchain_anthropic import ChatAnthropic
17
  from langchain_community.tools import ShellTool
18
  from langgraph.prebuilt import create_react_agent
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # Show title and description.
22
  # Add a radio button for mode selection
@@ -349,7 +375,17 @@ else:
349
  messages_modifier=st.session_state.qa_system_prompt,
350
  checkpointer=memory
351
  )
 
 
 
 
 
 
352
 
 
 
 
 
353
  async def run_github_editor(query: str, thread_id: str = "default"):
354
  inputs = {"messages": [HumanMessage(content=query)]}
355
  config = {
@@ -360,6 +396,7 @@ else:
360
  st.write(f"Human: {query}\n")
361
 
362
  current_thought = ""
 
363
 
364
  graph = task_graph if mode == "Task" else qa_graph
365
 
@@ -374,15 +411,21 @@ else:
374
  if isinstance(content, list) and content and isinstance(content[0], dict):
375
  text = content[0].get('text', '')
376
  current_thought += text
 
377
  if text.endswith(('.', '?', '!')):
378
  st.write(current_thought.strip())
379
  current_thought = ""
380
  else:
 
381
  st.write(content, end="")
382
  elif kind == "on_tool_start" and mode == "Task":
383
  st.write(f"\nUsing tool: {event['name']}")
384
  elif kind == "on_tool_end" and mode == "Task":
385
  st.write(f"Tool result: {event['data']['output']}\n")
 
 
 
 
386
 
387
  # Create a session state variable to store the chat messages. This ensures that the
388
  # messages persist across reruns.
 
17
  from langchain_community.tools import ShellTool
18
  from langgraph.prebuilt import create_react_agent
19
 
20
+ st.markdown("""
21
+ <style>
22
+ .stCodeBlock {
23
+ background-color: #f0f0f0;
24
+ border: 1px solid #d0d0d0;
25
+ border-radius: 5px;
26
+ padding: 10px;
27
+ margin-bottom: 10px;
28
+ }
29
+ .stCodeBlock pre {
30
+ margin: 0;
31
+ }
32
+ .copyButton {
33
+ position: absolute;
34
+ top: 5px;
35
+ right: 5px;
36
+ padding: 5px 10px;
37
+ background-color: #4CAF50;
38
+ color: white;
39
+ border: none;
40
+ border-radius: 3px;
41
+ cursor: pointer;
42
+ }
43
+ </style>
44
+ """, unsafe_allow_html=True)
45
+
46
 
47
  # Show title and description.
48
  # Add a radio button for mode selection
 
375
  messages_modifier=st.session_state.qa_system_prompt,
376
  checkpointer=memory
377
  )
378
+
379
+ def format_ai_response(response):
380
+ # Function to replace code blocks with formatted versions
381
+ def replace_code_block(match):
382
+ code = match.group(1).strip()
383
+ return f"\n```python\n{code}\n```\n"
384
 
385
+ # Replace code blocks
386
+ formatted_response = re.sub(r'```(.*?)```', replace_code_block, response, flags=re.DOTALL)
387
+
388
+ return formatted_response
389
  async def run_github_editor(query: str, thread_id: str = "default"):
390
  inputs = {"messages": [HumanMessage(content=query)]}
391
  config = {
 
396
  st.write(f"Human: {query}\n")
397
 
398
  current_thought = ""
399
+ full_response = ""
400
 
401
  graph = task_graph if mode == "Task" else qa_graph
402
 
 
411
  if isinstance(content, list) and content and isinstance(content[0], dict):
412
  text = content[0].get('text', '')
413
  current_thought += text
414
+ full_response += text
415
  if text.endswith(('.', '?', '!')):
416
  st.write(current_thought.strip())
417
  current_thought = ""
418
  else:
419
+ full_response += content
420
  st.write(content, end="")
421
  elif kind == "on_tool_start" and mode == "Task":
422
  st.write(f"\nUsing tool: {event['name']}")
423
  elif kind == "on_tool_end" and mode == "Task":
424
  st.write(f"Tool result: {event['data']['output']}\n")
425
+
426
+ # Format and display the full response with proper code block formatting
427
+ formatted_response = format_ai_response(full_response)
428
+ st.markdown(formatted_response)
429
 
430
  # Create a session state variable to store the chat messages. This ensures that the
431
  # messages persist across reruns.