Doux Thibault commited on
Commit
ed250fe
·
1 Parent(s): a431e1d

add personal info

Browse files
Modules/rag.py CHANGED
@@ -23,7 +23,7 @@ from langchain.retrievers import (
23
  from huggingface_hub import login
24
  login(token=os.getenv("HUGGING_FACE_TOKEN"))
25
 
26
- def load_chunk_persist_pdf() -> Chroma:
27
 
28
  pdf_folder_path = os.path.join(os.getcwd(),Path(f"data/pdf/{task}"))
29
  documents = []
@@ -37,12 +37,13 @@ def load_chunk_persist_pdf() -> Chroma:
37
  os.makedirs("data/chroma_store/", exist_ok=True)
38
  vectorstore = Chroma.from_documents(
39
  documents=chunked_documents,
40
- embedding=MistralAIEmbeddings(api_key=mistral_api_key),
41
  persist_directory= os.path.join(os.getcwd(),Path("data/chroma_store/"))
42
  )
43
  vectorstore.persist()
44
  return vectorstore
45
 
 
46
  zero2hero_vectorstore = load_chunk_persist_pdf("zero2hero")
47
  bodyweight_vectorstore = load_chunk_persist_pdf("bodyweight")
48
  nutrition_vectorstore = load_chunk_persist_pdf("nutrition")
@@ -51,13 +52,14 @@ zero2hero_retriever = zero2hero_vectorstore.as_retriever()
51
  nutrition_retriever = nutrition_vectorstore.as_retriever()
52
  bodyweight_retriever = bodyweight_vectorstore.as_retriever()
53
  workout_retriever = workout_vectorstore.as_retriever()
 
54
 
55
  llm = ChatMistralAI(model="mistral-large-latest", mistral_api_key=mistral_api_key, temperature=0)
56
 
57
  prompt = ChatPromptTemplate.from_template(
58
  """
59
  You are a professional AI coach specialized in fitness, bodybuilding and nutrition.
60
- You must adapt to the user : if he is a beginner, use simple words. You are gentle and motivative.
61
  Use the following pieces of retrieved context to answer the question.
62
  If you don't know the answer, use your common knowledge.
63
  Use three sentences maximum and keep the answer concise.
@@ -73,7 +75,7 @@ prompt = ChatPromptTemplate.from_template(
73
  def format_docs(docs):
74
  return "\n\n".join(doc.page_content for doc in docs)
75
 
76
- retriever = MergerRetriever(retrievers=[zero2hero_retriever, bodyweight_retriever, nutrition_retriever, workout_retriever])
77
 
78
  rag_chain = (
79
  {"context": retriever | format_docs, "question": RunnablePassthrough()}
@@ -84,6 +86,6 @@ rag_chain = (
84
 
85
 
86
 
87
- print(rag_chain.invoke("What supplement could i buy to improve my sleep?"))
88
 
89
  # print(rag_chain.invoke("I am a 45 years old woman and I have to loose weight for the summer. Provide me with a fitness program, and a nutrition program"))
 
23
  from huggingface_hub import login
24
  login(token=os.getenv("HUGGING_FACE_TOKEN"))
25
 
26
+ def load_chunk_persist_pdf(task) -> Chroma:
27
 
28
  pdf_folder_path = os.path.join(os.getcwd(),Path(f"data/pdf/{task}"))
29
  documents = []
 
37
  os.makedirs("data/chroma_store/", exist_ok=True)
38
  vectorstore = Chroma.from_documents(
39
  documents=chunked_documents,
40
+ embedding=MistralAIEmbeddings(),
41
  persist_directory= os.path.join(os.getcwd(),Path("data/chroma_store/"))
42
  )
43
  vectorstore.persist()
44
  return vectorstore
45
 
46
+ personal_info_vectorstore = load_chunk_persist_pdf("personal_info")
47
  zero2hero_vectorstore = load_chunk_persist_pdf("zero2hero")
48
  bodyweight_vectorstore = load_chunk_persist_pdf("bodyweight")
49
  nutrition_vectorstore = load_chunk_persist_pdf("nutrition")
 
52
  nutrition_retriever = nutrition_vectorstore.as_retriever()
53
  bodyweight_retriever = bodyweight_vectorstore.as_retriever()
54
  workout_retriever = workout_vectorstore.as_retriever()
55
+ personal_info_retriever = personal_info_vectorstore.as_retriever()
56
 
57
  llm = ChatMistralAI(model="mistral-large-latest", mistral_api_key=mistral_api_key, temperature=0)
58
 
59
  prompt = ChatPromptTemplate.from_template(
60
  """
61
  You are a professional AI coach specialized in fitness, bodybuilding and nutrition.
62
+ You must adapt to the user according to personal informations in the context. A You are gentle and motivative.
63
  Use the following pieces of retrieved context to answer the question.
64
  If you don't know the answer, use your common knowledge.
65
  Use three sentences maximum and keep the answer concise.
 
75
  def format_docs(docs):
76
  return "\n\n".join(doc.page_content for doc in docs)
77
 
78
+ retriever = MergerRetriever(retrievers=[zero2hero_retriever, bodyweight_retriever, nutrition_retriever, workout_retriever, personal_info_retriever])
79
 
80
  rag_chain = (
81
  {"context": retriever | format_docs, "question": RunnablePassthrough()}
 
86
 
87
 
88
 
89
+ print(rag_chain.invoke("WHi I'm Susan. Can you make a fitness program for me please?"))
90
 
91
  # print(rag_chain.invoke("I am a 45 years old woman and I have to loose weight for the summer. Provide me with a fitness program, and a nutrition program"))
data/Zero To Hero - Bienvenue dans ta nouvelle vie.pdf DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:1e59511fd335776dcb8b0e98e9e9afe6d22483df94c83755163d5d5d4a7f3809
3
- size 60211845
 
 
 
 
data/{RaptorBodyweight.pdf → pdf/personal_info/Susan_Thompson.pdf} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:cbc90aebadbad36b8f47cd9ba45a958d278e38e7481037acb9ecee5e3805d771
3
- size 26474997
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:25e4eaa98480003224e09c48011e82678f9947382fc37c16011b75fab80f93a2
3
+ size 55515