22import os
33import requests
44
5- class LLM ():
5+
6+ class LLM :
67 def __init__ (self , transcript_text = None , target_fields = None , json = None ):
78 if json is None :
89 json = {}
9- self ._transcript_text = transcript_text # str
10- self ._target_fields = target_fields # List, contains the template field.
11- self ._json = json # dictionary
12-
10+ self ._transcript_text = transcript_text # str
11+ self ._target_fields = target_fields # List, contains the template field.
12+ self ._json = json # dictionary
13+
1314 def type_check_all (self ):
14- if type (self ._transcript_text ) != str :
15- raise TypeError (f"ERROR in LLM() attributes ->\
16- Transcript must be text. Input:\n \t transcript_text: { self ._transcript_text } " )
17- elif type (self ._target_fields ) != list :
18- raise TypeError (f"ERROR in LLM() attributes ->\
19- Target fields must be a list. Input:\n \t target_fields: { self ._target_fields } " )
20-
21-
15+ if type (self ._transcript_text ) is not str :
16+ raise TypeError (
17+ f"ERROR in LLM() attributes ->\
18+ Transcript must be text. Input:\n \t transcript_text: { self ._transcript_text } "
19+ )
20+ elif type (self ._target_fields ) is not list :
21+ raise TypeError (
22+ f"ERROR in LLM() attributes ->\
23+ Target fields must be a list. Input:\n \t target_fields: { self ._target_fields } "
24+ )
25+
2226 def build_prompt (self , current_field ):
23- """
24- This method is in charge of the prompt engineering. It creates a specific prompt for each target field.
25- @params: current_field -> represents the current element of the json that is being prompted.
27+ """
28+ This method is in charge of the prompt engineering. It creates a specific prompt for each target field.
29+ @params: current_field -> represents the current element of the json that is being prompted.
2630 """
2731 prompt = f"""
2832 SYSTEM PROMPT:
@@ -52,7 +56,7 @@ def main_loop(self):
5256 payload = {
5357 "model" : "mistral" ,
5458 "prompt" : prompt ,
55- "stream" : False # don't really know why --> look into this later.
59+ "stream" : False , # don't really know why --> look into this later.
5660 }
5761
5862 try :
@@ -68,11 +72,10 @@ def main_loop(self):
6872
6973 # parse response
7074 json_data = response .json ()
71- parsed_response = json_data [' response' ]
75+ parsed_response = json_data [" response" ]
7276 # print(parsed_response)
7377 self .add_response_to_json (field , parsed_response )
7478
75-
7679 print ("----------------------------------" )
7780 print ("\t [LOG] Resulting JSON created from the input text:" )
7881 print (json .dumps (self ._json , indent = 2 ))
@@ -81,52 +84,52 @@ def main_loop(self):
8184 return self
8285
8386 def add_response_to_json (self , field , value ):
84- """
85- this method adds the following value under the specified field,
86- or under a new field if the field doesn't exist, to the json dict
8787 """
88- value = value .strip ().replace ('"' , '' )
88+ this method adds the following value under the specified field,
89+ or under a new field if the field doesn't exist, to the json dict
90+ """
91+ value = value .strip ().replace ('"' , "" )
8992 parsed_value = None
90- plural = False
91-
93+
9294 if value != "-1" :
93- parsed_value = value
94-
95+ parsed_value = value
96+
9597 if ";" in value :
9698 parsed_value = self .handle_plural_values (value )
97- plural = True
98-
9999
100100 if field in self ._json .keys ():
101101 self ._json [field ].append (parsed_value )
102- else :
102+ else :
103103 self ._json [field ] = parsed_value
104-
104+
105105 return
106106
107107 def handle_plural_values (self , plural_value ):
108- """
109- This method handles plural values.
110- Takes in strings of the form 'value1; value2; value3; ...; valueN'
111- returns a list with the respective values -> [value1, value2, value3, ..., valueN]
108+ """
109+ This method handles plural values.
110+ Takes in strings of the form 'value1; value2; value3; ...; valueN'
111+ returns a list with the respective values -> [value1, value2, value3, ..., valueN]
112112 """
113113 if ";" not in plural_value :
114- raise ValueError (f"Value is not plural, doesn't have ; separator, Value: { plural_value } " )
115-
116- print (f"\t [LOG]: Formating plural values for JSON, [For input { plural_value } ]..." )
114+ raise ValueError (
115+ f"Value is not plural, doesn't have ; separator, Value: { plural_value } "
116+ )
117+
118+ print (
119+ f"\t [LOG]: Formating plural values for JSON, [For input { plural_value } ]..."
120+ )
117121 values = plural_value .split (";" )
118-
122+
119123 # Remove trailing leading whitespace
120124 for i in range (len (values )):
121- current = i + 1
125+ current = i + 1
122126 if current < len (values ):
123127 clean_value = values [current ].lstrip ()
124128 values [current ] = clean_value
125129
126130 print (f"\t [LOG]: Resulting formatted list of values: { values } " )
127-
131+
128132 return values
129-
130133
131134 def get_data (self ):
132135 return self ._json
0 commit comments