File size: 2,487 Bytes
e490ab5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from semantic import SemanticSearch
import json
import argparse
import os
from embeddings import get_embedding
from openai import OpenAI
import configparser



class BackEnd:

    def __init__(self, config):
        self.model = "gpt-4.1"  #config['ChatBot']['model']
        
        self.client = OpenAI()


        self.semantic_search = SemanticSearch()#config['Semantic Search'])
        try:
            with open('data/demo_prompt.json') as json_file:#config['ChatBot']['prompt file']) as json_file:
                prompts = json.load(json_file)
        except:
            print(f"ERROR. Couldn't load prompt file {config['ChatBot']['prompt file']} or wrong json format")
            quit()
            
        lang = 'fr'#config['General']['language'][:2].lower()
        if lang  == 'fr':
            self.prompt_template = prompts["French"]
        elif lang == 'en':
            self.prompt_template = prompts["English"]

                    
               
                

    def process_query(self, query):


        query_embeddings = get_embedding(query)
        context = self.semantic_search.search(query_embeddings)
        for index, document in enumerate(context):
            context[index] = 'Document ' + str(index + 1) + '\n\n' + document
        print('context = ', context)


        documents = '\n\n'.join(context)
        
        prompt = self.prompt_template['system_prompt']
        demo_prefix = self.prompt_template['demo_prefix'].format(query = query, context = context)
        prompt += demo_prefix + '\n' + documents + '\n\n'
        demo_postfix = self.prompt_template['demo_postfix']
        prompt += demo_postfix

        if 'gpt' in self.model:
            response  = self.client.responses.create(
                model = self.model ,
                input= prompt)

        return json.loads(response.output_text), context
        



# def main():
#     parser = argparse.ArgumentParser()
#     parser.add_argument('--config_file', type=str, required=True, help='File containing the configuration for the backend (in .ini format)')
#     parser.add_argument('--query', type=str, required=False, help='Test query for testing the system')
    
#     args = parser.parse_args()

#     config = configparser.ConfigParser()
#     config.read(args.config_file)
#     backend = BackEnd(config)

#     if args.query:
#         response = backend.process_query(args.query)
#         print(response)


# if __name__ == '__main__':
#     main()