lrtherond commited on
Commit
f30f794
·
1 Parent(s): 3a26be7

Franc v1.0

Browse files
Files changed (1) hide show
  1. app.py +31 -15
app.py CHANGED
@@ -6,6 +6,7 @@ from langchain import PromptTemplate
6
  from langchain.chains import RetrievalQA
7
  from langchain.embeddings.huggingface import HuggingFaceEmbeddings
8
  from langchain.llms import HuggingFaceEndpoint
 
9
  from langchain.vectorstores import Pinecone
10
  from torch import cuda
11
 
@@ -47,18 +48,20 @@ def get_prompt_template(instruction, system_prompt):
47
 
48
 
49
  template = get_prompt_template(
50
- """Use the following pieces of context to answer the question at the end.
 
51
 
52
  {context}
53
 
54
- Question: {question}
55
- Helpful Answer:""",
56
- """Your name is Franc.
57
- You are a running coach and exercise physiologist.
58
- You communicate in the style of Hal Higdon, but you never reference Hal Higdon in your answers.
59
- You never uses hashtags.
60
- Your answers are always 512-character long or less.
61
- If you don't know the answer to a question, please don't share false information."""
 
62
  )
63
 
64
  endpoint_url = (
@@ -71,21 +74,34 @@ llm = HuggingFaceEndpoint(
71
  task="text-generation",
72
  model_kwargs={
73
  "max_new_tokens": 512,
74
- "temperature": 0.4,
75
  "repetition_penalty": 1.3,
76
  "return_full_text": True,
77
- }
78
  )
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  rag_chain = RetrievalQA.from_chain_type(
81
  llm=llm,
82
  chain_type='stuff',
83
  retriever=vector_store.as_retriever(search_kwargs={'k': 5}),
84
  chain_type_kwargs={
85
- "prompt": PromptTemplate(
86
- template=template,
87
- input_variables=["context", "question"],
88
- ),
89
  },
90
  )
91
 
 
6
  from langchain.chains import RetrievalQA
7
  from langchain.embeddings.huggingface import HuggingFaceEmbeddings
8
  from langchain.llms import HuggingFaceEndpoint
9
+ from langchain.memory import ConversationBufferWindowMemory
10
  from langchain.vectorstores import Pinecone
11
  from torch import cuda
12
 
 
48
 
49
 
50
  template = get_prompt_template(
51
+ """Use the following scientific literature to answer the question at the end.
52
+ If the scientific literature is not relevant, please answer using your own knowledge about the topic:
53
 
54
  {context}
55
 
56
+ Question: {question}""",
57
+ """Your name is Franc. You are a running coach.
58
+ You are assisting a runner with some of their questions.
59
+
60
+ Reply in 10 sentences or less. Reply as a friend would.
61
+
62
+ Here is a record of the conversation so far:
63
+
64
+ {history}"""
65
  )
66
 
67
  endpoint_url = (
 
74
  task="text-generation",
75
  model_kwargs={
76
  "max_new_tokens": 512,
77
+ "temperature": 0.3,
78
  "repetition_penalty": 1.3,
79
  "return_full_text": True,
80
+ },
81
  )
82
 
83
+ prompt = PromptTemplate(
84
+ template=template,
85
+ input_variables=["history", "context", "question"],
86
+ )
87
+
88
+ memory = ConversationBufferWindowMemory(
89
+ k=3,
90
+ memory_key="history",
91
+ input_key="question",
92
+ ai_prefix="Franc",
93
+ human_prefix="Runner",
94
+ )
95
+
96
+ memory.chat_memory.add_user_message("Hello, my name is Laurent!")
97
+
98
  rag_chain = RetrievalQA.from_chain_type(
99
  llm=llm,
100
  chain_type='stuff',
101
  retriever=vector_store.as_retriever(search_kwargs={'k': 5}),
102
  chain_type_kwargs={
103
+ "prompt": prompt,
104
+ "memory": memory,
 
 
105
  },
106
  )
107