-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
107 lines (83 loc) · 3.23 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#pip install --upgrade langchain langchain-community langchainhub gpt4all langchain-openai chromadb bs4 sentence-transformers
# pip install jq
# install ollama
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from pprint import pprint
from langchain_core.prompts import PromptTemplate
import functions
import os
from dotenv import load_dotenv
load_dotenv()
# USE: Function to load data from a web source
def loadData():
# Load data from JSON.
# two examples provided,
# Formation1_Mod.json contains json formar pertaining to real use case
# geo_E2.json is a simpler generic example
data_load = functions.jsonLoader("JSON/Formation_JSON/Formation1_Mod.json", ".hits[].hit", ".page_content")
#data_load = functions.jsonLoader("JSON/Generic_JSON/geo_Mod.json", ".features[]", ".properties")
# Load data from a web source using a custom function
#data_load = functions.webLoader("https://www.jillesvangurp.com")
return data_load
# Set up LLM
def loadLLM():
llm = functions.setLlama2()
#llm = functions.setGemma()
#llm = functions.setGPT4()
return llm
# Set up Embedding function
def loadEmbedding():
embedding_function = functions.setEmbeddingMini()
#embedding_function = functions.setEmbeddingOpenAI()
return embedding_function
# Set Question to be asked
def loadQuestion():
question = "What is Formation GmbH?"
return question
# Template for generating prompts
template = """Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
Try and keep the answer direct and consise
Always say "thanks for asking!" or something similar to be friendly at the end of the answer.
{context}
Question: {question}
Answer:"""
# Create a prompt template from the provided template
prompt_template= PromptTemplate.from_template(template=template)
llm = loadLLM()
embedding_function = loadEmbedding()
# Load data
data_load = loadData()
# Split data
data = functions.splitData(data_load)
# Create vector store
vectorstore = functions.createVectorStore(data, embedding_function)
# question
question= loadQuestion()
# Perform similarity search
# param:
# vectorstore: send in vectorstore
# k : retrives the top k results of a given query after similarity search
docs = functions.similaritySearch(vectorstore=vectorstore,k = 4)
# TEST: Print the documents retrieved after similarity search on the
#pprint(docs.invoke(question))
# Chain of operations for generating a response
chain = (
{"context": docs | functions.format_docs, "question": RunnablePassthrough()}
| prompt_template
| llm
| StrOutputParser()
)
# Ollama framework and OPENAI's framework have slightly different methods of printing.
# Ollama prints automatically, OPENAI needs print to be called
# This try and catch block prevents Ollama's results to be printed twice.
try:
if llm.model_name == "gpt-4":
print(chain.invoke(question))
else:
chain.invoke(question)
except AttributeError:
chain.invoke(question)