Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LLama-3 in RAG #959

Open
Dyke-F opened this issue May 2, 2024 · 0 comments
Open

LLama-3 in RAG #959

Dyke-F opened this issue May 2, 2024 · 0 comments

Comments

@Dyke-F
Copy link

Dyke-F commented May 2, 2024

LLama 3 70B performing poorly in RAG:

import dspy
from dspy import Signature
from chroma_db_retriever import ChromadbRM
from rag_config import RAGConfig
rag_config = RAGConfig()

from chromadb.utils import embedding_functions
sentence_transformer_ef = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="BAAI/bge-large-en-v1.5", device="cuda")

class GenQASignature(Signature):
"""Given a detailed patient medical question, generate a generic question that can be used to look up for helpful information in medical databases.
Avoid patient specific information. The question must be generic enough to be useful to retrieve relevant information from medical textbooks, guidelines etc."""
in_question = dspy.InputField(description="A specific medical question.")
out_question = dspy.OutputField(description="A rephrased medical question that is helpful to use as a lookup question in medical textbooks and guidelines to get generic medical information that will help in answering the question.")

class AnswerSignature(Signature):
"""You are a professional medical AI, developed by OpenAI.
Given a medical question and context from medical textbooks, guidelines and other generic medical databases, answer the question as best as you can.
Think step by step, when providing the answer. If you are given multiple options, explain why an option is wrong or right.
If you are given an opion-ended question, provide a structured response.
"""
context = dspy.InputField(description="Truthful, medical context from documents, guidelines, textbooks. Might not always be helpful for the given question.")
question = dspy.InputField(description="The medical question or instruction that must be completed.")
answer = dspy.OutputField(description="Your answer that fully adresses the question or instruction.")

class SimpleRAG(dspy.Module):
def init(self, num_passages=7):
super().init()
self.generate_question = dspy.ChainOfThought(GenQASignature)
self.retrieve = dspy.Retrieve(k=num_passages)
self.generate_answer = dspy.ChainOfThought("context, question -> answer")
#self.generate_answer = dspy.ChainOfThought(AnswerSignature)

def forward(self, question):
    lookup_question = self.generate_question(in_question=question).out_question
    context = self.retrieve(lookup_question).passages
    rag_answer = self.generate_answer(context=context, question=question)
    return dspy.Prediction(answer=rag_answer, reasoning=rag_answer.rationale)

retriever_model = ChromadbRM(
persist_directory="./db",
collection_name="med_db",
embedding_function=sentence_transformer_ef,
k=7,
)

======
A simple query returns:

Prediction(
answer=Prediction(
rationale="Here is the generated text:\n\n---\n\nContext: [1] «The use of these ....\n\nReasoning: Let's think step by step in order to determine ..... We need to",
answer="Here is the generated text:\n\n---\n\nContext: [1] «The use of these .....,
reasoning="Here is the generated text:\n\n---\n\nContext: [1] «The use of these ...... . We need to"
)

..... for brevity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant