Skip to content

Commit 2d90d6e

Browse files
committed
chore: tune repl
1 parent 3a59c2a commit 2d90d6e

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

repl.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
from typing import List, Tuple
55

66

7+
def title_to_url(title: str):
8+
return f"https://en.wikipedia.org/wiki/{title.replace(' ', '_')}"
9+
10+
711
def print_chunk_info(chunk, rank):
8-
url = f"https://en.wikipedia.org/wiki/{chunk.title.replace(' ', '_')}"
9-
print(f"{chunk.title} ({rank}) {url}")
10-
print(f"{chunk.description}")
12+
print(f" {chunk.title} ({rank}) {title_to_url(chunk.title)}")
13+
print(f" {chunk.description}")
1114

1215

1316
def print_results(chunks_with_distances):
@@ -29,7 +32,7 @@ def print_results(chunks_with_distances):
2932
if not existing_chunk:
3033
pages[chunk.pageId] = (chunk, rank)
3134

32-
print("Results:\n")
35+
print("\nResults:\n")
3336

3437
# sort pages by rank and print
3538
for p in sorted(pages.values(), key=lambda x: x[1]):
@@ -39,29 +42,36 @@ def print_results(chunks_with_distances):
3942

4043
def get_sys_prompt():
4144
return """
42-
Don't answer questions that are harmful or immoral.
43-
You are a powerful conversational AI trained to help people. You are
44-
augmented by a number of documents, and your job is to use and consume the
45-
documents to best help the user. You will then see a specific instruction
46-
instructing you what kind of response to generate. When you answer the user's
47-
requests, you cite your sources in your answers, according to those
48-
instructions. You should focus on serving the user's needs as best you can,
49-
which will be wide-ranging. You should answer in full sentences, using proper
50-
grammar and spelling.
45+
You are a powerful AI trained to help people. You are augmented by a context
46+
with a number of documents, and your job is to use and consume the documents to
47+
best help the user. When you answer the user's query, you cite documents from
48+
the context by referencing document URLs. You should answer in full sentences,
49+
using proper grammar and spelling.
5150
"""
5251

5352

5453
def get_user_prompt(query, chunks_with_distances: List[Tuple[postgres.Chunk, float]]):
5554
context = ""
5655
index = 0
5756
for c, _ in chunks_with_distances:
58-
context += f"Document {index}\n{c.title}: {c.description}\n{c.text}\n\n"
57+
context += f"Document url:{title_to_url(c.title)}\n{c.title}: {c.description}\n{c.text}\n\n"
5958
index += 1
6059

61-
return (
62-
f"Respond to a query. Base your answer on information from this context only:\n"
63-
+ f"{context}\n\nEnd of context.\n\nRespond to the following query {query}."
64-
)
60+
return f"""
61+
Respond to a query using the following context. Base your answer on documents
62+
from this context only and cite the documents you used by using the URL like
63+
this:
64+
65+
According to https://en.wikipedia.org/wiki/Cat the domestic cat is a small carnivorous mammal.
66+
67+
Context:
68+
69+
{context}
70+
71+
End of context.
72+
73+
Respond to the following query: {query}.
74+
"""
6575

6676

6777
def print_chatbot(stream):
@@ -107,5 +117,6 @@ def rag(query, number_of_documents=5):
107117

108118
if __name__ == "__main__":
109119
while True:
120+
print(78 * ".")
110121
query = input("\nQuery >> ")
111-
rag(query, number_of_documents=6)
122+
rag(query, number_of_documents=1)

test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# query = "Adjustment of DNA"
66
# query = "Scandinavian wild carnivore animal"
77
query = "Cold climate cat"
8+
# query = "choclate like candy"
89
print("Query:", query)
910
repl.rag(query, number_of_documents=1)
1011

0 commit comments

Comments
 (0)