MefhigosetH commited on
Commit
b6a664f
·
1 Parent(s): 66f7e10

Implementamos Llama.cpp y HuggingFace Hub.

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. Pipfile +2 -0
  3. app.py +23 -22
.gitignore CHANGED
@@ -1,2 +1,3 @@
1
  .env
 
2
  Pipfile.lock
 
1
  .env
2
+ *.gguf
3
  Pipfile.lock
Pipfile CHANGED
@@ -10,6 +10,8 @@ langchain-huggingface = "*"
10
  langchain = "*"
11
  langchain-core = "*"
12
  transformers = "*"
 
 
13
 
14
  [dev-packages]
15
 
 
10
  langchain = "*"
11
  langchain-core = "*"
12
  transformers = "*"
13
+ llama-cpp-python = "*"
14
+ langchain-community = "*"
15
 
16
  [dev-packages]
17
 
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
- from langchain_huggingface import HuggingFaceEndpoint, HuggingFacePipeline
3
- from langchain_core.prompts import PromptTemplate
 
4
  from langchain.globals import set_verbose, set_debug
5
  import os
6
 
@@ -10,17 +11,18 @@ def isDevelopmentEnv():
10
 
11
 
12
  def initPrompt():
13
- template = """[INST]Tu eres Harry Potter, el estudiante de magia más hábil de todo el mundo mágico.
14
  Responde amablemente a la consulta del usuario basado en la información disponible y a las siguientes reglas:
15
  1. Si no sabes la respuesta, pide al usuario que intente reformular su consulta.
16
  2. Responde siempre en idioma Español.
17
  3. Da respuestas únicamente relacionadas al mundo mágico.
18
-
19
- Consulta: {question}
20
- [/INST]
21
  """
22
 
23
- prompt = PromptTemplate.from_template(template)
 
 
 
 
24
  return prompt
25
 
26
 
@@ -30,21 +32,19 @@ def initLLM():
30
  Inicializamos el modelo LLM.
31
 
32
  Otros modelos que podríamos usar:
33
- - meta-llama/Meta-Llama-3.1-8B-Instruct
34
- - HuggingFaceH4/zephyr-7b-beta
35
  """
36
 
37
- model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
38
-
39
- llm = HuggingFaceEndpoint(
40
- repo_id = model_id,
41
- task = "text-generation",
42
- temperature = 0.5,
43
- model_kwargs = {
44
- "min_length": 200,
45
- "max_length": 2000,
46
- "num_return_sequences": 1
47
- }
48
  )
49
 
50
  return llm
@@ -55,11 +55,12 @@ def respond(message, history):
55
  response = ""
56
 
57
  try:
58
- response = llm_chain.invoke(message)
59
  except:
60
  raise gradio.Error("Se ha producido un error al interactuar con el modelo LLM.", duratio=5)
61
 
62
- return response
 
63
 
64
 
65
 
 
1
  import gradio as gr
2
+ from huggingface_hub import hf_hub_download
3
+ from langchain_community.chat_models import ChatLlamaCpp
4
+ from langchain_core.prompts import ChatPromptTemplate
5
  from langchain.globals import set_verbose, set_debug
6
  import os
7
 
 
11
 
12
 
13
  def initPrompt():
14
+ system_prompt = """Tu eres Harry Potter, el estudiante de magia más hábil de todo el mundo mágico.
15
  Responde amablemente a la consulta del usuario basado en la información disponible y a las siguientes reglas:
16
  1. Si no sabes la respuesta, pide al usuario que intente reformular su consulta.
17
  2. Responde siempre en idioma Español.
18
  3. Da respuestas únicamente relacionadas al mundo mágico.
 
 
 
19
  """
20
 
21
+ prompt = ChatPromptTemplate.from_messages([
22
+ ("system", system_prompt),
23
+ ("human", "{input}"),
24
+ ])
25
+
26
  return prompt
27
 
28
 
 
32
  Inicializamos el modelo LLM.
33
 
34
  Otros modelos que podríamos usar:
35
+ * bartowski/Llama-3.2-1B-Instruct-GGUF
36
+ * HuggingFaceH4/zephyr-7b-beta
37
  """
38
 
39
+ model_path = hf_hub_download(repo_id="bartowski/Llama-3.2-1B-Instruct-GGUF", filename="Llama-3.2-1B-Instruct-Q5_K_S.gguf")
40
+
41
+ llm = ChatLlamaCpp(
42
+ model_path=model_path,
43
+ temperature=0.7,
44
+ max_tokens=500,
45
+ top_p=1,
46
+ # callback_manager=callback_manager,
47
+ # verbose=True, # Verbose is required to pass to the callback manager
 
 
48
  )
49
 
50
  return llm
 
55
  response = ""
56
 
57
  try:
58
+ response = llm_chain.invoke({"input": message})
59
  except:
60
  raise gradio.Error("Se ha producido un error al interactuar con el modelo LLM.", duratio=5)
61
 
62
+ print(response)
63
+ return response.content
64
 
65
 
66