forked from llmware-ai/llmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt_with_sources.py
68 lines (45 loc) · 2.61 KB
/
prompt_with_sources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
""""This example demonstrates:
prompt_with_sources - powerful abstraction to integrate various knowledge sources into a prompt
"""
import os
from llmware.prompts import Prompt
from llmware.setup import Setup
from llmware.models import PromptCatalog
from llmware.library import Library
from llmware.retrieval import Query
def prompt_with_sources(model_name, library_name):
print(f"Example - prompt_with_sources - attaching several different knowledge sources to a Prompt directly.")
library = Library().create_new_library(library_name)
sample_files_path = Setup().load_sample_files(over_write=False)
ingestion_folder_path = os.path.join(sample_files_path, "Agreements")
parsing_output = library.add_files(ingestion_folder_path)
local_file = "Apollo EXECUTIVE EMPLOYMENT AGREEMENT.pdf"
prompter = Prompt().load_model(model_name)
sources2 = prompter.add_source_document(ingestion_folder_path, local_file, query="base salary")
prompt = "What is the base salary amount?"
prompt_instruction="default_with_context"
response = prompter.prompt_with_source(prompt=prompt, prompt_name=prompt_instruction)[0]["llm_response"]
print (f"- Context: {local_file}\n- Prompt: {prompt}\n- LLM Response:\n{response}")
prompter.clear_source_materials()
prompt = "Was Barack Obama the Prime Minister of Canada?"
wiki_topic = "Barack Obama"
prompt_instruction = "yes_no"
sources3 = prompter.add_source_wikipedia(wiki_topic, article_count=1)
response = prompter.prompt_with_source(prompt=prompt, prompt_name=prompt_instruction)[0]["llm_response"]
print (f"- Context: {local_file}\n- Prompt: {prompt}\n- LLM Response:\n{response}")
prompter.clear_source_materials()
query_results = Query(library).text_query("base salary")
prompt = "What is the annual rate of the base salary?"
sources4 = prompter.add_source_query_results(query_results)
response = prompter.prompt_with_source(prompt=prompt, prompt_name=prompt_instruction)[0]["llm_response"]
print(f"- Context: {local_file}\n- Prompt: {prompt}\n- LLM Response:\n{response}")
prompter.clear_source_materials()
return 0
if __name__ == "__main__":
# to use API-based model for this example, set API keys in os.environ variable
# e.g., see example: set_model_api_keys.py
# e.g., os.environ["USER_MANAGED_OPENAI_API_KEY"] = "<insert-your-api-key>"
# this model is a placeholder which will run on local laptop - swap out for higher accuracy, larger models
model_name = "llmware/bling-1b-0.1"
library_name = "lib_prompt_with_sources_1"
prompt_with_sources(model_name,library_name)