Skip to content

Commit

Permalink
adding new slim models to catalog and supporting methods
Browse files Browse the repository at this point in the history
  • Loading branch information
DARREN OBERST authored and DARREN OBERST committed Mar 22, 2024
1 parent 9412bb1 commit bb7ef78
Show file tree
Hide file tree
Showing 3 changed files with 575 additions and 150 deletions.
95 changes: 88 additions & 7 deletions llmware/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ def analyze_responses(self, key,value):

def load_tool(self, tool_type,
# new options added
use_gpu=True, sample=True, get_logits=False,
max_output=100, temperature=-99):
use_gpu=True, sample=False, get_logits=True,
max_output=100, temperature=0.0):

""" Loads a single tool """

Expand Down Expand Up @@ -442,11 +442,14 @@ def unload_tool(self, tool_type):
self.write_to_journal(journal_update)

model = getattr(self, tool_type + "_model")
model.unload_model()

delattr(self, tool_type + "_model")
setattr(self, tool_type + "_model", None)
gc.collect()
if model:

model.unload_model()

delattr(self, tool_type + "_model")
setattr(self, tool_type + "_model", None)
gc.collect()

return 0

Expand Down Expand Up @@ -513,6 +516,10 @@ def exec_function_call(self, tool_type, text=None, function="classify", params=N
dict_output = True
self.report[work_iter] = self.report[work_iter] | response["llm_response"]

elif response["usage"]["type"] == "list" and tool_type == "summary":
dict_output = True
self.report[work_iter] = self.report[work_iter] | {"summary": response["llm_response"]}

else:
logging.warning("update: could not automatically convert to dictionary - "
"keeping as string output")
Expand Down Expand Up @@ -745,6 +752,79 @@ def category(self, text=None, params=None):

return self.exec_function_call("category", text=text, params=params)

def sa_ner(self, text=None, params=None):

""" Generates a dictionary with keys corresponding to 'sentiment' and 'named entity recognition' (NER)
identifiers in the next, such as people, organization, and place. """

if not params:
# default parameter key
params = ["sentiment, people, organization, place"]

if isinstance(params, str):
params = [params]

return self.exec_function_call("sa-ner", text=text, params=params)

def extract(self, text=None, params=None):

""" Extract receives an input of a text passage and a custom parameter key, and generates a dictionary with
key corresponding to the 'custom parameter' key and a list of values associated with that key, extracted from
the text passage. """

if not params:
# default parameter key
params = ["key points"]

if isinstance(params, str):
params = [params]

return self.exec_function_call("extract", text=text, params=params)

def xsum(self, text=None, params=None):

""" XSum or 'extreme summarization' receives an input text passage, and returns a dictionary with a 'xsum'
key and a value of a list with one string element, with the string element consisting of a short phrase,
title, headline that provides a concise summary of the text passage. """

if not params:
# default parameter key
params = ["xsum"]

if isinstance(params, str):
params = [params]

return self.exec_function_call("xsum", text=text, params=params)

def summarize(self, text=None, params=None):

""" Summarizes receives an input text passage, and optional parameters to guide the summarization, and
returns a list of summary points from the text. """

if not params:
# default parameter key
params = ["key points (3)"]

if isinstance(params, str):
params = [params]

return self.exec_function_call("summary", text=text, params=params)

def boolean(self, text=None, params=None):

""" Boolean receives an input text passage, a yes/no question as its parameter, and then returns a
dictionary with two keys - 'answer' and 'explain' with the 'answer' providing a yes/no classification, and the
explanation providing text from the passage that was used as the basis for the classification. """

if not params:
#TODO: what is right way to handle - needs params
params = ["Is this true?"]

if isinstance(params, str):
params = [params]

return self.exec_function_call("boolean", text=text, params=params)

def nli(self, text1, text2, params=None):

""" Executes a natural language inference classification on a text, if passed directly, or will pull current
Expand Down Expand Up @@ -1194,6 +1274,7 @@ def create_new_table(self, output, table_name):
for i, entry in enumerate(header_row):
col_name = re.sub("[\xfe\xff]","",entry)
try:
#TODO: build more robust type checking, e.g., float/decimal/currency
test_int = int(test_row[i])
type="integer"
except:
Expand Down Expand Up @@ -1264,7 +1345,7 @@ def create_new_table_from_csv(self,fp=None, fn=None, table_name=None):
logging.info("update: table created - column names - %s ", column_names)

else:
print("update: table exists - getting column names")
logging.info("update: table exists - getting column names")
column_names = self.get_column_names(table_name)

# insert records
Expand Down
Loading

0 comments on commit bb7ef78

Please sign in to comment.