ericjohnson97 commited on
Commit
d02afc3
·
1 Parent(s): b8270b1

added Vector DB for message definitions. plotter now working reliably

Browse files
README.md CHANGED
@@ -2,6 +2,12 @@
2
 
3
  MAVPlot is a Python-based project which uses Gradio as an interface and GPT-X powered by OpenAI as a chatbot to generate and plot MAVLink data. It provides an easy-to-use, chatbot-like interface for users to describe the plot they would like to generate.
4
 
 
 
 
 
 
 
5
  ## Installation
6
 
7
  1. Clone the repository:
@@ -10,23 +16,26 @@ MAVPlot is a Python-based project which uses Gradio as an interface and GPT-X po
10
  git clone https://github.com/yourusername/mavplot.git
11
  ```
12
 
 
13
  2. Install the requirements:
14
 
15
  ```shell
16
  pip install -r requirements.txt
17
  ```
18
 
 
 
 
 
19
  ## Usage
20
 
21
  After installing all dependencies, run the main script using:
22
 
23
  ```shell
24
- python llm_plot.py
25
  ```
26
 
27
- A web-based Gradio interface will launch. You can then input the description of the plot you would like to generate in the textbox, or upload a file.
28
-
29
- The chatbot will process your request and generate the corresponding plot, which will be displayed in the chat interface.
30
 
31
  ## Contributing
32
 
 
2
 
3
  MAVPlot is a Python-based project which uses Gradio as an interface and GPT-X powered by OpenAI as a chatbot to generate and plot MAVLink data. It provides an easy-to-use, chatbot-like interface for users to describe the plot they would like to generate.
4
 
5
+ ![chat bot](docs/chat_bot_if.PNG)
6
+
7
+ ## Architecture
8
+
9
+ ![arch](docs/GPT_MAVPlot_Arch.png)
10
+
11
  ## Installation
12
 
13
  1. Clone the repository:
 
16
  git clone https://github.com/yourusername/mavplot.git
17
  ```
18
 
19
+ TODO
20
  2. Install the requirements:
21
 
22
  ```shell
23
  pip install -r requirements.txt
24
  ```
25
 
26
+ 3. Setup .env File
27
+
28
+ Copy the `template.env` file to a file named `.env` in your root directory. Add your Openai API key to the file
29
+
30
  ## Usage
31
 
32
  After installing all dependencies, run the main script using:
33
 
34
  ```shell
35
+ python gpt_mavplot.py
36
  ```
37
 
38
+ A web-based Gradio interface will launch. You can upload a mavlink tlog then prompt the bot to generate plots from the log. The chatbot will process your request and generate the corresponding plot, which will be displayed in the chat interface. The script use to generate the log will also be posted to the chat interface.
 
 
39
 
40
  ## Contributing
41
 
docs/GPT_MAVPlot_Arch.png ADDED
docs/chat_bot_if.PNG ADDED
llm_plot.py → gpt_mavplot.py RENAMED
@@ -27,9 +27,8 @@ def bot(history):
27
 
28
  print(history)
29
 
30
- history_str = format_history(history)
31
- response = plot_creator.create_plot(user_input, history_str)
32
- print(response)
33
  history[-1][1] = response[0]
34
  history = history + [(None, f"Here is the code used to generate the plot:")]
35
  history = history + [(None, f"{response[1]}")]
@@ -41,7 +40,10 @@ def bot(history):
41
  filename, extension = os.path.splitext(os.path.basename(file_path))
42
 
43
  history[-1][0] = f"user uploaded file: {filename}{extension}"
44
- history[-1][1] = "I will be using the file you uploaded to generate the plot. Please describe the plot you would like to generate."
 
 
 
45
 
46
  return history
47
 
 
27
 
28
  print(history)
29
 
30
+ # history_str = format_history(history)
31
+ response = plot_creator.create_plot(user_input)
 
32
  history[-1][1] = response[0]
33
  history = history + [(None, f"Here is the code used to generate the plot:")]
