Exercise 2: LLM pipeline
Overview
In this exercise, youβll build a simple fact-checking system using OpenAIβs API. Youβll implement a Mixture of Experts (MoE) approach to verify facts and ensure they follow specific guidelines.
Setup
- Open Google Colab
- Create a new notebook
- Install required packages:
!pip install openai python-dotenv
- Set up your OpenAI API key:
from openai import OpenAI
from IPython.display import Markdown, display
from google.colab import userdata
= userdata.get('OPENAI_API_KEY')
OPENAI_API_KEY
= OpenAI(api_key=OPENAI_API_KEY) client
Exercise Tasks
Task 1: Basic Fact Generation
Create a function that takes a question as input and returns a factual answer.
def generate_fact(question):
# Your code here
pass
Task 2: Fact Verification
Implement a function that verifies if the generated answer is correct.
def verify_fact(question, answer):
# Your code here
pass
Task 3: Guidelines Check
Create a function that ensures the answer follows specific guidelines.
def check_guidelines(question, answer, guidelines):
# Your code here
pass
Task 4: Complete MoE Pipeline
Combine all functions into a complete Mixture of Experts pipeline.
def fact_checking_pipeline(question, guidelines):
# Your code here
pass
Testing Your Implementation
Try your implementation with these test cases:
# Test case 1
= "How many moons does Jupiter have?"
question = "Answers should be concise and factual, without speculation."
guidelines
# Test case 2
= "What is the capital of France?"
question = "Answers should be brief and accurate."
guidelines
# Test case 3
= "What is the boiling point of water at sea level?"
question = "Answers should include units and be scientifically accurate." guidelines
Challenge Tasks (Optional)
Multiple Verifications: Modify your pipeline to get multiple verifications and only accept answers that are consistent across multiple checks.
Chain of Thought: Implement a chain-of-thought approach where the system explains its reasoning before giving the final answer.
Error Handling: Add robust error handling for API failures and invalid responses.