4
4
from typing import List , Tuple
5
5
6
6
7
+ def title_to_url (title : str ):
8
+ return f"https://en.wikipedia.org/wiki/{ title .replace (' ' , '_' )} "
9
+
10
+
7
11
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 } " )
11
14
12
15
13
16
def print_results (chunks_with_distances ):
@@ -29,7 +32,7 @@ def print_results(chunks_with_distances):
29
32
if not existing_chunk :
30
33
pages [chunk .pageId ] = (chunk , rank )
31
34
32
- print ("Results :\n " )
35
+ print ("\n Results :\n " )
33
36
34
37
# sort pages by rank and print
35
38
for p in sorted (pages .values (), key = lambda x : x [1 ]):
@@ -39,29 +42,36 @@ def print_results(chunks_with_distances):
39
42
40
43
def get_sys_prompt ():
41
44
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.
51
50
"""
52
51
53
52
54
53
def get_user_prompt (query , chunks_with_distances : List [Tuple [postgres .Chunk , float ]]):
55
54
context = ""
56
55
index = 0
57
56
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 "
59
58
index += 1
60
59
61
- return (
62
- f"Respond to a query. Base your answer on information from this context only:\n "
63
- + f"{ context } \n \n End of context.\n \n Respond 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
+ """
65
75
66
76
67
77
def print_chatbot (stream ):
@@ -107,5 +117,6 @@ def rag(query, number_of_documents=5):
107
117
108
118
if __name__ == "__main__" :
109
119
while True :
120
+ print (78 * "." )
110
121
query = input ("\n Query >> " )
111
- rag (query , number_of_documents = 6 )
122
+ rag (query , number_of_documents = 1 )
0 commit comments