Skip to content

Commit aff4d9b

Browse files
committed
Reasonably working version 2
1 parent b15e14b commit aff4d9b

File tree

1 file changed

+28
-10
lines changed
  • pgml-apps/pgml-chat/pgml_chat

1 file changed

+28
-10
lines changed

pgml-apps/pgml-chat/pgml_chat/main.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ def handler(signum, frame):
152152
model_name = "hkunlp/instructor-xl"
153153
model_embedding_instruction = "Represent the %s document for retrieval: " % (bot_topic)
154154
model_params = {"instruction": model_embedding_instruction}
155+
# model_name = "BAAI/bge-large-en-v1.5"
156+
# model_params = {}
155157
model = Model(model_name, "pgml", model_params)
156158
pipeline = Pipeline(args.collection_name + "_pipeline", model, splitter)
157159
chat_history_pipeline = Pipeline(
@@ -162,6 +164,7 @@ def handler(signum, frame):
162164
"Represent the %s question for retrieving supporting documents: " % (bot_topic)
163165
)
164166
query_params = {"instruction": query_params_instruction}
167+
#query_params = {}
165168

166169
default_system_prompt_template = """
167170
You are an assistant to answer questions about {topic}.
@@ -181,15 +184,30 @@ def handler(signum, frame):
181184

182185
system_prompt = os.environ.get("SYSTEM_PROMPT", default_system_prompt)
183186

184-
base_prompt = """Use the following pieces of context to answer the question at the end. First identify which of the contexts are relevant to the question.
185-
If you don't know the answer, just say that you don't know, don't try to make up an answer.
186-
Use five sentences maximum and keep the answer as concise as possible.
187+
base_prompt = """Use the following list of documents to answer user's question.
188+
Use the following steps:
189+
190+
1. Identify if the user input is really a question.
191+
2. If the user input is not related to the topic then respond that it is not related to the topic.
192+
3. If the user input is related to the topic then first identify relevant documents from the list of documents.
193+
4. Ignore all the documents that are not relevant to the question.
194+
5. If the documents that you found relevant have information to completely and accurately answers the question then respond with the answer.
195+
6. If the documents that you found relevant have code snippets then respond with the code snippets.
196+
7. Most importantly, don't make up code snippets that are not present in the documents.
197+
198+
####
199+
Documents
187200
####
188201
{context}
189202
###
190-
Question: {question}
203+
User: {question}
191204
###
192-
Include a {response_programming_language} code snippet verbatim in the answer wherever possible. You speak like {persona} in {language}. If the context is empty, ask for more information.
205+
206+
If the user input is generic then respond with a generic answer. For example: If the user says "Hello" then respond with "Hello". If the user says "Thank you" then respond with "You are welcome".
207+
You speak like {persona} in {language}.
208+
209+
Most importantly, If you don't find any document to answer the question say I don't know! DON'T MAKE UP AN ANSWER! It is very important that you don't make up an answer!
210+
193211
Helpful Answer:"""
194212

195213
openai_api_key = os.environ.get("OPENAI_API_KEY")
@@ -251,7 +269,7 @@ async def generate_chat_response(
251269

252270
if user_input:
253271
query = await get_prompt(user_input)
254-
messages.append({"role": "system", "content": query})
272+
messages.append({"role": "user", "content": query})
255273
print(messages)
256274
response = await generate_response(
257275
messages,
@@ -281,7 +299,7 @@ async def generate_response(
281299
openai.api_key = openai_api_key
282300
log.debug("Generating response from OpenAI API: " + str(messages))
283301
response = openai.ChatCompletion.create(
284-
model="gpt-3.5-turbo",
302+
model="gpt-3.5-turbo-16k",
285303
messages=messages,
286304
temperature=temperature,
287305
max_tokens=max_tokens,
@@ -302,10 +320,10 @@ async def ingest_documents(folder: str):
302320

303321

304322
async def get_prompt(user_input: str = ""):
305-
# user_input = "In the context of " + bot_topic + ", " + user_input
323+
query_input = "In the context of " + bot_topic + ", " + user_input
306324
vector_results = (
307325
await collection.query()
308-
.vector_recall(user_input, pipeline, query_params)
326+
.vector_recall(query_input, pipeline, query_params)
309327
.limit(5)
310328
.fetch_all()
311329
)
@@ -315,7 +333,7 @@ async def get_prompt(user_input: str = ""):
315333

316334
for id, result in enumerate(vector_results):
317335
if result[0] > 0.6:
318-
context += "#### \n Context %d: "%(id) + result[1] + "\n"
336+
context += "#### \n Document %d: "%(id) + result[1] + "\n"
319337

320338
query = base_prompt.format(
321339
context=context,

0 commit comments

Comments
 (0)