AndreaAlessandrelli4 commited on
Commit
68186c8
·
verified ·
1 Parent(s): 5aeefb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -40
app.py CHANGED
@@ -26,44 +26,44 @@ if torch.cuda.is_available():
26
  tokenizer.use_default_system_prompt = False
27
 
28
 
29
- key='4vNfIDO8PmFwCloxA40y2b4PSHm62vmcuPoM'
30
- url = "https://mmchpi0yssanukk5t3ofta.c0.europe-west3.gcp.weaviate.cloud"
31
- client = weaviate.Client(
32
- url = url,
33
- auth_client_secret=weaviate.auth.AuthApiKey(api_key=key),
34
- )
35
-
36
-
37
- def prompt_template(materiali, query):
38
- mat = ''
39
- for i, doc in enumerate(materiali):
40
- mat += f"""DOCUMENTO {i+1}: {doc["contenuto"]};\n"""
41
- prompt_template = f"""
42
- Basandoti sulle tue conoscenze e usando le informazioni che ti fornisco di seguito.
43
- CONTESTO:
44
- {mat}
45
 
46
- Rispondi alla seguente domanda in modo esaustivo e conciso in massimo 100 parole, evitando inutili giri di parole o ripetizioni, .
47
- {query}
48
- """
49
- return prompt_template
50
 
51
 
52
 
53
- def richiamo_materiali(query, vett_query, alpha=1.0, N_items=5):
54
- try:
55
- materiali = client.query.get("Default", ["content"]).with_hybrid(
56
- query=text_query,
57
- vector=vett_query,
58
- alpha=alpha,
59
- fusion_type=HybridFusion.RELATIVE_SCORE,
60
- ).with_additional(["score"]).with_limit(N_items).do()
61
-
62
- mat = [{"score":i["_additional"]["score"],'contenuto':i["content"]} for i in materiali["data"]["Get"]["Default"]]
63
- except:
64
- mat =[{"score":0, "contenuto":'NESSUN MATERIALE FORNITO'}]
65
-
66
- return mat
67
 
68
 
69
 
@@ -79,11 +79,11 @@ def generate(
79
  top_k: int = 50,
80
  repetition_penalty: float = 1.2,
81
  ) -> Iterator[str]:
82
- model1 = SentenceTransformer('intfloat/multilingual-e5-large')
83
- embeddings_query = model1.encode('query: '+str(message), normalize_embeddings=True)
84
- vettor_query = embeddings_query
85
- materiali = richiamo_materiali(message, vettor_query)
86
- prompt_finale = prompt_template(materiali, message)
87
  conversation = []
88
  conversation.append({"role": "system", "content":
89
  '''Sei un an assistente AI di nome 'AvvoChat' specializzato nel rispondere a domande riguardanti la legge Italiana.
@@ -92,7 +92,7 @@ def generate(
92
  Firmati alla fine di ogni risposta '-AvvoChat'.'''})
93
  for user, assistant in chat_history:
94
  conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
95
- conversation.append({"role": "user", "content": prompt_finale})
96
 
97
  input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
98
  if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
 
26
  tokenizer.use_default_system_prompt = False
27
 
28
 
29
+ #key='4vNfIDO8PmFwCloxA40y2b4PSHm62vmcuPoM'
30
+ #url = "https://mmchpi0yssanukk5t3ofta.c0.europe-west3.gcp.weaviate.cloud"
31
+ #client = weaviate.Client(
32
+ # url = url,
33
+ # auth_client_secret=weaviate.auth.AuthApiKey(api_key=key),
34
+ #)
35
+
36
+
37
+ #def prompt_template(materiali, query):
38
+ # mat = ''
39
+ # for i, doc in enumerate(materiali):
40
+ # mat += f"""DOCUMENTO {i+1}: {doc["contenuto"]};\n"""
41
+ # prompt_template = f"""
42
+ # Basandoti sulle tue conoscenze e usando le informazioni che ti fornisco di seguito.
43
+ # CONTESTO:
44
+ # {mat}
45
 
46
+ # Rispondi alla seguente domanda in modo esaustivo e conciso in massimo 100 parole, evitando inutili giri di parole o ripetizioni, .
47
+ # {query}
48
+ # """
49
+ # return prompt_template
50
 
51
 
52
 
53
+ #def richiamo_materiali(query, vett_query, alpha=1.0, N_items=5):
54
+ # try:
55
+ # materiali = client.query.get("Default", ["content"]).with_hybrid(
56
+ # query=text_query,
57
+ # vector=vett_query,
58
+ # alpha=alpha,
59
+ # fusion_type=HybridFusion.RELATIVE_SCORE,
60
+ # ).with_additional(["score"]).with_limit(N_items).do()
61
+ #
62
+ # mat = [{"score":i["_additional"]["score"],'contenuto':i["content"]} for i in materiali["data"]["Get"]["Default"]]
63
+ # except:
64
+ # mat =[{"score":0, "contenuto":'NESSUN MATERIALE FORNITO'}]
65
+ #
66
+ # return mat
67
 
68
 
69
 
 
79
  top_k: int = 50,
80
  repetition_penalty: float = 1.2,
81
  ) -> Iterator[str]:
82
+ #model1 = SentenceTransformer('intfloat/multilingual-e5-large')
83
+ #embeddings_query = model1.encode('query: '+str(message), normalize_embeddings=True)
84
+ #vettor_query = embeddings_query
85
+ #materiali = richiamo_materiali(message, vettor_query)
86
+ #prompt_finale = prompt_template(materiali, message)
87
  conversation = []
88
  conversation.append({"role": "system", "content":
89
  '''Sei un an assistente AI di nome 'AvvoChat' specializzato nel rispondere a domande riguardanti la legge Italiana.
 
92
  Firmati alla fine di ogni risposta '-AvvoChat'.'''})
93
  for user, assistant in chat_history:
94
  conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
95
+ conversation.append({"role": "user", "content": message})
96
 
97
  input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
98
  if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH: