File size: 1,332 Bytes
b3fc90b
 
82fdbb3
 
b3fc90b
23e9e9c
b3fc90b
 
 
23e9e9c
b3fc90b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82fdbb3
23e9e9c
b3fc90b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import json

from services.qa_service.utils import format_prompt

class QAService:
    def __init__(self, conf, pinecone, model_pipeline, question, context):
        self.conf = conf
        self.pc = pinecone['connection']
        self.embedder = pinecone['embedder']
        self.model_pipeline = model_pipeline
        self.question = question
        self.context = context
    
    def __enter__(self):
        print("Start Q&A Service")
        return self
    
    def __exit__(self, exc_type, exc_val, exc_tb):
        print("Exiting Q&A Service")
    
    def retrieve_context(self):
        """Pass embedded question into pinecone"""
        embedded_query = self.embedder.get_text_embedding(self.question)
        pinecone_index = self.pc.Index(conf['embeddings']['index_name'])
        
        result = pinecone_index.query(
            vector=embedded_query,
            top_k=1,
            include_values=False,
            include_metadata=True
        )
        
        output = json.loads(result['matches'][0]['metadata']['_node_content'])
        
        return output
    
    def run(self):
        """Query pinecone outputs and infer results"""
        output = self.retrieve_context()
        output = format_prompt(output)
        output = self.model_pipeline.infer(output)
        
        return output