34
  history = history + [(None, f"{response[1]}")]
 
40
  filename, extension = os.path.splitext(os.path.basename(file_path))
41
 
42
  history[-1][0] = f"user uploaded file: {filename}{extension}"
43
+ history[-1][1] = "processing file..."
44
+ data_types = plot_creator.parse_mavlink_log()
45
+ history = history + [(None, f"here are the data types in the log {data_types}")]
46
+ history = history + [(None, f"I am done processing the file. Now you can ask me to generate a plot.")]
47
 
48
  return history
49
 
llm/gptPlotCreator.py CHANGED
@@ -12,75 +12,115 @@ from langchain.prompts.chat import (
12
  ChatPromptTemplate,
13
  HumanMessagePromptTemplate,
14
  )
 
 
15
  import os
16
  from dotenv import load_dotenv
17
  from PIL import Image
18
 
19
-
 
 
20
 
21
  class PlotCreator:
 
 
 
 
22
 
23
- last_code = ""
24
 
25
  def __init__(self):
26
- load_dotenv()
27
- self.model = os.getenv("OPENAI_MODEL")
28
- # llm = ChatOpenAI(model_name="gpt-3.5-turbo", max_tokens=2000, temperature=0)
 
 
 
 
 
 
29
  llm = ChatOpenAI(model_name=self.model, max_tokens=2000, temperature=0)
30
 
31
-
32
  mavlink_data_prompt = PromptTemplate(
33
- input_variables=["history", "human_input", "file"],
34
- template="You are an AI conversation agent that will be used for generating python scripts to plot mavlink data provided by the user. Please create a python script using matplotlib and pymavlink's mavutil to plot the data provided by the user. Please do not explain the code just return the script. Please plot each independent variable over time in seconds. Please save the plot to file named plot.png in the same directory as plot.py with at least 400 dpi. Also be careful not to write a script that gets stuck in an endless loop.\n\nChat History:\n{history} \n\nHUMAN: {human_input} \n\nplease read this data from the file {file}.",
35
  )
36
- self.chain = LLMChain(verbose=True, llm=llm, prompt=mavlink_data_prompt)
37
 
38
- @staticmethod
39
- def sample_lines(filename, num_lines=5):
40
- with open(filename) as f:
41
- total_lines = sum(1 for _ in f)
42
-
43
- if total_lines < num_lines:
44
- raise ValueError("File has fewer lines than the number of lines requested.")
45
-
46
- line_numbers = random.sample(range(1, total_lines + 1), num_lines)
47
- lines = [linecache.getline(filename, line_number).rstrip() for line_number in line_numbers]
48
-
49
- return '\n'.join(lines)
50
 
51
  @staticmethod
52
  def extract_code_snippets(text):
53
- pattern = r'```.*?\n(.*?)```'
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  snippets = re.findall(pattern, text, re.DOTALL | re.MULTILINE)
55
- if len(snippets) == 0:
56
- snippets = [text]
57
  return snippets
58
 
59
  @staticmethod
60
  def write_plot_script(filename, text):
61
- with open(filename, 'w') as file:
62
- file.write(text)
 
 
 
 
 
 
 
 
63
 
64
  def attempt_to_fix_sctript(self, filename, error_message):
65
- # llm = ChatOpenAI(model_name="gpt-3.5-turbo", max_tokens=2000, temperature=0)
66
- llm = ChatOpenAI(model_name=self.model , max_tokens=2000, temperature=0)
 
 
 
 
67
 
 
 
 
 
 
 
 
 
68
  fix_plot_script_template = PromptTemplate(
69
- input_variables=["error", "script"],
70
- template="You are an AI agent that is designed to debug scripts created to plot mavlink data using matplotlib and pymavlink's mavutil. the following script produced this error: \n\n{script}\n\nThe error is: \n\n{error}\n\nPlease fix the script so that it produces the correct plot. please return the fixed script in a markdown code block.",
71
  )
72
 
73
  # read script from file
74
  with open(filename, 'r') as file:
75
  script = file.read()
76
-
 
