Skip to content

Commit

Permalink
feat: update knowledge
Browse files Browse the repository at this point in the history
  • Loading branch information
fibonacci998 committed Oct 31, 2024
1 parent 810d4af commit 46775e4
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 38 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Depip agent
## 1. Prequisite
- Miniconda
- Python3
## 2. Install
```sh
conda env create --file environment.yml
conda activate depip-agent
pip install -r requirements.txt
```
## 3. Start app
```sh
fastapi run app/main.py
```
6 changes: 3 additions & 3 deletions app/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def __init__(self, embedding_model: BedrockEmbedding, llm_model: BedrockLLM) ->
"programmable_ip_license",
"Searches and returns knowledge from the programmable ip license or story protocol",
)

self.agent_executor = create_react_agent(llm_model.llm, tools=[tool], checkpointer=memory,
state_modifier=SystemMessage("Your name always is Depip. I provided knowledge for you about story protocol and programmable ip license. Help human answer their questions"))
instruction = "You are Depip, an friendly agent has knowledge about story protocol and programmable ip license. Help human answer their questions about this context only."
self.agent_executor = create_react_agent(llm_model.llm, tools=[tool],
checkpointer=memory, state_modifier=instruction)

def invoke(self, query: str, session_id: str):
config = {
Expand Down
9 changes: 9 additions & 0 deletions app/embedding_model/bedrock_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from depip_agent.app.utils.boto3_client import Boto3Client
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.vectorstores import InMemoryVectorStore, VectorStoreRetriever
from langchain_community.vectorstores import FAISS
class BedrockEmbedding:
def __init__(self) -> None:
boto3Client = Boto3Client()
Expand All @@ -23,3 +24,11 @@ def createEmbeddingPDF(self):

self.retriever = vectorstore.as_retriever()
print("--- %s seconds to create embedding ---" % (time.time() - start_time))

def loadVectorStore(self):
print('load vector store')
start_time = time.time()
faiss_index_path = "app/knowledge/faiss_index"
loaded_vectorstore = FAISS.load_local(folder_path=faiss_index_path, embeddings=self.embedding_model, allow_dangerous_deserialization=True)
self.retriever = loaded_vectorstore.as_retriever()
print("--- %s seconds to load vector store ---" % (time.time() - start_time))
Binary file added app/knowledge/faiss_index/index.faiss
Binary file not shown.
Binary file added app/knowledge/faiss_index/index.pkl
Binary file not shown.
64 changes: 32 additions & 32 deletions app/llm_model/bedrock_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@
from langchain_core.vectorstores import VectorStoreRetriever
class BedrockLLM:

system_prompt = (
"You are an assistant for question-answering tasks. "
"Use the following pieces of retrieved context to answer "
"the question. If you don't know the answer, say that you "
"don't know. Use three sentences maximum and keep the "
"answer concise."
"\n\n"
"{context}"
)
contextualize_q_system_prompt = (
"Given a chat history and the latest user question "
"which might reference context in the chat history, "
"formulate a standalone question which can be understood "
"without the chat history. Do NOT answer the question, "
"just reformulate it if needed and otherwise return it as is."
)
prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
("human", "{input}"),
]
)
# system_prompt = (
# "You are an assistant for question-answering tasks. "
# "Use the following pieces of retrieved context to answer "
# "the question. If you don't know the answer, say that you "
# "don't know. Use three sentences maximum and keep the "
# "answer concise."
# "\n\n"
# "{context}"
# )
# contextualize_q_system_prompt = (
# "Given a chat history and the latest user question "
# "which might reference context in the chat history, "
# "formulate a standalone question which can be understood "
# "without the chat history. Do NOT answer the question, "
# "just reformulate it if needed and otherwise return it as is."
# )
# prompt = ChatPromptTemplate.from_messages(
# [
# ("system", system_prompt),
# ("human", "{input}"),
# ]
# )


def __init__(self, retriever: VectorStoreRetriever) -> None:
boto3Client = Boto3Client()

self.llm = ChatBedrock(client=boto3Client.client, model_id='anthropic.claude-3-sonnet-20240229-v1:0')
question_answer_chain = create_stuff_documents_chain(self.llm, self.prompt)
contextualize_q_prompt = ChatPromptTemplate.from_messages(
[
("system", self.contextualize_q_system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
history_aware_retriever = create_history_aware_retriever(self.llm, retriever, contextualize_q_prompt)
self.rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
# question_answer_chain = create_stuff_documents_chain(self.llm, self.prompt)
# contextualize_q_prompt = ChatPromptTemplate.from_messages(
# [
# ("system", self.contextualize_q_system_prompt),
# MessagesPlaceholder("chat_history"),
# ("human", "{input}"),
# ]
# )
# history_aware_retriever = create_history_aware_retriever(self.llm, retriever, contextualize_q_prompt)
# self.rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)

3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from depip_agent.app.dto import invoke_input

embedding_model = BedrockEmbedding()
embedding_model.createEmbeddingPDF()
# embedding_model.createEmbeddingPDF()
embedding_model.loadVectorStore()
llm_model = BedrockLLM(embedding_model.retriever)
agent = Agent(embedding_model=embedding_model, llm_model=llm_model)

Expand Down
4 changes: 2 additions & 2 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: depip_agent
name: depip-agent
channels:
- defaults
dependencies:
- python
- python==3.12.1
- pip:
- -r file:requirements.txt
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ click==8.1.7
dataclasses-json==0.6.7
dnspython==2.7.0
email_validator==2.2.0
faiss-cpu==1.9.0
fastapi==0.115.4
fastapi-cli==0.0.5
frozenlist==1.5.0
Expand Down

0 comments on commit 46775e4

Please sign in to comment.