MCPcopy
hub / github.com/llmware-ai/llmware / invoice_processing

Function invoice_processing

solutions/use_cases/invoice_processing.py:26–96  ·  view source on GitHub ↗
(run_on_cpu=True)

Source from the content-addressed store, hash-verified

24
25
26def invoice_processing(run_on_cpu=True):
27
28 # Step 1 - Pull down the sample files from S3 through the .load_sample_files() command
29 # --note: if you need to refresh the sample files, set 'over_write=True'
30 print("update: Downloading Sample Files")
31
32 sample_files_path = Setup().load_sample_files(over_write=False)
33 invoices_path = os.path.join(sample_files_path, "Invoices")
34
35 # Step 2 - simple sample query list - each question will be asked to each invoice
36 query_list = ["What is the total amount of the invoice?",
37 "What is the invoice number?",
38 "What are the names of the two parties?"]
39
40 # Step 3 - Load Model
41
42 if run_on_cpu:
43
44 # load local bling model that can run on cpu/laptop
45
46 # note: bling-1b-0.1 is the *fastest* & *smallest*, but will make more errors than larger BLING models
47 # model_name = "llmware/bling-1b-0.1"
48
49 # try the new bling-phi-3 quantized with gguf - most accurate
50 model_name = 'bling-phi-3-gguf'
51 else:
52
53 # use GPU-based inference server to process
54 # *** see the launch_llmware_inference_server.py example script to setup ***
55
56 server_uri_string = "http://11.123.456.789:8088" # insert your server_uri_string
57 server_secret_key = "demo-test"
58 ModelCatalog().setup_custom_llmware_inference_server(server_uri_string, secret_key=server_secret_key)
59 model_name = "llmware-inference-server"
60
61 # attach inference server to prompt object
62 prompter = Prompt().load_model(model_name)
63
64 # Step 4 - main loop thru folder of invoices
65
66 for i, invoice in enumerate(os.listdir(invoices_path)):
67
68 # just in case (legacy on mac os file system - not needed on linux or windows)
69 if invoice != ".DS_Store":
70
71 print("\nAnalyzing invoice: ", str(i + 1), invoice)
72
73 for question in query_list:
74
75 # Step 4A - parses the invoices in memory and attaches as a source to the Prompt
76 source = prompter.add_source_document(invoices_path,invoice)
77
78 # Step 4B - executes the prompt on the LLM (with the loaded source)
79 output = prompter.prompt_with_source(question,prompt_name="default_with_context")
80
81 for i, response in enumerate(output):
82 print("LLM Response - ", question, " - ", re.sub("[\n]"," ", response["llm_response"]))
83

Callers 1

Calls 13

SetupClass · 0.90
ModelCatalogClass · 0.90
PromptClass · 0.90
HumanInTheLoopClass · 0.90
load_sample_filesMethod · 0.80
add_source_documentMethod · 0.80
prompt_with_sourceMethod · 0.80
get_prompt_pathMethod · 0.80
load_modelMethod · 0.45

Tested by

no test coverage detected