77
  chain = LLMChain(verbose=True, llm=llm, prompt=fix_plot_script_template)
78
- response = chain.run({"error": error_message, "script": script})
 
 
 
79
  print(response)
80
- code = PlotCreator.extract_code_snippets(response)
81
- PlotCreator.write_plot_script("plot.py", code[0])
82
 
83
- # run the script
84
  try:
85
  subprocess.check_output(["python", "plot.py"], stderr=subprocess.STDOUT)
86
  except:
@@ -88,22 +128,48 @@ class PlotCreator:
88
  return code
89
 
90
  def set_logfile_name(self, filename):
 
 
 
 
 
 
91
  self.logfile_name = filename
92
 
93
- def create_plot(self, human_input, history):
94
-
 
 
 
 
 
 
95
  if self.last_code != "":
96
- history = history + "\n\nLast script generated:\n\n" + self.last_code
 
 
 
 
 
 
 
 
 
 
97
 
 
98
 
99
- response = self.chain.run({"history" : history, "file": self.logfile_name, "human_input": human_input})
 
100
  print(response)
101
 
102
- # parse the code from the response
103
  code = self.extract_code_snippets(response)
 
 
104
  self.write_plot_script("plot.py", code[0])
105
 
106
- # run the script if it doesn't work capture output and call attempt_to_fix_script
107
  try:
108
  subprocess.check_output(["python", "plot.py"], stderr=subprocess.STDOUT)
109
  except subprocess.CalledProcessError as e:
@@ -113,6 +179,83 @@ class PlotCreator:
113
  print(e)
114
  code = self.attempt_to_fix_sctript("plot.py", str(e))
115
 
 
116
  self.last_code = code[0]
117
 
 
118
  return [("plot.png", None), code[0]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  ChatPromptTemplate,
13
  HumanMessagePromptTemplate,
14
  )
15
+ from langchain.vectorstores import Chroma
16
+ from langchain.embeddings.openai import OpenAIEmbeddings
17
  import os
18
  from dotenv import load_dotenv
19
  from PIL import Image
20
 
21
+ # Import required modules
22
+ from pymavlink import mavutil
23
+ import json
24
 
25
  class PlotCreator:
26
+ """
27
+ PlotCreator is a class that generates Python scripts to plot MAVLink data
28
+ provided by a user, leveraging OpenAI's models for conversational agents.
29
+ """
30
 
31
+ last_code = "" # stores the last code generated
32
 
33
  def __init__(self):
34
+ """
35
+ Initialize an instance of PlotCreator.
36
+ """
37
+
38
+ load_dotenv() # load environment variables from a .env file
39
+
40
+ self.model = os.getenv("OPENAI_MODEL") # get the name of the OpenAI model to use
41
+
42
+ # create an instance of ChatOpenAI with the specified model, maximum tokens, and temperature
43
  llm = ChatOpenAI(model_name=self.model, max_tokens=2000, temperature=0)
44
 
45
+ # define the input variables and template for the prompt to generate Python scripts
46
  mavlink_data_prompt = PromptTemplate(
47
+ input_variables=["data_types", "history", "human_input", "file"],
48
+ template="You are an AI conversation agent that will be used for generating python scripts to plot mavlink data provided by the user. Please create a python script using matplotlib and pymavlink's mavutil to plot the data provided by the user. Please do not explain the code just return the script. Please plot each independent variable over time in seconds. Please save the plot to file named plot.png in the same directory as plot.py with at least 400 dpi. please use blocking=false in your call to recv_match and be sure to break the loop if a msg in None. here are the relevant data types in the log:\n\n{data_types} \n\nChat History:\n{history} \n\nHUMAN: {human_input} \n\nplease read this data from the file {file}.",
49
  )
 
50
 
51
+ # create an instance of LLMChain with the defined prompt and verbosity
52
+ self.chain = LLMChain(verbose=True, llm=llm, prompt=mavlink_data_prompt)
 
 
 
 
 
 
 
 
 
 
53
 
54
  @staticmethod
