Marylene commited on
Commit
cd42b03
·
1 Parent(s): b12f643

Change try another model

Browse files
Files changed (1) hide show
  1. agent.py +26 -22
agent.py CHANGED
@@ -1,42 +1,46 @@
1
  from huggingface_hub import InferenceClient
2
  import os
3
 
4
- client = InferenceClient(
5
- model= "mistralai/Mistral-7B-Instruct-v0.1", # "HuggingFaceH4/zephyr-7b-beta", # ou mistralai/Mistral-7B-Instruct-v0.1
6
- token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
7
- )
8
 
9
- def build_email_agent():
10
- return client
11
-
12
- def generate_email(client, to_email: str, context: str, tone: str) -> tuple[str, str]:
13
- prompt = f"""Tu es un assistant virtuel. Rédige un email destiné à {to_email}.
 
14
 
15
  Contexte : {context}
16
  Ton : {tone}
17
 
18
- Réponds de manière courte en deux parties :
19
  1. Objet : ligne d'objet de l'email
20
  2. Corps : contenu de l'email bien rédigé, en respectant le ton demandé.
21
  Commence chaque partie par son étiquette.
22
  """
23
 
24
- response = client.text_generation(
25
- prompt=prompt,
26
- max_new_tokens=512,
 
 
 
27
  temperature=0.7,
28
- do_sample=True,
29
- return_full_text=False
30
  )
31
 
32
- print("🔍 Résultat brut :\n", response)
 
 
 
 
 
33
 
34
- subject = "Sans objet"
35
- lines = response.splitlines()
36
- for i, line in enumerate(lines):
37
  if line.lower().startswith("objet"):
38
  subject = line.split(":", 1)[-1].strip()
39
- body = "\n".join(lines[i + 1:]).strip()
40
- return subject, body
41
 
42
- return subject, response.strip()
 
1
  from huggingface_hub import InferenceClient
2
  import os
3
 
4
+ # Initialise le client avec le modèle open-source gratuit
5
+ client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
 
 
6
 
7
+ def generate_email(model, to: str, context: str, tone: str) -> tuple[str, str]:
8
+ """
9
+ Génère un objet et un corps d'email à partir du destinataire, du contexte et du ton.
10
+ """
11
+ prompt = f"""
12
+ Tu es un assistant virtuel. Rédige un email destiné à {to}.
13
 
14
  Contexte : {context}
15
  Ton : {tone}
16
 
17
+ Réponds en deux parties :
18
  1. Objet : ligne d'objet de l'email
19
  2. Corps : contenu de l'email bien rédigé, en respectant le ton demandé.
20
  Commence chaque partie par son étiquette.
21
  """
22
 
23
+ response = client.chat_completion(
24
+ model="mistralai/Mistral-7B-Instruct-v0.1",
25
+ messages=[
26
+ {"role": "system", "content": "Tu es un assistant qui rédige des emails."},
27
+ {"role": "user", "content": prompt}
28
+ ],
29
  temperature=0.7,
30
+ max_tokens=800,
 
31
  )
32
 
33
+ content = response.choices[0].message["content"]
34
+
35
+ # Extraire les deux parties de la réponse (objet + corps)
36
+ lines = content.strip().split("\n", 1)
37
+ subject = ""
38
+ body = ""
39
 
40
+ for line in lines:
 
 
41
  if line.lower().startswith("objet"):
42
  subject = line.split(":", 1)[-1].strip()
43
+ else:
44
+ body += line.strip() + "\n"
45
 
46
+ return subject, body.strip()