File size: 2,361 Bytes
e94d3a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from typing import List
from openai import OpenAI

from src.core.types import DEFAULT_OPENAI_ANALYZER, DEFAULT_SYSTEM_PROMPT, DEFAULT_USER_PROMPT

from src.models.analyzer_models import AnalyzerResult
from src.models.scrape_models import ScrapeResult

from src.core.interface.analyzer_interface import AnalyzerInterface


class OpenaiAnalyzer(AnalyzerInterface):
    def __init__(self, api_key, model_name = DEFAULT_OPENAI_ANALYZER):
       self.client = OpenAI(api_key=api_key)
       self.model_name = model_name

    def analyze_search_result(self, query: str, search_results: List[ScrapeResult]) -> AnalyzerResult:
        """
        Analyzes the provided search results based on the given query.
        Args:
            query (str): The search query string.
            search_results (List[ScrapeResult]): A list of search results to be analyzed.
        Returns:
            AnalyzerResult: The result of the analysis.
        Raises:
            NotImplementedError: If the method is not implemented by a subclass.
        """
        try:
            user_prompt = DEFAULT_USER_PROMPT.replace("query", query).replace("scrape_results", f"{search_results}")
            completion = self.client.beta.chat.completions.parse(model=self.model_name,
                                                                 messages=[
                                                                     {
                                                                         "role": "system",
                                                                         "content": DEFAULT_SYSTEM_PROMPT
                                                                     },
                                                                     {
                                                                         "role": "user",
                                                                         "content": user_prompt
                                                                     }
                                                                 ],
                                                                 response_format=AnalyzerResult)
            response = completion.choices[0].message.parsed
            return response
        except Exception as e:
            raise Exception(f"Error while analyzing search result: {str(e)}")