gperdrizet commited on
Commit
2e66273
·
verified ·
1 Parent(s): e249170

Added tool to find title of article being refered to by query with vector search of content.

Browse files
Files changed (2) hide show
  1. functions/tools.py +48 -4
  2. rss_server.py +43 -7
functions/tools.py CHANGED
@@ -6,6 +6,7 @@ import time
6
  import json
7
  import logging
8
  import queue
 
9
  from upstash_vector import Index
10
 
11
  import functions.feed_extraction as extraction_funcs
@@ -85,7 +86,7 @@ def get_feed(website: str, n: int = 3) -> list:
85
  return json.dumps(articles)
86
 
87
 
88
- def context_search(query: str, article_title: str = None) -> str:
89
  '''Searches for context relevant to query in article vector store.
90
  Use this Function to search for additional information before
91
  answering the user's question about an article. If article_title is
@@ -99,7 +100,7 @@ def context_search(query: str, article_title: str = None) -> str:
99
  context from a specific context, defaults to None
100
 
101
  Returns:
102
- Context information which bests matches the query.
103
  '''
104
 
105
  logger = logging.getLogger(__name__ + 'context_search')
@@ -114,11 +115,54 @@ def context_search(query: str, article_title: str = None) -> str:
114
  results = index.query(
115
  data=query,
116
  top_k=3,
117
- include_metadata=True,
118
  include_data=True,
119
  namespace=article_title
120
  )
121
 
122
  logger.info('Retrieved %s chunks for "%s"', len(results), query)
123
 
124
- return results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import json
7
  import logging
8
  import queue
9
+ from typing import Tuple
10
  from upstash_vector import Index
11
 
12
  import functions.feed_extraction as extraction_funcs
 
86
  return json.dumps(articles)
87
 
88
 
89
+ def context_search(query: str, article_title: str = None) -> list[Tuple[float, str]]:
90
  '''Searches for context relevant to query in article vector store.
91
  Use this Function to search for additional information before
92
  answering the user's question about an article. If article_title is
 
100
  context from a specific context, defaults to None
101
 
102
  Returns:
103
+ List of tuples with the following format: [(relevance score, 'context string')]
104
  '''
105
 
106
  logger = logging.getLogger(__name__ + 'context_search')
 
115
  results = index.query(
116
  data=query,
117
  top_k=3,
 
118
  include_data=True,
119
  namespace=article_title
120
  )
121
 
122
  logger.info('Retrieved %s chunks for "%s"', len(results), query)
123
 
124
+ contexts = []
125
+
126
+ for result in results:
127
+ contexts.append((result.score, result.data))
128
+
129
+ return contexts
130
+
131
+
132
+ def find_article(query: str) -> list[Tuple[float, str]]:
133
+ '''Uses vector search to find the most likely title of the article
134
+ referred to by query. Use this function if the user is asking about
135
+ and article, but it is unclear which article.
136
+
137
+ Args:
138
+ query: query to to find source article for
139
+
140
+ Returns:
141
+ List of tuples of most likely context article titles in the following format:
142
+ [(relevance score, 'article title')]
143
+ '''
144
+
145
+ logger = logging.getLogger(__name__ + 'context_search')
146
+
147
+ index = Index(
148
+ url='https://living-whale-89944-us1-vector.upstash.io',
149
+ token=os.environ['UPSTASH_VECTOR_KEY']
150
+ )
151
+
152
+ results = None
153
+
154
+ results = index.query(
155
+ data=query,
156
+ top_k=3,
157
+ include_metadata=True,
158
+ include_data=True
159
+ )
160
+
161
+ logger.info('Retrieved %s chunks for "%s"', len(results), query)
162
+
163
+ contexts = []
164
+
165
+ for result in results:
166
+ contexts.append((result.score, result.metadata['namespace']))
167
+
168
+ return contexts
rss_server.py CHANGED
@@ -71,20 +71,56 @@ with gr.Blocks() as demo:
71
 
72
  # Vector search tool
73
  gr.Markdown('### 2. `context_search()`')
74
- search_query = gr.Textbox('Does apple offer parental controls?', label='Vector search query')
75
- search_output = gr.Textbox(label='Vector search results', lines=7, max_lines=7)
 
 
 
 
 
 
 
 
76
 
77
  with gr.Row():
78
- search_submit_button = gr.Button('Submit query')
79
- search_clear_button = gr.ClearButton(components=[search_query, search_output])
 
 
80
 
81
- search_submit_button.click( # pylint: disable=no-member
82
  fn=tool_funcs.context_search,
83
- inputs=search_query,
84
- outputs=search_output,
85
  api_name='Context vector search'
86
  )
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  if __name__ == '__main__':
90
 
 
71
 
72
  # Vector search tool
73
  gr.Markdown('### 2. `context_search()`')
74
+
75
+ context_search_query = gr.Textbox(
76
+ 'Does apple offer parental controls?',
77
+ label='Context search query'
78
+ )
79
+ context_search_output = gr.Textbox(
80
+ label='Context search results',
81
+ lines=7,
82
+ max_lines=7
83
+ )
84
 
85
  with gr.Row():
86
+ context_search_submit_button = gr.Button('Submit query')
87
+ context_search_clear_button = gr.ClearButton(
88
+ components=[context_search_query, context_search_output]
89
+ )
90
 
91
+ context_search_submit_button.click( # pylint: disable=no-member
92
  fn=tool_funcs.context_search,
93
+ inputs=context_search_query,
94
+ outputs=context_search_output,
95
  api_name='Context vector search'
96
  )
97
 
98
+ # Find article tool
99
+ gr.Markdown('### 3. `find_article()`')
100
+
101
+ article_search_query = gr.Textbox(
102
+ 'Does apple offer parental controls?',
103
+ label='Article search query'
104
+ )
105
+ article_search_output = gr.Textbox(
106
+ label='Article search results',
107
+ lines=7,
108
+ max_lines=7
109
+ )
110
+
111
+ with gr.Row():
112
+ article_search_submit_button = gr.Button('Submit query')
113
+ article_search_clear_button = gr.ClearButton(
114
+ components=[article_search_query, article_search_output]
115
+ )
116
+
117
+ article_search_submit_button.click( # pylint: disable=no-member
118
+ fn=tool_funcs.find_article,
119
+ inputs=article_search_query,
120
+ outputs=article_search_output,
121
+ api_name='Article vector search'
122
+ )
123
+
124
 
125
  if __name__ == '__main__':
126