pvanand commited on
Commit
0d7396d
·
verified ·
1 Parent(s): 01a6ead

Update artifacts

Browse files
Files changed (1) hide show
  1. presentation_api.py +28 -14
presentation_api.py CHANGED
@@ -15,6 +15,7 @@ from datetime import datetime
15
  from fastapi import APIRouter
16
  from langchain_core.runnables import RunnableConfig
17
  from langchain_core.prompts import ChatPromptTemplate
 
18
 
19
  router = APIRouter(
20
  prefix="/presentation",
@@ -23,20 +24,35 @@ router = APIRouter(
23
 
24
  import json
25
 
26
- @tool
27
- def plan(slides_json: str) -> str:
28
  """Create a presentation plan from a JSON string of slides (keys=slide numbers, values=content)."""
29
  try:
30
  slides = json.loads(slides_json)
31
- return f"Plan created with {len(slides)} slides: {', '.join(slides.keys())}."
32
- except json.JSONDecodeError as e:
33
- return f"Invalid JSON format. Please provide a valid JSON string {str(e)[:100]}."
 
 
 
 
 
 
 
34
 
35
- @tool
36
- def create_slide(slide_number: int, content: str) -> str:
37
  """Create a slide with the given number and content."""
38
  # Integration with slide creation API or template would go here
39
- return f"Slide {slide_number} created: {content}"
 
 
 
 
 
 
 
 
40
 
41
  @tool(parse_docstring=True)
42
  def execute_python(expression: str) -> str:
@@ -113,7 +129,6 @@ async def chat(input_data: ChatInput):
113
  version="v2"
114
  ):
115
  kind = event["event"]
116
- print(event)
117
 
118
  if kind == "on_chat_model_stream":
119
  content = event["data"]["chunk"].content
@@ -125,12 +140,11 @@ async def chat(input_data: ChatInput):
125
  yield f"{json.dumps({'type': 'tool_start', 'tool': event['name'], 'input': tool_input})}\n"
126
 
127
  elif kind == "on_tool_end":
 
128
  tool_output = event['data'].get('output', '')
129
- print(type(tool_output))
130
- print(type(dir(tool_output)))
131
- tool_output = tool_output.pretty_repr()
132
- yield f"{json.dumps({'type': 'tool_end', 'tool': event['name'], 'output': tool_output})}\n"
133
-
134
  return EventSourceResponse(
135
  generate(),
136
  media_type="text/event-stream"
 
15
  from fastapi import APIRouter
16
  from langchain_core.runnables import RunnableConfig
17
  from langchain_core.prompts import ChatPromptTemplate
18
+ from typing import Any
19
 
20
  router = APIRouter(
21
  prefix="/presentation",
 
24
 
25
  import json
26
 
27
+ @tool(response_format="content_and_artifact")
28
+ def plan(slides_json: str) -> tuple[str, dict]:
29
  """Create a presentation plan from a JSON string of slides (keys=slide numbers, values=content)."""
30
  try:
31
  slides = json.loads(slides_json)
32
+ print(slides)
33
+ return (
34
+ f"Plan created with {len(slides)} slides: {', '.join(slides.keys())}.",
35
+ {"slides_plan_json": slides_json}
36
+ )
37
+ except Exception as e:
38
+ return (
39
+ f"Invalid JSON format. Please provide a valid JSON string {str(e)[:100]}.",
40
+ None
41
+ )
42
 
43
+ @tool(response_format="content_and_artifact")
44
+ def create_slide(slide_number: int, content: str, config: RunnableConfig) -> tuple[str, dict]:
45
  """Create a slide with the given number and content."""
46
  # Integration with slide creation API or template would go here
47
+ slide = {
48
+ "number": slide_number,
49
+ "content": content,
50
+ "created_at": datetime.now().isoformat()
51
+ }
52
+ return (
53
+ f"Slide {slide_number} created",
54
+ {"slide": slide}
55
+ )
56
 
57
  @tool(parse_docstring=True)
58
  def execute_python(expression: str) -> str:
 
129
  version="v2"
130
  ):
131
  kind = event["event"]
 
132
 
133
  if kind == "on_chat_model_stream":
134
  content = event["data"]["chunk"].content
 
140
  yield f"{json.dumps({'type': 'tool_start', 'tool': event['name'], 'input': tool_input})}\n"
141
 
142
  elif kind == "on_tool_end":
143
+ print(event['data'])
144
  tool_output = event['data'].get('output', '')
145
+ artifact_output = tool_output.artifact if tool_output.artifact else None
146
+ yield f"{json.dumps({'type': 'tool_end', 'tool': event['name'], 'output': tool_output.pretty_repr(), 'artifacts_data': artifact_output})}\n"
147
+ print(tool_output.pretty_repr())
 
 
148
  return EventSourceResponse(
149
  generate(),
150
  media_type="text/event-stream"