55
  def extract_code_snippets(text):
56
+ """
57
+ Extracts code snippets from a text.
58
+
59
+ This function searches the text for substrings enclosed in '```', which are assumed to be code snippets.
60
+
61
+ Args:
62
+ text (str): The text to search for code snippets.
63
+
64
+ Returns:
65
+ list: A list of code snippets found in the text. If no snippets are found, returns a list containing the original text.
66
+ """
67
+
68
+ pattern = r'```.*?\n(.*?)```' # pattern to match code snippets enclosed in '```'
69
+ # use regex to find all matches of the pattern in the text
70
  snippets = re.findall(pattern, text, re.DOTALL | re.MULTILINE)
71
+ if len(snippets) == 0: # if no snippets were found
72
+ snippets = [text] # treat the entire text as a single snippet
73
  return snippets
74
 
75
  @staticmethod
76
  def write_plot_script(filename, text):
77
+ """
78
+ Writes a script to a file.
79
+
80
+ Args:
81
+ filename (str): The name of the file to write the script to.
82
+ text (str): The script to write to the file.
83
+ """
84
+
85
+ with open(filename, 'w') as file: # open the file for writing
86
+ file.write(text) # write the script to the file
87
 
88
  def attempt_to_fix_sctript(self, filename, error_message):
89
+ """
90
+ Attempts to fix a script that caused an error.
91
+
92
+ Args:
93
+ filename (str): The name of the file containing the script.
94
+ error_message (str): The error message produced by the script.
95
 
96
+ Returns:
97
+ list: A list containing the fixed script, or the original script with an error message if it couldn't be fixed.
98
+ """
99
+
100
+ # create an instance of ChatOpenAI with the specified model, maximum tokens, and temperature
101
+ llm = ChatOpenAI(model_name=self.model , max_tokens=8000, temperature=0)
102
+
103
+ # define the input variables and template for the prompt to generate Python scripts
104
  fix_plot_script_template = PromptTemplate(
105
+ input_variables=["data_types", "error", "script"],
106
+ template="You are an AI agent that is designed to debug scripts created to plot mavlink data using matplotlib and pymavlink's mavutil. the following script produced this error: \n\n{script}\n\nThe error is: \n\n{error}\n\n Here are message definitions that are possibly relevant for the script:\n\n {data_types}\n\n. Please fix the script so that it produces the correct plot. please return the fixed script in a markdown code block.",
107
  )
108
 
109
  # read script from file
110
  with open(filename, 'r') as file:
111
  script = file.read()
112
+
113
+ # create an instance of LLMChain with the defined prompt and verbosity
114
  chain = LLMChain(verbose=True, llm=llm, prompt=fix_plot_script_template)
115
+ try:
116
+ response = chain.run({"data_types" : self.message_types, "error": error_message, "script": script}) # run the LLMChain with the error and script as input
117
+ except:
118
+ return "Sorry I couldn't fix the script. Here is the original script I tried:\n\n" + script
119
  print(response)
120
+ code = PlotCreator.extract_code_snippets(response) # extract the fixed script from the response
121
+ PlotCreator.write_plot_script("plot.py", code[0]) # write the fixed script to a file
122
 
123
+ # run the fixed script
124
  try:
125
  subprocess.check_output(["python", "plot.py"], stderr=subprocess.STDOUT)
126
  except:
 
128
  return code
129
 
130
  def set_logfile_name(self, filename):
131
+ """
132
+ Set the name of the log file.
133
+
134
+ :param filename: The name of the log file.
135
+ :type filename: str
136
+ """
137
  self.logfile_name = filename
138
 
139
+ def create_plot(self, human_input):
140
+ """
141
+ Create a plot based on the input provided by the human.
142
+
143
+ :param human_input: Input provided by the human.
144
+ :type human_input: str
145
+ """
146
+ # Create a history of generated scripts if one exists
147
  if self.last_code != "":
