Spaces:
Sleeping
Sleeping
| import os | |
| from groq import Groq | |
| def analyze_reaction(reactants: str, reagents: str, conditions: str) -> str: | |
| api_key = os.getenv("GROQ_API_KEY") | |
| if not api_key: | |
| raise RuntimeError("GROQ_API_KEY environment variable is not set.") | |
| client = Groq(api_key=api_key) | |
| system_prompt = ( | |
| "You are an expert chemistry teaching assistant. " | |
| "You analyze chemical reactions for educational purposes only. " | |
| "Predict likely reaction outcomes and explain the mechanism conceptually. " | |
| ) | |
| user_prompt = f""" | |
| Reactants: | |
| {reactants} | |
| Reagents / Catalysts: | |
| {reagents} | |
| Reaction Conditions: | |
| {conditions} | |
| Tasks: | |
| 1. Predict the major product(s) | |
| 2. Mention possible side products (if any) | |
| 3. Explain the reaction mechanism in simple terms | |
| 4. Explain why the given conditions matter | |
| Use clear headings and educational language. | |
| """ | |
| completion = client.chat.completions.create( | |
| model="llama-3.1-8b-instant", | |
| messages=[ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": user_prompt} | |
| ], | |
| temperature=0.3, | |
| max_tokens=800, | |
| ) | |
| return completion.choices[0].message.content | |