Skip to content

Commit

Permalink
Refactor preprocess_text and answer_question functions
Browse files Browse the repository at this point in the history
  • Loading branch information
strickvl committed Apr 8, 2024
1 parent 0662ec5 commit 6f342a5
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions llm-complete-guide/most_basic_rag_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@


def preprocess_text(text):
# Lowercase the text
text = text.lower()
# Remove punctuation
text = text.translate(str.maketrans("", "", string.punctuation))
# Remove extra whitespace
text = re.sub(r"\s+", " ", text).strip()
return text

Expand All @@ -33,28 +30,28 @@ def retrieve_relevant_chunks(query, corpus, top_n=2):


def answer_question(query, corpus, top_n=2):
if relevant_chunks := retrieve_relevant_chunks(query, corpus, top_n):
context = "\n".join(relevant_chunks)

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
chat_completion = client.chat.completions.create(
messages=[
{
"role": "system",
"content": f"Based on the provided context, answer the following question: {query}\n\nContext:\n{context}",
},
{
"role": "user",
"content": query,
},
],
model="gpt-3.5-turbo",
)

return chat_completion.choices[0].message.content.strip()
else:
relevant_chunks = retrieve_relevant_chunks(query, corpus, top_n)
if not relevant_chunks:
return "I don't have enough information to answer the question."

context = "\n".join(relevant_chunks)
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
chat_completion = client.chat.completions.create(
messages=[
{
"role": "system",
"content": f"Based on the provided context, answer the following question: {query}\n\nContext:\n{context}",
},
{
"role": "user",
"content": query,
},
],
model="gpt-3.5-turbo",
)

return chat_completion.choices[0].message.content.strip()


# Sci-fi themed corpus about "ZenML World"
corpus = [
Expand Down

0 comments on commit 6f342a5

Please sign in to comment.