File size: 2,081 Bytes
a635093
 
108508b
a635093
 
108508b
 
 
a635093
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108508b
 
 
 
 
 
 
 
 
 
a635093
 
 
108508b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a635093
 
 
108508b
 
 
 
a719853
a635093
108508b
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
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from langchain_community.llms import HuggingFacePipeline
from langchain_core.output_parsers import StrOutputParser
from langchain_chroma import Chroma
from langchain_huggingface import HuggingFaceEmbeddings
from langchain import hub
import gradio as gr

# ------------------------------
# MODELO
# ------------------------------
model_id = "mistralai/Mistral-7B-Instruct-v0.1"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id)

pipe = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
    max_new_tokens=512,
    temperature=0.7
)

llm = HuggingFacePipeline(pipeline=pipe)
parser = StrOutputParser()

# ------------------------------
# EMBEDDINGS + CHROMA
# ------------------------------
embedding_function = HuggingFaceEmbeddings(
    model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2",
    model_kwargs={"device": "cpu"}
)

vectordb = Chroma(
    persist_directory="chroma_db",
    embedding_function=embedding_function
)

# ------------------------------
# FUNCI脫N RAG
# ------------------------------
def responder_pregunta(query):
    docs = vectordb.similarity_search_with_score(query, k=5)
    prompt = hub.pull("rlm/rag-prompt")
    rag_chain = prompt | llm | parser

    context = []
    for doc, score in docs:
        if score < 7:
            context.append(doc.page_content)

    if context:
        context_text = "\n".join(context)
        result = rag_chain.invoke({"context": context_text, "question": query})
        return result
    else:
        return "No tengo informaci贸n suficiente para responder a esta pregunta."

# ------------------------------
# INTERFAZ GRADIO
# ------------------------------
gr.Interface(
    fn=responder_pregunta,
    inputs=gr.Textbox(label="Pregunta sobre nutrici贸n"),
    outputs="text",
    title="Sistema RAG sobre Nutrici贸n Cl铆nica",
    description="Haz preguntas sobre el manual cl铆nico procesado con embeddings + Mistral 7B."
).launch()