/ src / python / txtai / api / routers / similarity.py
similarity.py
 1  """
 2  Defines API paths for similarity endpoints.
 3  """
 4  
 5  from typing import List
 6  
 7  from fastapi import APIRouter, Body
 8  
 9  from .. import application
10  from ..route import EncodingAPIRoute
11  
12  router = APIRouter(route_class=EncodingAPIRoute)
13  
14  
15  @router.post("/similarity")
16  def similarity(query: str = Body(...), texts: List[str] = Body(...)):
17      """
18      Computes the similarity between query and list of text. Returns a list of
19      {id: value, score: value} sorted by highest score, where id is the index
20      in texts.
21  
22      Args:
23          query: query text
24          texts: list of text
25  
26      Returns:
27          list of {id: value, score: value}
28      """
29  
30      return application.get().similarity(query, texts)
31  
32  
33  @router.post("/batchsimilarity")
34  def batchsimilarity(queries: List[str] = Body(...), texts: List[str] = Body(...)):
35      """
36      Computes the similarity between list of queries and list of text. Returns a list
37      of {id: value, score: value} sorted by highest score per query, where id is the
38      index in texts.
39  
40      Args:
41          queries: queries text
42          texts: list of text
43  
44      Returns:
45          list of {id: value, score: value} per query
46      """
47  
48      return application.get().batchsimilarity(queries, texts)