File size: 2,583 Bytes
853a071
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
import os
from dotenv import load_dotenv
from openai import OpenAI
from qdrant_client import QdrantClient
from pipelines.message import send_message
import redis

conversation_chat = []


def run():
    load_dotenv()

    try:
        oa_client = OpenAI(
            api_key=os.environ.get("OPENAI_API_KEY")
        )
        print("✅ Conectado a OpenAI.")

        qdrant_client = QdrantClient(
            host=os.environ.get("QDRANT_HOST"),
            port=os.environ.get("QDRANT_PORT")
        )
        print("✅ Conectado ao Qdrant.")

        redis_client = redis.Redis(
            host=os.environ.get("REDIS_HOST"),
            port=os.environ.get("REDIS_PORT"),
            decode_responses=True
        )
        print("✅ Conectado ao Redis.")

        while True:
            prompt = input("Digite sua pergunta: ")

            embedding = oa_client.embeddings.create(
                input=[prompt],
                model=os.environ.get("OPENAI_MODEL_EMBEDDING")
            ).data[0].embedding

            child_texts = qdrant_client.search(
                collection_name=os.environ.get("COLLECTION_NAME"), 
                query_vector=embedding,
                limit=3
            )

            print("--------- Child text ---------")
            print(child_texts)

            contexts = []

            for child_text in child_texts:
                parent_text = redis_client.hgetall(
                    child_text[0].payload["parent_id"]
                )
                context = {
                    "content": parent_text["content"],
                    "url": parent_text["url"]
                }
                contexts.append(context)

            print("--------- Contexts ---------")
            print(contexts)

            stream_response = send_message(
                oa_client,
                context,
                prompt,
                conversation_chat
            )

            print("--------- Response Agent ---------")
            response = ""
            for chunk in stream_response:
                if chunk.choices[0].delta.content is not None:
                    response += chunk.choices[0].delta.content
                    print(chunk.choices[0].delta.content, end="")
            conversation_chat.append({
                "role": "assistant",
                "content": response
            })

            is_exit = input("\nDeseja sair? (s/n): ")
            if is_exit == "s":
                break
    except Exception as error:
        print(f"❌ Erro: {error}")


if __name__ == "__main__":
    run()