|
1 | | - |
2 | | -# LLMModel |
| 1 | +#%% |
3 | 2 | import os |
4 | 3 | from langchain.chains import RetrievalQA |
5 | | - |
6 | 4 | from langchain.cache import InMemoryCache |
7 | 5 | from langchain.globals import set_llm_cache |
8 | 6 | from langchain.document_loaders.generic import GenericLoader |
|
16 | 14 | from langchain.memory import ConversationSummaryMemory |
17 | 15 | from langchain.vectorstores import FAISS |
18 | 16 | from langchain.document_loaders.text import TextLoader |
| 17 | +from langchain.agents import AgentType, Tool, initialize_agent |
| 18 | +from langchain.memory import ConversationBufferMemory |
| 19 | +from langchain.agents import AgentExecutor |
| 20 | +# import faiss |
| 21 | +from langchain.vectorstores import FAISS as FAISS |
| 22 | +import faiss |
19 | 23 |
|
20 | | -openai_api_key = os.environ["OPENAI_API_KEY"] |
| 24 | +# Load the OpenAI API key |
21 | 25 |
|
22 | | -# check if the API key is loaded |
| 26 | +openai_api_key = os.environ["OPENAI_API_KEY"] |
23 | 27 | assert openai_api_key is not None, "Failed to load the OpenAI API key from .env file. Please create .env file and add OPENAI_API_KEY = 'your key'" |
24 | 28 |
|
25 | | - |
26 | | - |
27 | | - |
28 | | -llm = ChatOpenAI(model_name='gpt-3.5-turbo',openai_api_key=openai_api_key) # Load the LLM model |
29 | | -# set_llm_cache(InMemoryCache()) |
30 | | - |
31 | | - |
32 | | -embeddings = OpenAIEmbeddings(disallowed_special=(), openai_api_key=openai_api_key) # Load the embeddings |
33 | | -# |
34 | | -# # This is the root directory for the documents i want to create the RAG from |
35 | | -# repo_path = '/Users/zainhazzouri/projects/amos2023ws05-pipeline-config-chat-ai/src/RAG' |
36 | | -# loader = GenericLoader.from_filesystem( |
37 | | -# repo_path, |
38 | | -# glob="**/*", |
39 | | -# suffixes=[".py"], |
40 | | -# parser=LanguageParser(language=Language.PYTHON, parser_threshold=500), |
41 | | -# ) |
42 | | -# documents = loader.load() |
43 | | -# |
44 | | -# python_splitter = RecursiveCharacterTextSplitter.from_language( |
45 | | -# language=Language.PYTHON, chunk_size=2000, chunk_overlap=200 |
46 | | -# ) |
47 | | -# texts = python_splitter.split_documents(documents) |
48 | | -# |
49 | | -# |
50 | | -# db = Chroma.from_documents(texts, OpenAIEmbeddings(disallowed_special=())) |
51 | | -# retriever = db.as_retriever( |
52 | | -# search_type="mmr", # Also test "similarity" |
53 | | -# search_kwargs={"k": 8}, |
54 | | -# ) |
55 | | - |
56 | | -########################################## the old version of RAG |
57 | | -# This is the root directory for the documents i want to create the RAG from |
58 | | -root_dir = os.path.join("..", "RAG") |
59 | | -docs = [] # Create an empty list to store the docs |
60 | | - |
61 | | -# Go through each folder to extract all the files |
62 | | -for dirpath, dirnames, filenames in os.walk(root_dir): |
63 | | - |
64 | | - # Go through each file |
65 | | - for file in filenames: |
66 | | - try: |
67 | | - # Load up the file as a doc and split |
68 | | - loader = TextLoader(os.path.join(dirpath, file), encoding='utf-8') |
69 | | - docs.extend(loader.load_and_split()) |
70 | | - except Exception as e: |
71 | | - pass |
72 | | - |
73 | | -docsearch = FAISS.from_documents(docs, embeddings) # Create the FAISS index |
74 | | -# source https://python.langchain.com/docs/integrations/vectorstores/faiss_async |
75 | | - |
76 | | - |
77 | | -#memory = ConversationSummaryMemory(llm=llm, memory_key="chat_history", return_messages=True) |
78 | | -# add caching to the memory |
79 | | - |
80 | | - |
81 | | -RAG = RetrievalQA.from_chain_type(llm,chain_type="stuff" ,retriever=docsearch.as_retriever()) # the old chain for the retrieval |
| 29 | +# Initialize the language model |
| 30 | +llm = ChatOpenAI(model_name='gpt-3.5-turbo', openai_api_key=openai_api_key) |
| 31 | + |
| 32 | +# Load the embeddings |
| 33 | +embeddings = OpenAIEmbeddings(disallowed_special=(), openai_api_key=openai_api_key) |
| 34 | + |
| 35 | +# # Load and split documents |
| 36 | +# root_dir = '/Users/zainhazzouri/projects/amos2023ws05-pipeline-config-chat-ai/src/RAG/pipelines' |
| 37 | +# docs = [] |
| 38 | +# for dirpath, dirnames, filenames in os.walk(root_dir): |
| 39 | +# for file in filenames: |
| 40 | +# try: |
| 41 | +# loader = TextLoader(os.path.join(dirpath, file), encoding='utf-8') |
| 42 | +# docs.extend(loader.load_and_split()) |
| 43 | +# except Exception as e: |
| 44 | +# pass # Consider logging the exception for debugging |
| 45 | + |
| 46 | +# # Create the FAISS index |
| 47 | +# docsearch = FAISS.from_documents(docs, embeddings) |
| 48 | + |
| 49 | +#%% |
| 50 | +# save the vector store offline for later use |
| 51 | +# faiss.write_index(docsearch.index, '/Users/zainhazzouri/projects/amos2023ws05-pipeline-config-chat-ai/src/ChatUI_streamlit/faiss_index_file') |
| 52 | +# docsearch.save_local("/Users/zainhazzouri/projects/amos2023ws05-pipeline-config-chat-ai/src/ChatUI_streamlit/faiss_index") |
| 53 | + |
| 54 | +#%% |
| 55 | +docsearch = FAISS.load_local("/Users/zainhazzouri/projects/amos2023ws05-pipeline-config-chat-ai/src/ChatUI_streamlit/faiss_index", embeddings) |
| 56 | +#%% |
| 57 | +# Initialize RetrievalQA |
| 58 | +RAG = RetrievalQA.from_chain_type(llm, chain_type="stuff", retriever=docsearch.as_retriever()) |
| 59 | + |
| 60 | +# Define tools |
| 61 | +tools = [ |
| 62 | + Tool( |
| 63 | + name="RTDIP SDK", |
| 64 | + func=RAG.run, |
| 65 | + description="useful for when you need to answer questions about RTDIP", |
| 66 | + ) |
| 67 | +] |
| 68 | + |
| 69 | +# Initialize conversation memory |
| 70 | +conversation_memory = ConversationBufferMemory() |
| 71 | + |
| 72 | +# Initialize Agent with conversation memory |
| 73 | +agent = initialize_agent( |
| 74 | + tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, memory=conversation_memory, handle_parsing_errors=True |
| 75 | +) |
| 76 | + |
| 77 | +# Set the LLM cache |
82 | 78 | set_llm_cache(InMemoryCache()) |
83 | 79 |
|
| 80 | +# Function to update and retrieve conversation context |
| 81 | +def update_and_get_context(user_input, conversation_memory): |
| 82 | + conversation_memory.add_user_input(user_input) |
| 83 | + context = conversation_memory.get_conversation() |
| 84 | + model_input = "\n".join(context + [user_input]) |
| 85 | + return model_input |
84 | 86 |
|
85 | | -#RAG = ConversationalRetrievalChain.from_llm(llm,chain_type="stuff", retriever=docsearch.as_retriever()) # the new chain for the retrieval |
86 | | - |
| 87 | +# Example usage (commented out for testing) |
| 88 | +# user_input = "What's the weather like today?" |
| 89 | +# model_input = update_and_get_context(user_input, conversation_memory) |
| 90 | +# response = llm.run(model_input) |
| 91 | +# print(response) |
87 | 92 |
|
88 | | -##### this code for testing the model don't delete it -- |
| 93 | +# Note: You can uncomment and modify the testing code as per your use case. |
89 | 94 |
|
90 | | -# question1 = " Hello , my name is Zain" |
91 | | -# question2 = " what's my name?" |
92 | | -#question3 = "I would like to use RTDIP components to read from an eventhub using ‘connection string’ as the connection string, and ‘consumer group’ as the consumer group, transform using binary to string, and edge x transformer then write to delta, return only the python code " |
93 | | -# |
94 | | -# result = RAG(question1) |
95 | | -# result["answer"] |
96 | | -# print(result["answer"]) |
97 | | -# |
98 | | -# result = RAG(question2) |
99 | | -# result["answer"] |
100 | | -# print(result["answer"]) |
101 | | -# |
102 | | -#result = RAG(question3) |
103 | | -#result["answer"] |
104 | | -#print(result["answer"]) |
| 95 | +# %% |
0 commit comments