update 2
This commit is contained in:
54
main.py
54
main.py
@@ -1,48 +1,52 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
import requests
|
||||
import yaml
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
app = FastAPI(title="AI CI/CD Assistant")
|
||||
|
||||
OLLAMA_URL = "http://10.10.10.136:11434/api/generate"
|
||||
# ЗАМЕНИТЕ на IP вашего контейнера с Ollama
|
||||
OLLAMA_API_URL = "http://10.10.10.136:11434/api/generate"
|
||||
|
||||
class Prompt(BaseModel):
|
||||
task_description: str
|
||||
class GenerationRequest(BaseModel):
|
||||
description: str
|
||||
platform: str = "GitHub Actions" # или GitLab CI
|
||||
model: str = "codellama"
|
||||
|
||||
def validate_yaml(content: str):
|
||||
def validate_config(config_text: str):
|
||||
"""Простая валидация YAML синтаксиса"""
|
||||
try:
|
||||
yaml.safe_load(content)
|
||||
return True, "Valid YAML"
|
||||
except yaml.YAMLError as exc:
|
||||
return False, str(exc)
|
||||
yaml.safe_load(config_text)
|
||||
return True, "Success"
|
||||
except Exception as e:
|
||||
return False, str(e)
|
||||
|
||||
@app.post("/generate_config")
|
||||
async def generate_config(data: Prompt):
|
||||
@app.post("/generate")
|
||||
async def generate_pipeline(req: GenerationRequest):
|
||||
prompt = f"Write only a valid {req.platform} YAML configuration for: {req.description}. No explanations, just code."
|
||||
|
||||
payload = {
|
||||
"model": data.model,
|
||||
"prompt": f"Generate only a valid CI/CD YAML configuration for the following task: {data.task_description}. Return ONLY the code block.",
|
||||
"model": req.model,
|
||||
"prompt": prompt,
|
||||
"stream": False
|
||||
}
|
||||
|
||||
|
||||
try:
|
||||
response = requests.post(OLLAMA_URL, json=payload)
|
||||
response_data = response.json()
|
||||
generated_text = response_data.get("response", "")
|
||||
response = requests.post(OLLAMA_API_URL, json=payload, timeout=60)
|
||||
raw_text = response.json().get("response", "")
|
||||
|
||||
# Очистка от markdown-тегов, если модель их добавила
|
||||
config_content = generated_text.replace("```yaml", "").replace("```", "").strip()
|
||||
# Очистка от Markdown-оберток (```yaml ... ```)
|
||||
clean_code = raw_text.replace("```yaml", "").replace("```", "").strip()
|
||||
|
||||
is_valid, message = validate_yaml(config_content)
|
||||
is_valid, error_msg = validate_config(clean_code)
|
||||
|
||||
return {
|
||||
"config": config_content,
|
||||
"is_valid": is_valid,
|
||||
"validation_message": message
|
||||
"status": "ok" if is_valid else "error",
|
||||
"config": clean_code,
|
||||
"validation_error": error_msg if not is_valid else None
|
||||
}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
raise HTTPException(status_code=500, detail=f"Ollama error: {str(e)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
Reference in New Issue
Block a user