Skip to content

Commit 96738ea

Browse files
DARREN OBERSTDARREN OBERST
DARREN OBERST
authored and
DARREN OBERST
committed
updating slim examples
1 parent ef128df commit 96738ea

6 files changed

+481
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
"""This example demonstrates the use of LLM function calls to perform document clustering and
3+
automated classification of different parts of a document. """
4+
5+
from llmware.parsers import Parser
6+
from llmware.agents import LLMfx
7+
from llmware.setup import Setup
8+
9+
import os
10+
11+
12+
def document_clustering_example ():
13+
14+
samples_fp = Setup().load_sample_files(over_write=True)
15+
agreements_fp = os.path.join(samples_fp, "Agreements")
16+
agreement_files = os.listdir(agreements_fp)
17+
18+
if len(agreement_files) == 0:
19+
print("something went wrong")
20+
return -1
21+
22+
# parsing the first file (could be random) found in the os.listdir in the Agreements sample folder
23+
contract_chunks = Parser().parse_one_pdf(agreements_fp,agreement_files[0])
24+
25+
# create a LLMfx object
26+
agent = LLMfx()
27+
28+
# there are ~65-70 contract_chunks in ~15 page contract - feel free to slice (faster demo), or the whole thing
29+
agent.load_work(contract_chunks[0:5])
30+
31+
agent.load_tool_list(["topics","tags", "ner"])
32+
33+
while True:
34+
agent.exec_multitool_function_call(["topics", "tags","ner"])
35+
36+
if not agent.increment_work_iteration():
37+
break
38+
39+
agent.show_report()
40+
41+
agent.activity_summary()
42+
43+
# uncomment this to see a full view of all of the responses
44+
"""
45+
for i, entries in enumerate(agent.response_list):
46+
print("response_list: ", i, entries)
47+
"""
48+
49+
return agent.response_list
50+
51+
52+
if __name__ == "__main__":
53+
54+
analysis= document_clustering_example()
55+
56+
57+

