File size: 1,449 Bytes
a73d4bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from groq import Client
from retriever import Retriever
from typing import List
import glob

dir_path = 'docs'

class QuestionAnsweringBot:

    PROMPT = """

        You are a helpful assistant that answers the questions.



        Rules:

        - Reply with answer only and nothing but answer.

        - Say 'I don`t know' if you don`t know the answer.

        - Use the provided context.

    """

    def __init__(self, docs: List[str], score: int, api_key) -> None:
        self.retriever = Retriever(docs=docs, score=score)
        self.client = Client(api_key=api_key)

    def answer_question(self, question: str) -> str:
        context = self.retriever.get_docs(query=question)
        messages = [
            {
                "role": "system",
                "content": self.PROMPT
                },
            {
                "role": "user",
                "content": f"Context: {context}\nQuestion: {question}"
                }
            ]
        chat_completion = self.client.chat.completions.create(
            messages=messages,
            model="llama3-70b-8192"
            )

        return chat_completion.choices[0].message.content


def read_docs(dir_path) -> List[str]:
    docs = []
    for path in glob.glob(f'{dir_path}/*.txt'):
        with open(path, 'r', encoding='utf-8') as file:
            text = file.read()
            docs.append(text)
    return docs