File size: 4,549 Bytes
b9fe2dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Create a folder at out_dir + query (percent encoded)
import argparse
import os
import sys
import urllib.parse

from docs_index import blocking_query_engine
from llama_index.core.instrumentation import get_dispatcher
from llama_index.core.instrumentation.event_handlers import BaseEventHandler
from llama_index.core.instrumentation.events.llm import (
  LLMChatEndEvent,
  LLMCompletionEndEvent,
)


class ModelEventHandler(BaseEventHandler):
  @classmethod
  def class_name(cls) -> str:
    """Class name."""
    return "ModelEventHandler"

  def handle(self, event) -> None:
    """Logic for handling event."""
    if isinstance(event, LLMCompletionEndEvent):
      print(f"LLM Prompt length: {len(event.prompt)}")
      print(f"LLM Prompt CONTENT: {event.prompt}")
      print(f"LLM Completion: {event.response.text!s}")
    elif isinstance(event, LLMChatEndEvent):
      messages_str = "\n".join([str(x.content) for x in event.messages])
      print(f"LLM Input Messages RAW: {event.messages}")
      print(f"LLM Input Messages length: {len(messages_str)}")
      print(f"LLM Input Messages CONTENT: {messages_str}")
      print(f"LLM Response: {event.response.message.content!s}")

      # Create a folder for the query
      query_folder = os.path.join(args.out_dir, urllib.parse.quote(query))
      os.makedirs(query_folder, exist_ok=True)
      print(f"Created folder for query: {query_folder}")

      # Save the LLM input and output to files
      with open(os.path.join(query_folder, "input.txt"), "w") as f:
        f.write(messages_str)
      with open(os.path.join(query_folder, "output.txt"), "w") as f:
        f.write(str(event.response.message.content))


# root dispatcher
root_dispatcher = get_dispatcher()

# register event handler
root_dispatcher.add_event_handler(ModelEventHandler())

QUERIES = [
  "How can I reset an input component?",
  "Show me how to style a component",
  "Create a multi-page app",
  "Is it possible to create custom components?",
  "Implement authentication",
  "Deploy a Mesop app",
  "Optimize performance",
  "Can I use JavaScript libraries in Mesop?",
  "Stream UI updates from an LLM API",
  "Debug a Mesop application",
  "Is Mesop ready for production use?",
  "Create a mobile-friendly and responsive UI",
  "Handle asynchronous operations",
  "Implement dark mode",
  "Add tooltips to Mesop components",
  "Render a pandas DataFrame as a table",
  "Add charts",
  "Handle file uploads",
]

# QUERIES = [
#   "How do I test a Mesop application?",
#   "What components are available in Mesop?",
#   "How can I reset a text input field in Mesop?",
#   "Show me how to style a component in Mesop",
#   "Create a multi-page app using Mesop",
#   "Is it possible to create custom components in Mesop?",
#   "Implement authentication in a Mesop app",
#   "How do I call an API from a Mesop application?",
#   "What's the process for deploying a Mesop app?",
#   "Optimize performance in a Mesop application",
#   "Implement a datepicker in Mesop",
#   "Can I use JavaScript libraries with Mesop?",
#   "Implement real-time updates in a Mesop app",
#   "Stream UI updates from an LLM API in Mesop",
#   "Debug a Mesop application",
#   "Is Mesop ready for production use?",
#   "Implement form validation in Mesop",
#   "Create a mobile-friendly Mesop app",
#   "Handle asynchronous operations in Mesop",
#   "Implement dark mode in a Mesop application",
#   "Add keyboard shortcuts to a Mesop app",
#   "Implement drag and drop functionality in Mesop",
#   "Create an infinite scroll feature in Mesop",
#   "How to make a row of components in Mesop",
#   "Add tooltips to Mesop components",
#   "Render a pandas DataFrame in a Mesop app",
#   "Add charts to a Mesop application",
#   "Create a table component in Mesop",
#   "Handle file uploads in a Mesop app",
#   "Use command-line flags with a Mesop application",
#   "Create a clickable link in Mesop",
#   "Implement a download link in a Mesop app",
# ]


parser = argparse.ArgumentParser(
  description="Process queries and record model events."
)
parser.add_argument(
  "--out-dir", type=str, help="Output directory for recorded events"
)
args = parser.parse_args()

if args.out_dir:
  print(f"Output directory set to: {args.out_dir}")

  # Create the output directory if it doesn't exist
  os.makedirs(args.out_dir, exist_ok=True)
  print(f"Created output directory: {args.out_dir}")
else:
  print("No output directory specified. Exiting! Specify with --out-dir")
  sys.exit(1)


for query in QUERIES:
  blocking_query_engine.query(query)