-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt.py
76 lines (59 loc) · 2.42 KB
/
chatgpt.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
import os
import sys
import openai
from langchain.chains import ConversationalRetrievalChain, RetrievalQA
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import DirectoryLoader, TextLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.indexes import VectorstoreIndexCreator
from langchain.indexes.vectorstore import VectorStoreIndexWrapper
from langchain.llms import OpenAI
from langchain.vectorstores import Chroma
from settings import APIKEY
# Declare global variables
chain = None
chat_history = []
def init_chatbot():
global chain, chat_history
api_key = APIKEY
if not api_key:
raise ValueError("API key environment variable not set.")
os.environ["OPENAI_API_KEY"] = api_key
PERSIST = False
if PERSIST and os.path.exists("persist"):
print("Reusing index...\n")
vectorstore = Chroma(persist_directory="persist", embedding_function=OpenAIEmbeddings())
index = VectorStoreIndexWrapper(vectorstore=vectorstore)
else:
loader = TextLoader("data/data.txt") # Uncomment to load from data.txt only
if PERSIST:
index = VectorstoreIndexCreator(vectorstore_kwargs={"persist_directory":"persist"}).from_loaders([loader])
else:
index = VectorstoreIndexCreator().from_loaders([loader])
chain = ConversationalRetrievalChain.from_llm(
llm=ChatOpenAI(model="gpt-3.5-turbo"),
retriever=index.vectorstore.as_retriever(search_kwargs={"k": 1}),
)
chat_history = []
def get_response(user_input):
global chain, chat_history
# Parse and handle context (this is a placeholder, you might need NLP or regex-based extraction methods)
context = "Mozilla extensions"
# Build the refined query
refined_input = f"In the context of {context}, {user_input}"
# Query the model
result = chain({"question": refined_input, "chat_history": chat_history})
# Ensure a valid answer is returned
answer = result.get('answer', "I don't have access to that information")
if not answer.strip():
answer = "I don't have access to that information"
# Update chat history
chat_history.append((user_input, answer))
return answer
if __name__ == "__main__":
init_chatbot()
while True:
user_input = input("Prompt: ")
if user_input in ['quit', 'q', 'exit']:
break
print(get_response(user_input))