148
+ history = "\n\nLast script generated:\n\n" + self.last_code
149
+ else:
150
+ history = ""
151
+
152
+ # Search the database for documents that are similar to the human input
153
+ docs = self.db.similarity_search(human_input)
154
+
155
+ # Concatenate the content of the documents into a string
156
+ data_type_info_text = ""
157
+ for doc in docs:
158
+ data_type_info_text += doc.page_content + "\n\n"
159
 
160
+ print(docs)
161
 
162
+ # Generate a response by running the chain with the relevant data types, history, file name and human input
163
+ response = self.chain.run({"data_types" : data_type_info_text, "history" : history, "file": self.logfile_name, "human_input": human_input})
164
  print(response)
165
 
166
+ # Parse the code from the response
167
  code = self.extract_code_snippets(response)
168
+
169
+ # Write the code to a file named "plot.py"
170
  self.write_plot_script("plot.py", code[0])
171
 
172
+ # Run the script and if it doesn't work, capture the output and call attempt_to_fix_script
173
  try:
174
  subprocess.check_output(["python", "plot.py"], stderr=subprocess.STDOUT)
175
  except subprocess.CalledProcessError as e:
 
179
  print(e)
180
  code = self.attempt_to_fix_sctript("plot.py", str(e))
181
 
182
+ # Store the code for the next iteration
183
  self.last_code = code[0]
184
 
185
+ # Return a list containing the filename of the plot and the code used to generate it
186
  return [("plot.png", None), code[0]]
187
+
188
+
189
+ def parse_mavlink_log(self):
190
+ """
191
+ Parse the MAVLink log to extract unique message types and their fields.
192
+
193
+ :return: A JSON string representation of the unique message types and their fields.
194
+ :rtype: str
195
+ """
196
+ # Initialize a dictionary to store unique message types and their fields
197
+ self.message_types = {}
198
+
199
+ # Establish a MAVLink connection
200
+ mav_log = mavutil.mavlink_connection(self.logfile_name)
201
+
202
+ # Loop through the log file and extract all unique message types
203
+ while True:
204
+ try:
205
+ # Receive a message
206
+ msg = mav_log.recv_match(blocking=False, type=None)
207
+ # Check if we received a message
208
+ if msg is None:
209
+ break
210
+
211
+ # Store the unique message types and their fields in the dictionary
212
+ if msg.get_type() not in self.message_types:
213
+ # Add the message type and its fields to the dictionary
214
+ self.message_types[msg.get_type()] = {
215
+ "count": 1,
216
+ "fields": {field: type(getattr(msg, field)).__name__ for field in msg.get_fieldnames()}
217
+ }
218
+ else:
219
+ # Increment the count for this message type
220
+ self.message_types[msg.get_type()]["count"] += 1
221
+
222
+ except KeyboardInterrupt:
223
+ break
224
+ except:
225
+ print("Unknown error")
226
+ break
227
+
228
+ # Create embeddings for the message types
229
+ self.create_embeddings(self.message_types)
230
+
231
+ # Return a JSON string of the message types
232
+ return json.dumps(self.message_types, indent=4)
233
+
234
+ def create_embedding(self, texts):
235
+ """
236
+ Create OpenAI embeddings for a list of texts.
237
+
238
+ :param texts: A list of texts to create embeddings for.
239
+ :type texts: list of str
240
+ """
241
+ # Initialize a dictionary to store the embeddings
242
+ embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
243
+ self.db = Chroma.from_texts(texts, embeddings)
244
+
245
+ def create_embeddings(self, message_types):
246
+ """
247
+ Create OpenAI embeddings for a dictionary of message types.
248
+
249
+ :param message_types: A dictionary of message types to create embeddings for.
250
+ :type message_types: dict
251
+ """
252
+ print(message_types)
253
+
254
+ # Convert the message types to a list of JSON strings
255
+ texts = []
256
+ for message_type in message_types:
257
+ texts.append(json.dumps({ message_type : message_types[message_type]}))
258
+
259
+ print(f"Texts: {texts}")
260
+ # Create the embeddings
261
+ self.create_embedding(texts)