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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#coding: utf-8
|
| 2 |
+
|
| 3 |
+
import re
|
| 4 |
+
from os import getenv
|
| 5 |
+
from typing import Any
|
| 6 |
+
from typing import Dict
|
| 7 |
+
from typing import IO
|
| 8 |
+
from typing import List
|
| 9 |
+
from typing import Optional
|
| 10 |
+
from typing import Tuple
|
| 11 |
+
from typing import Union
|
| 12 |
+
from var_app import GlobalSystemPrompts
|
| 13 |
+
import streamlit as st
|
| 14 |
+
from openai import OpenAI
|
| 15 |
+
|
| 16 |
+
from dotenv import load_dotenv
|
| 17 |
+
# Charger les variables d'environnement depuis le fichier .env
|
| 18 |
+
|
| 19 |
+
class DemorrhaAssistant(object):
|
| 20 |
+
def __init__(self):
|
| 21 |
+
load_dotenv()
|
| 22 |
+
self.client = self.initialize_client() # OpenAI(api_key = getenv("OPENAI_API_KEY"))
|
| 23 |
+
self.vector_store = None
|
| 24 |
+
self.load_assistant()
|
| 25 |
+
|
| 26 |
+
def initialize_client(self):
|
| 27 |
+
# Initialiser le client OpenAI avec la clé API
|
| 28 |
+
api_key = getenv("OPENAI_API_KEY")
|
| 29 |
+
return OpenAI(api_key=api_key)
|
| 30 |
+
|
| 31 |
+
def search_assistant(self, assistant_name="Demorrha"):
|
| 32 |
+
last_id = None
|
| 33 |
+
while True:
|
| 34 |
+
# Lister tous les assistants
|
| 35 |
+
assistants_list = self.client.beta.assistants.list(
|
| 36 |
+
order="desc",
|
| 37 |
+
limit="20",
|
| 38 |
+
after=last_id
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if len(assistants_list.data) < 1:
|
| 42 |
+
break
|
| 43 |
+
|
| 44 |
+
last_id = assistants_list.data[-1].id
|
| 45 |
+
for assistant in assistants_list.data:
|
| 46 |
+
if assistant.name == assistant_name:
|
| 47 |
+
return assistant.id
|
| 48 |
+
return None
|
| 49 |
+
|
| 50 |
+
def search_vector_store(self, vector_store_name="Demorrha_Style"):
|
| 51 |
+
last_id=None
|
| 52 |
+
while True:
|
| 53 |
+
# Lister tous les assistants
|
| 54 |
+
vector_store_list = self.client.beta.vector_stores.list(
|
| 55 |
+
order="desc",
|
| 56 |
+
limit="20",
|
| 57 |
+
after=last_id
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if len(vector_store_list.data) < 1:
|
| 61 |
+
break
|
| 62 |
+
last_id = vector_store_list.data[-1].id
|
| 63 |
+
for vector_store in vector_store_list.data:
|
| 64 |
+
if vector_store.name == f"{vector_store_name}":
|
| 65 |
+
return vector_store.id
|
| 66 |
+
return None
|
| 67 |
+
|
| 68 |
+
def load_vector_store(self, vector_store_name="Demorrha_Style"):
|
| 69 |
+
vector_store_id = self.search_vector_store(vector_store_name)
|
| 70 |
+
if vector_store_id is None:
|
| 71 |
+
vector_store = self.client.beta.vector_stores.create(name=f"{vector_store_name}")
|
| 72 |
+
self.vector_store = vector_store
|
| 73 |
+
else:
|
| 74 |
+
self.vector_store = self.client.beta.vector_stores.retrieve(vector_store_id)
|
| 75 |
+
return self
|
| 76 |
+
|
| 77 |
+
def get_vector_store(self):
|
| 78 |
+
return self.vector_store
|
| 79 |
+
|
| 80 |
+
def upload_file(self,
|
| 81 |
+
file_path,
|
| 82 |
+
purpose="assistants"):
|
| 83 |
+
return self.client.files.create(
|
| 84 |
+
file=open(file_path, "rb"),
|
| 85 |
+
purpose=purpose
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
def list_files_in_vector_store(self, vector_store_id):
|
| 89 |
+
files_list = []
|
| 90 |
+
last_id=None
|
| 91 |
+
while True:
|
| 92 |
+
files_list = self.client.beta.vector_stores.files.list(
|
| 93 |
+
vector_store_id=vector_store_id,
|
| 94 |
+
limit="20",
|
| 95 |
+
after=last_id
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
if len(files_list.data) < 1:
|
| 99 |
+
break
|
| 100 |
+
last_id = files_list.data[-1].id
|
| 101 |
+
for file in files_list.data:
|
| 102 |
+
files_list.append(file)
|
| 103 |
+
return files_list
|
| 104 |
+
|
| 105 |
+
def attach_file_to_vectore_store(self,
|
| 106 |
+
vector_store_id,
|
| 107 |
+
file_id):
|
| 108 |
+
return self.client.beta.vector_stores.files.create(
|
| 109 |
+
vector_store_id=vector_store_id,
|
| 110 |
+
file_id=file_id
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
def load_assistant(self, assistant_name="Demorrha"):
|
| 114 |
+
system_prompt = GlobalSystemPrompts.linguascribe()
|
| 115 |
+
assistant_id = self.search_assistant(assistant_name)
|
| 116 |
+
if assistant_id is None:
|
| 117 |
+
self.assistant = self.client.beta.assistants.create(
|
| 118 |
+
model="gpt-4o-mini",
|
| 119 |
+
name="Demorrha",
|
| 120 |
+
description="Traite les messages des utilisateurs et génère une traduction.",
|
| 121 |
+
instructions=f"{system_prompt}",
|
| 122 |
+
temperature=0.1,
|
| 123 |
+
tools=[{"type": "file_search"}]
|
| 124 |
+
)
|
| 125 |
+
else:
|
| 126 |
+
self.assistant = self.client.beta.assistants.retrieve(assistant_id)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def add_file_to_vector_store(self, file_paths):
|
| 130 |
+
file_streams = [open(path, "rb") for path in file_paths]
|
| 131 |
+
file_batch = self.client.beta.vector_stores.file_batches.upload_and_poll(
|
| 132 |
+
vector_store_id=self.vector_store.id, files=file_streams
|
| 133 |
+
)
|
| 134 |
+
return file_batch
|
| 135 |
+
|
| 136 |
+
def use_assistant(self):
|
| 137 |
+
# Utiliser l'assistant ici
|
| 138 |
+
pass
|
| 139 |
+
|
| 140 |
+
"""
|
| 141 |
+
|
| 142 |
+
file_paths = ["style.txt"]
|
| 143 |
+
file_streams = [open(path, "rb") for path in file_paths]
|
| 144 |
+
|
| 145 |
+
file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
|
| 146 |
+
vector_store_id=vector_store.id, files=file_streams
|
| 147 |
+
)
|
| 148 |
+
"""
|
| 149 |
+
|
| 150 |
+
if __name__ == "__main__":
|
| 151 |
+
demorrha = DemorrhaAssistant()
|
| 152 |
+
vector_store = demorrha.load_vector_store().get_vector_store()
|
| 153 |
+
print(vector_store)
|
| 154 |
+
|
| 155 |
+
if vector_store.status == "completed":
|
| 156 |
+
if vector_store.file_counts.total > 0:
|
| 157 |
+
if vector_store.file_counts.completed == vector_store.file_counts.total:
|
| 158 |
+
print("Le chargement du vecteur est terminé.")
|
| 159 |
+
else:
|
| 160 |
+
file_paths = ["style.txt"]
|
| 161 |
+
file_batch = demorrha.add_file_to_vector_store(file_paths)
|
| 162 |
+
print("Fichier ajouté au vector_store:", file_batch)
|
| 163 |
+
"""
|
| 164 |
+
# Exemple d'utilisation de l'assistant
|
| 165 |
+
assistant_id = demorrha.search_assistant("Demorrha")
|
| 166 |
+
if assistant_id:
|
| 167 |
+
response = demorrha.use_assistant(assistant_id)
|
| 168 |
+
print("Réponse de l'assistant:", response)
|
| 169 |
+
else:
|
| 170 |
+
print("Assistant non trouvé.")
|
| 171 |
+
"""
|