CoreQuest-AI / ragout.py
AIbyKaindu's picture
Update ragout.py
2ff8877 verified
Raw
History Blame Contribute Delete
3.23 kB
import os
import requests
DOCUMENTS = {
"Newton's Laws": "Newton's Laws: 1. Inertia, 2. F=ma, 3. Action-Reaction. Core to motion physics.",
"Derivatives": "Derivatives measure rate of change. Example: f(x) = x² -> f'(x) = 2x.",
"Electric Circuits": "Electric circuits involve voltage, current (I), resistance (R). Ohm’s law: V = IR."
}
def retrieve_context(user_question):
for topic, notes in DOCUMENTS.items():
if topic.lower() in user_question.lower():
return notes
return "General STEM principles: Newtonian mechanics, calculus, electromagnetism."
def learning_galaxy_answer(question):
context = retrieve_context(question)
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
headers = {"Authorization": f"Bearer {os.environ['HF_Token']}"}
prompt = f"""You are Learning Galaxy, a genius physics and math tutor. Use the context below to answer carefully.
Context:
{context}
Question:
{question}
Answer:"""
payload = {"inputs": prompt}
response = requests.post(API_URL, headers=headers, json=payload)
result = response.json()
try:
if isinstance(result, list) and "generated_text" in result[0]:
return result[0]["generated_text"]
else:
return "⚠️ Couldn't generate a good answer. Try again."
except Exception as e:
return f"⚠️ API Error: {e}"
DOCUMENTS = {
"Newton's Laws": "Newton's Laws describe the relationship between a body and the forces acting upon it.",
"Kinematics": "Kinematics studies motion without considering its causes.",
"Dynamics": "Dynamics concerns forces and their effect on motion.",
"Work and Energy": "Work is force applied over a distance. Energy is the capacity to do work.",
"Thermodynamics": "Study of heat, energy, and the transformation between them.",
"Electric Circuits": "Circuits allow current to flow through a closed loop of conductors.",
"Magnetism": "Magnetism arises from the motion of electric charges.",
"Waves and Oscillations": "Waves transport energy through space without transport of matter.",
"Optics": "Optics is the study of light and its interactions with materials.",
"Special Relativity": "Relativity describes how time and space are linked for objects moving at constant speeds.",
"Calculus": "Calculus is the study of continuous change through derivatives and integrals.",
"Algebra": "Algebra involves solving equations and finding unknowns.",
"Geometry": "Geometry studies the properties of shapes and spaces.",
"Probability and Statistics": "Probability measures chance, and statistics analyze data.",
"Vectors and Scalars": "Scalars have magnitude; vectors have magnitude and direction.",
"Gravitation": "Gravity is the force that attracts two bodies toward each other.",
"Fluid Mechanics": "Fluids exert forces and flows that follow physical laws.",
"Quantum Mechanics": "Quantum physics studies very small particles and probabilities.",
"Nuclear Physics": "Nuclear physics studies atomic nuclei and their properties.",
"Modern Physics": "Modern physics explores theories beyond classical mechanics.",
}