Spaces:
Sleeping
Sleeping
Deepak Sahu
commited on
Commit
·
f0c8373
1
Parent(s):
060a333
image retrieval
Browse files- z_generate.py +49 -6
z_generate.py
CHANGED
@@ -10,7 +10,7 @@ class ServerlessInference:
|
|
10 |
|
11 |
def test(self, query:str) -> str:
|
12 |
'''Responds to query using llm'''
|
13 |
-
messages:
|
14 |
{
|
15 |
"role": "user",
|
16 |
"content": query
|
@@ -33,7 +33,7 @@ class ServerlessInference:
|
|
33 |
context += "".join([f"Document {str(i)}:::\n" + doc for i, doc in enumerate(retrieved_docs_text)])
|
34 |
|
35 |
# Augmented Generation
|
36 |
-
messages:
|
37 |
{
|
38 |
"role": "system",
|
39 |
"content": """Using the information contained in the context,
|
@@ -42,9 +42,7 @@ give a comprehensive answer to the question.
|
|
42 |
|
43 |
Respond only to the question asked, response should be concise and relevant to the question.
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
If the answer cannot be deduced from the context, do not give an answer.""",
|
48 |
|
49 |
},
|
50 |
|
@@ -70,4 +68,49 @@ Question: {question}""".format(context=context, question=query),
|
|
70 |
)
|
71 |
|
72 |
response_text = completion.choices[0].message.content
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
def test(self, query:str) -> str:
|
12 |
'''Responds to query using llm'''
|
13 |
+
messages:list = [
|
14 |
{
|
15 |
"role": "user",
|
16 |
"content": query
|
|
|
33 |
context += "".join([f"Document {str(i)}:::\n" + doc for i, doc in enumerate(retrieved_docs_text)])
|
34 |
|
35 |
# Augmented Generation
|
36 |
+
messages:list = [
|
37 |
{
|
38 |
"role": "system",
|
39 |
"content": """Using the information contained in the context,
|
|
|
42 |
|
43 |
Respond only to the question asked, response should be concise and relevant to the question.
|
44 |
|
45 |
+
If the answer cannot be deduced from the context, do not give an answer. Instead say `Theres lack of information in document source.`""",
|
|
|
|
|
46 |
|
47 |
},
|
48 |
|
|
|
68 |
)
|
69 |
|
70 |
response_text = completion.choices[0].message.content
|
71 |
+
|
72 |
+
# Image retrieval
|
73 |
+
retrieved_image = self.vs_images.similarity_search(query=query, k=5)
|
74 |
+
retrieved_docs_text = [doc.page_content for doc in retrieved_image] # We only need the text of the documents
|
75 |
+
context = "\nExtracted Images:\n"
|
76 |
+
context += "".join([f"Document {str(i)}:::\n" + doc for i, doc in enumerate(retrieved_docs_text)])
|
77 |
+
|
78 |
+
|
79 |
+
messages:list = [
|
80 |
+
{
|
81 |
+
"role": "system",
|
82 |
+
"content": """Using the information contained in the context about the images stored in the database,
|
83 |
+
|
84 |
+
give a list of identifiers of the image that best represent the kind of information seeked by the question.
|
85 |
+
|
86 |
+
Respond only to the question asked. Provide only number(s) of the source images relevant to the question.
|
87 |
+
|
88 |
+
If the image is relevant to the question then output format should be a list [1, 3, 0]
|
89 |
+
|
90 |
+
otherwise reply with [] (empty list)""",
|
91 |
+
|
92 |
+
},
|
93 |
+
|
94 |
+
{
|
95 |
+
"role": "user",
|
96 |
+
"content": """Context:
|
97 |
+
|
98 |
+
{context}
|
99 |
+
|
100 |
+
---
|
101 |
+
|
102 |
+
Now here is the question you need to answer.
|
103 |
+
|
104 |
+
Question: {question}""".format(context=context, question=query),
|
105 |
+
|
106 |
+
},
|
107 |
+
]
|
108 |
+
|
109 |
+
completion = self.client.chat.completions.create(
|
110 |
+
model=self.model,
|
111 |
+
messages=messages,
|
112 |
+
max_tokens=500
|
113 |
+
)
|
114 |
+
|
115 |
+
images_list = completion.choices[0].message.content
|
116 |
+
return response_text + str(images_list)
|