api_get_article2.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. from typing import List, Optional
  3. from fastapi import Request, APIRouter, BackgroundTasks
  4. from pydantic import BaseModel, Field, conint
  5. from core.respone_format import *
  6. from gpt.get_article2 import GetArticle
  7. from tools.loglog import log_err_e
  8. router = APIRouter()
  9. get_article = GetArticle()
  10. class Word(BaseModel):
  11. meaning_id: int = Field(..., description="单词的词义id")
  12. word_id: int = Field(..., description="单词id")
  13. spell: str = Field(..., description="单词的拼写")
  14. meaning: str = Field(..., description="单词的意思")
  15. class ArticleRequest(BaseModel):
  16. core_words: List[Word] = Field(..., description="单词列表")
  17. take_count: int = 2
  18. demo_name: Optional[str] = "无"
  19. reading_level: conint(ge=1, le=30) = Field(default=10, description="阅读水平,默认值为10;[8,16,24]小学初中高中")
  20. article_length: int = Field(default=None, description="需要生成的文章长度,可以不传,不传自己根据reading_level判断")
  21. exercise_id: int = Field(default=0, description="学案ID,用于日志快速定位")
  22. @router.post("/article/reading-comprehension")
  23. def post_article(
  24. json_data: ArticleRequest,
  25. request: Request,
  26. background_tasks: BackgroundTasks,
  27. ):
  28. json_data = json_data.model_dump()
  29. real_ip = request.headers.get("X-Real-IP", "0.0.0.0")
  30. core_words = json_data["core_words"]
  31. take_count = json_data["take_count"]
  32. demo_name = json_data["demo_name"]
  33. reading_level = json_data["reading_level"]
  34. article_length = json_data["article_length"]
  35. exercise_id = json_data["exercise_id"]
  36. try:
  37. r = get_article.submit_task(
  38. real_ip=real_ip,
  39. core_words=core_words,
  40. take_count=take_count,
  41. demo_name=demo_name,
  42. reading_level=reading_level,
  43. article_length=article_length,
  44. exercise_id=exercise_id,
  45. background_tasks=background_tasks
  46. )
  47. return r if not isinstance(r, str) else resp_500(message=r)
  48. except Exception as e:
  49. log_err_e(e, msg="文章2接口错误/article/reading-comprehension;")
  50. return resp_500(message=f"{type(e).__name__},{e}")