Update interim.py
Browse files- interim.py +10 -3
interim.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from langchain_openai import ChatOpenAI
|
|
@@ -6,7 +7,7 @@ from langchain.agents import AgentExecutor, create_openai_tools_agent
|
|
| 6 |
from langchain_core.messages import BaseMessage, HumanMessage
|
| 7 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 8 |
from langchain_experimental.tools import PythonREPLTool
|
| 9 |
-
from langchain_community.document_loaders import DirectoryLoader
|
| 10 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 11 |
from langchain_community.vectorstores import Chroma
|
| 12 |
from langchain.embeddings import HuggingFaceBgeEmbeddings
|
|
@@ -15,10 +16,15 @@ from langchain_core.runnables import RunnablePassthrough
|
|
| 15 |
from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser
|
| 16 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 17 |
from langgraph.graph import StateGraph, END
|
|
|
|
| 18 |
from typing import Annotated, Sequence, TypedDict
|
| 19 |
-
from langchain_core.tools import tool
|
| 20 |
import functools
|
| 21 |
import operator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Load environment variables
|
| 24 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
@@ -47,6 +53,7 @@ def agent_node(state, agent, name):
|
|
| 47 |
|
| 48 |
@tool
|
| 49 |
def RAG(state):
|
|
|
|
| 50 |
st.session_state.outputs.append('-> Calling RAG ->')
|
| 51 |
question = state
|
| 52 |
template = """Answer the question based only on the following context:\n{context}\nQuestion: {question}"""
|
|
@@ -72,7 +79,7 @@ if uploaded_files:
|
|
| 72 |
docs = []
|
| 73 |
for uploaded_file in uploaded_files:
|
| 74 |
content = uploaded_file.read().decode("utf-8")
|
| 75 |
-
docs.append(
|
| 76 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=10, length_function=len)
|
| 77 |
new_docs = text_splitter.split_documents(documents=docs)
|
| 78 |
embeddings = HuggingFaceBgeEmbeddings(model_name="BAAI/bge-base-en-v1.5", model_kwargs={'device': 'cpu'}, encode_kwargs={'normalize_embeddings': True})
|
|
|
|
| 1 |
import os
|
| 2 |
+
import chromadb
|
| 3 |
import streamlit as st
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from langchain_openai import ChatOpenAI
|
|
|
|
| 7 |
from langchain_core.messages import BaseMessage, HumanMessage
|
| 8 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 9 |
from langchain_experimental.tools import PythonREPLTool
|
| 10 |
+
from langchain_community.document_loaders import DirectoryLoader
|
| 11 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 12 |
from langchain_community.vectorstores import Chroma
|
| 13 |
from langchain.embeddings import HuggingFaceBgeEmbeddings
|
|
|
|
| 16 |
from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser
|
| 17 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 18 |
from langgraph.graph import StateGraph, END
|
| 19 |
+
from langchain_core.documents import Document
|
| 20 |
from typing import Annotated, Sequence, TypedDict
|
|
|
|
| 21 |
import functools
|
| 22 |
import operator
|
| 23 |
+
from langchain_core.tools import tool
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Clear ChromaDB cache to fix tenant issue
|
| 27 |
+
chromadb.api.client.SharedSystemClient.clear_system_cache()
|
| 28 |
|
| 29 |
# Load environment variables
|
| 30 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 53 |
|
| 54 |
@tool
|
| 55 |
def RAG(state):
|
| 56 |
+
"""Use this tool to execute RAG. If the question is related to Japan or Sports, this tool retrieves the results."""
|
| 57 |
st.session_state.outputs.append('-> Calling RAG ->')
|
| 58 |
question = state
|
| 59 |
template = """Answer the question based only on the following context:\n{context}\nQuestion: {question}"""
|
|
|
|
| 79 |
docs = []
|
| 80 |
for uploaded_file in uploaded_files:
|
| 81 |
content = uploaded_file.read().decode("utf-8")
|
| 82 |
+
docs.append(Document(page_content=content, metadata={"name": uploaded_file.name}))
|
| 83 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=10, length_function=len)
|
| 84 |
new_docs = text_splitter.split_documents(documents=docs)
|
| 85 |
embeddings = HuggingFaceBgeEmbeddings(model_name="BAAI/bge-base-en-v1.5", model_kwargs={'device': 'cpu'}, encode_kwargs={'normalize_embeddings': True})
|