examples/SLIM-Agents/ner-retrieval.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
""" This example illustrates a common two-step retrieval pattern using a SLIM NER model:
3+
4+
Step 1: Extract named entity information from a text. In this case, the name of a musician.
5+
Step 2: Use the extracted name information as the basis for a retrieval. In this case, we will use the
6+
extracted named entities to do a lookup in Wikipedia. """
7+
8+
from llmware.agents import LLMfx
9+
from llmware.parsers import WikiParser
10+
11+
12+
def ner_lookup_retrieval():
13+
14+
text = ("The new Miko Marks album is one of the best I have ever heard in a number of years. "
15+
"She is definitely an artist worth exploring further.")
16+
17+
# create agent
18+
agent = LLMfx()
19+
agent.load_work(text)
20+
agent.load_tool("ner")
21+
named_entities = agent.ner()
22+
ner_dict= named_entities["llm_response"]
23+
24+
# take named entities found and package into a lookup list
25+
26+
lookup = []
27+
for keys, value in ner_dict.items():
28+
if value:
29+
lookup.append(value)
30+
31+
for entries in lookup:
32+
33+
# run a wiki topic query with each of the named entities found
34+
35+
wiki_info = WikiParser().add_wiki_topic(entries, target_results=1)
36+
37+
print("update: wiki_info - ", wiki_info)
38+
summary = wiki_info["articles"][0]["summary"]
39+
40+
print("update: summary - ", summary)
41+
42+
return 0
43+
44+
45+
if __name__ == "__main__":
46+
47+
ner_lookup_retrieval()
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
""" Sentiment Analysis example - shows how to use the slim-sentiment-tool. In this example, we will:
3+
4+
1. Review several summary earnings transcripts, looking to evaluate the overall sentiment as
5+
'positive', 'negative', or 'neutral'
6+
7+
2. Evaluate a single transcript, and apply if...then based on the result and confidence level.
8+
9+
3. Run through a list of earnings transcripts with journaling activated to display the multi-step
10+
process on the screen.
11+
"""
12+
13+
from llmware.agents import LLMfx
14+
15+
earnings_transcripts = [
16+
"This is one of the best quarters we can remember for the industrial sector with significant growth across the "
17+
"board in new order volume, as well as price increases in excess of inflation. We continue to see very strong "
18+
"demand, especially in Asia and Europe. Accordingly, we remain bullish on the tier 1 suppliers and would be "
19+
"accumulating more stock on any dips. ",
20+
21+
"Not the worst results, but overall we view as negative signals on the direction of the economy, and the likely "
22+
"short-term trajectory for the telecom sector, and especially larger market leaders, including AT&T, Comcast, and"
23+
"Deutsche Telekom.",
24+
25+
"This quarter was a disaster for Tesla, with falling order volume, increased costs and supply, and negative "
26+
"guidance for future growth forecasts in 2024 and beyond.",
27+
28+
"On balance, this was an average result, with earnings in line with expectations and no big surprises to either "
29+
"the positive or the negative."
30+
]
31+
32+
33+
def get_one_sentiment_classification(text):
34+
35+
"""This example shows a basic use to get a sentiment classification and use the output programmatically. """
36+
37+
# simple basic use to get the sentiment on a single piece of text
38+
agent = LLMfx(verbose=True)
39+
agent.load_tool("sentiment")
40+
sentiment = agent.sentiment(text)
41+
42+
# look at the output
43+
print("sentiment: ", sentiment)
44+
for keys, values in sentiment.items():
45+
print(f"{keys}-{values}")
46+
47+
# two key attributes of the sentiment output dictionary
48+
sentiment_value = sentiment["llm_response"]["sentiment"]
49+
confidence_level = sentiment["confidence_score"]
50+
51+
# use the sentiment classification as a 'if...then' decision point in a process
52+
if "positive" in sentiment_value:
53+
print("sentiment is positive .... will take 'positive' analysis path ...", sentiment_value)
54+
55+
if "positive" in sentiment_value and confidence_level > 0.8:
56+
print("sentiment is positive with high confidence ... ", sentiment_value, confidence_level)
57+
58+
return sentiment
59+
60+
61+
def review_batch_earning_transcripts():
62+
63+
""" This example highlights how to review multiple earnings transcripts and iterate through a batch
64+
using the load_work mechanism. """
65+
66+
agent = LLMfx()
67+
agent.load_tool("sentiment")
68+
69+
# iterating through a larger list of samples
70+
# note: load_work method is a flexible input mechanism - pass a string, list, dictionary or combination, and
71+
# it will 'package' as iterable units of processing work for the agent
72+
73+
agent.load_work(earnings_transcripts)
74+
75+
while True:
76+
output = agent.sentiment()
77+
# print("update: test - output - ", output)
78+
if not agent.increment_work_iteration():
79+
break
80+
81+
response_output = agent.response_list
82+
83+
agent.clear_work()
84+
agent.clear_state()
85+
86+
return response_output
87+
88+
89+
if __name__ == "__main__":
90+
91+
# first - quick illustration of getting a sentiment classification
92+
# and using in an "if...then"
93+
sentiment = get_one_sentiment_classification(earnings_transcripts[0])
94+
95+
# second - iterate thru a batch of transcripts and apply a sentiment classification
96+
# response_output = review_batch_earning_transcripts()
97+
98+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
2+
3+
""" This example shows an end-to-end recipe for querying SQL database using only natural language.
4+
5+
The example shows the following steps:
6+
7+
1. Loading "slim-sql-tool" and running initial tests to confirm installation.
8+
2. Generating a SQL table from a sample CSV file included with the slim-sql-tool install.
9+
3. Asking basic natural language questions:
10+
A. Looks up the table schema
11+
B. Packages the table schema with query
12+
C. Runs inference to convert text into SQL
13+
D. Queries the database with the generated SQL
14+
E. Returns result
15+
3. All work performed on an integrated 'llmware-sqlite-experimental.db' that can be deleted safely anytime
16+
as part of experimentation lifecycle.
17+
18+
"""
19+
20+
import os
21+
22+
from llmware.agents import SQLTables, LLMfx
23+
from llmware.models import ModelCatalog
24+
from llmware.configs import LLMWareConfig
25+
26+
27+
def sql_e2e_test_script(table_name="customers1",create_new_table=False):
28+
29+
""" This is the end-to-end execution script. """
30+
31+
# create table if needed to set up
32+
if create_new_table:
33+
34+
# looks to pull sample csv 'customer_table.csv' from slim-sql-tool model package files
35+
sql_tool_repo_path = os.path.join(LLMWareConfig().get_model_repo_path(), "slim-sql-tool")
36+
37+
if not os.path.exists(sql_tool_repo_path):
38+
ModelCatalog().load_model("llmware/slim-sql-tool")
39+
40+
files = os.listdir(sql_tool_repo_path)
41+
csv_file = "customer_table.csv"
42+
43+
if csv_file in files:
44+
45+
# to create a testing table from a csv
46+
sql_db = SQLTables(experimental=True)
47+
sql_db.create_new_table_from_csv(sql_tool_repo_path, csv_file, table_name=table_name)
48+
# end - creating table
49+
50+
print("update: successfully created new db table")
51+
else:
52+
print("something has gone wrong - could not find customer_table.csv inside the slim-sql-tool file package")
53+
54+
# query starts here
55+
agent = LLMfx()
56+
agent.load_tool("sql")
57+
58+
# Pass direct queries to the DB
59+
60+
query_list = ["Which customers are vip customers?",
61+
"What is the highest annual spend of any customer?",
62+
"Which customer has account number 1234953",
63+
"Which customer has the lowest annual spend?",
64+
"Is Susan Soinsin a vip customer?"]
65+
66+
for i, query in enumerate(query_list):
67+
68+
# query_db method is doing all of the work
69+
# -- looks up the table schema in the db using the table_name
70+
# -- packages the text-2-sql query prompt
71+
# -- executes sql method to convert the prompt into a sql query
72+
# -- attempts to execute the sql query on the db
73+
# -- returns the db results as 'research' output
74+
75+
response = agent.query_db(query, table=table_name)
76+
77+
for x in range(0,len(agent.research_list)):
78+
print("research: ", x, agent.research_list[x])
79+
80+
return 0
81+
82+
def delete_table(table_name):
83+
84+
""" Start fresh in testing - delete table in experimental local SQLite DB """
85+
86+
sql_db = SQLTables(experimental=True)
87+
sql_db.delete_table(table_name, confirm_delete=True)
88+
89+
return True
90+
91+
92+
def delete_db():
93+
94+
""" Start fresh in testing - deletes SQLite DB and starts over. """
95+
96+
sql_db = SQLTables(experimental=True)
97+
sql_db.delete_experimental_db(confirm_delete=True)
98+
99+
return True
100+
101+
102+
if __name__ == "__main__":
103+
104+
ModelCatalog().get_llm_toolkit()
105+
106+
# run an end-to-end test
107+
sql_e2e_test_script(table_name="customer1",create_new_table=True)
108+
109+
# third - delete and start fresh for further testing
110+
delete_table("customer1")
111+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
""" This 'getting started' example shows the basics of how to start using text2sql model:
3+
4+
1. Loading "slim-sql-tool" and running initial tests to confirm installation.
5+
6+
2. 'Hello World' demonstration of how to 'package' a text2sql prompt combining a
7+
natural language query with a SQL table schema and run a basic inference to generate SQL output
8+
9+
"""
10+
11+
12+
from llmware.agents import LLMfx
13+
from llmware.models import ModelCatalog
14+
15+
16+
def load_slim_sql_tool():
17+
18+
""" First step is to install the slim-sql-tool locally """
19+
20+
# to cache locally the slim-sql-tool with config and test files
21+
ModelCatalog().get_llm_toolkit(["sql"])
22+
23+
# to run tests to confirm correct installation and see the model in action
24+
# note: the test results will include some minor errors - useful to learn how to sharpen prompts
25+
ModelCatalog().tool_test_run("slim-sql-tool")
26+
27+
return 0
28+
29+
30+
def hello_world_text_2_sql():
31+
32+
""" Illustrates a 'hello world' text-2-sql inference as part of an agent process. """
33+
34+
sample_table_schema = "CREATE TABLE customer_info (customer_name text, account_number integer, annual_spend integer)"
35+
36+
query = "What are the names of all customers with annual spend greater than $1000?"
37+
38+
agent = LLMfx(verbose=True)
39+
response = agent.sql(query, sample_table_schema)
40+
41+
print("update: text-2-sql response - ", response)
42+
43+
return response
44+
45+
46+
if __name__ == "__main__":
47+
48+
# first - load and test the tools
49+
load_slim_sql_tool()
50+
51+
# second - 'hello world' demo of using text2sql model
52+
hello_world_text_2_sql()
53+

0 commit comments

Comments
 (0)