class PromptGenerationService:
def __init__(self, model_name: str = 'mistral'):
self.model_name = model_name
self.api_url = "http://localhost:11434/api/generate"
async def generate_prompt(self, context: PromptContext) -> str:
prompt = f"""
Task: Create a detailed prompt for the following context:
- Task Type: {context.task}
- Domain: {context.domain}
- Requirements: {', '.join(context.requirements)}
Generate a structured prompt that includes:
1. Context setting
2. Specific requirements
3. Output format
4. Constraints
5. Examples (if applicable)
"""
response = requests.post(
self.api_url,
json={
"model": self.model_name,
"prompt": prompt,
"stream": False
}
)
return response.json()["response"]
async def optimize_prompt(self, original_prompt: str) -> Dict:
prompt = f"""
Analyze and optimize the following prompt:
"{original_prompt}"
Provide:
1. Improved version
2. Explanation of changes
3. Potential variations
"""
response = requests.post(
self.api_url,
json={
"model": self.model_name,
"prompt": prompt,
"stream": False
}
)
return response.json()["response"]
|