from openai import OpenAI from app.core.settings import settings class LLMService: def __init__(self): self.client = OpenAI( api_key=settings.openrouter_api_key, base_url="https://openrouter.ai/api/v1", ) def chat( self, question: str, context: str, ) -> str: prompt = self._build_prompt( question, context, ) response = self.client.chat.completions.create( model=settings.llm_model, temperature=0.2, messages=[ { "role": "system", "content": ( "You are an expert software engineer. " "Answer only using the provided repository context." ), }, { "role": "user", "content": prompt, }, ], ) return response.choices[0].message.content def _build_prompt( self, question: str, context: str, ) -> str: return f""" Repository Context ================== {context} ================== Question: {question} Instructions: - Answer only using the repository context. - If the answer cannot be inferred from the context, say so. - Be concise. - Mention function and class names using their qualified names. """