[feat][abaoxomtieu]: grammar check route
Browse files- apis/V1/prompts/prompt.py +38 -2
- apis/V1/routes/chat.py +16 -9
apis/V1/prompts/prompt.py
CHANGED
|
@@ -100,8 +100,44 @@ def summarizeNclassify_doc_prompt(docs):
|
|
| 100 |
|
| 101 |
You must return in JSON format, field is mandatory:
|
| 102 |
{{
|
| 103 |
-
summary: a concise summary about the document/paper
|
| 104 |
-
type: must be "Business" or "RnD" based on document content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
}}
|
| 106 |
"""
|
| 107 |
return prompt
|
|
|
|
| 100 |
|
| 101 |
You must return in JSON format, field is mandatory:
|
| 102 |
{{
|
| 103 |
+
"summary": a concise summary about the document/paper
|
| 104 |
+
"type": must be "Business" or "RnD" based on document content
|
| 105 |
+
}}
|
| 106 |
+
"""
|
| 107 |
+
return prompt
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def grammar_check_prompt(sentence):
|
| 111 |
+
prompt = f"""
|
| 112 |
+
# Introduction: You are an expert in English grammar. You are given a text to check grammar.
|
| 113 |
+
# System instructions:
|
| 114 |
+
0. No yapping
|
| 115 |
+
1. Read the text carefully.
|
| 116 |
+
2. Correct the grammar mistakes in the text.
|
| 117 |
+
3. Return the corrected text.
|
| 118 |
+
Examples:
|
| 119 |
+
|
| 120 |
+
1.
|
| 121 |
+
Sentence: "She don't know nothing about the new project, and there is many details that needs to be explained."
|
| 122 |
+
Incorrect: [don't know nothing, is, needs]
|
| 123 |
+
Correct: [doesn't know anything, are, need]
|
| 124 |
+
Corrected sentence: "She doesn't know anything about the new project, and there are many details that need to be explained."
|
| 125 |
+
|
| 126 |
+
2.
|
| 127 |
+
Sentence: "He go to the store every day, but he never buy nothing."
|
| 128 |
+
Incorrect: [go, buy nothing]
|
| 129 |
+
Correct: [goes, buys anything]
|
| 130 |
+
Corrected sentence: "He goes to the store every day, but he never buys anything."
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
***My sentence: {sentence}
|
| 134 |
+
|
| 135 |
+
You must return in JSON format, field is mandatory:
|
| 136 |
+
{{
|
| 137 |
+
"sentence": my origin sentence,
|
| 138 |
+
"incorrect": [list of incorrect words or phrases, it can be repeated],
|
| 139 |
+
"correct": [list of correct words or phrases, it can be repeated],
|
| 140 |
+
"corrected_sentence": corrected sentence
|
| 141 |
}}
|
| 142 |
"""
|
| 143 |
return prompt
|
apis/V1/routes/chat.py
CHANGED
|
@@ -1,20 +1,18 @@
|
|
| 1 |
from fastapi import APIRouter, status
|
| 2 |
from ..utils.response_fmt import jsonResponseFmt
|
| 3 |
from ..configs.thread_config import run_in_thread
|
| 4 |
-
from ..prompts.document_type_clf import chain_type_classify
|
| 5 |
-
from ..prompts.summarize_doc import chain_summarize
|
| 6 |
-
from ..prompts.translate_abstract_BIZ import chain_BIZ
|
| 7 |
-
from ..prompts.translate_abstract_RnD import chain_RnD
|
| 8 |
from ..configs.llm_config import llm
|
| 9 |
from ..prompts.prompt import (
|
| 10 |
classify_document_prompt,
|
| 11 |
summarizeNclassify_doc_prompt,
|
| 12 |
translate_BIZ_prompt,
|
| 13 |
translate_RnD_prompt,
|
|
|
|
| 14 |
)
|
| 15 |
from pydantic import BaseModel, Field
|
| 16 |
from langchain_core.output_parsers import JsonOutputParser
|
| 17 |
import asyncio
|
|
|
|
| 18 |
router = APIRouter(prefix="/llm", tags=["LLM"])
|
| 19 |
|
| 20 |
# @router.post("/classify_document_type", status_code=status.HTTP_200_OK)
|
|
@@ -25,8 +23,11 @@ router = APIRouter(prefix="/llm", tags=["LLM"])
|
|
| 25 |
# result = await run_in_thread(type_classify, text)
|
| 26 |
# return jsonResponseFmt(result)
|
| 27 |
|
|
|
|
| 28 |
class SummaryNClassifyDocument(BaseModel):
|
| 29 |
text: str
|
|
|
|
|
|
|
| 30 |
@router.post("/summary", status_code=status.HTTP_200_OK)
|
| 31 |
async def summarize_doc(body: SummaryNClassifyDocument):
|
| 32 |
prompt = summarizeNclassify_doc_prompt(body.text)
|
|
@@ -43,13 +44,9 @@ class TranslateAbstract(BaseModel):
|
|
| 43 |
|
| 44 |
@router.post("/translate_RnD", status_code=status.HTTP_200_OK)
|
| 45 |
async def translate_RnD(body: TranslateAbstract):
|
| 46 |
-
print("Đã vào translate_RnD")
|
| 47 |
-
await asyncio.sleep(15)
|
| 48 |
result = await run_in_thread(
|
| 49 |
llm.invoke, translate_RnD_prompt(body.abstract, body.sentence, body.word)
|
| 50 |
)
|
| 51 |
-
# result = JsonOutputParser().parse(result)
|
| 52 |
-
print(result)
|
| 53 |
return jsonResponseFmt(result)
|
| 54 |
|
| 55 |
|
|
@@ -59,7 +56,6 @@ async def translate_BIZ(body: TranslateAbstract):
|
|
| 59 |
result = await run_in_thread(
|
| 60 |
translate_BIZ_prompt, body.abstract, body.sentence, body.word
|
| 61 |
)
|
| 62 |
-
# result = JsonOutputParser().parse(result)
|
| 63 |
print(result)
|
| 64 |
return jsonResponseFmt(result)
|
| 65 |
|
|
@@ -77,3 +73,14 @@ async def classify_document_type(body: ClassifyDocumentType):
|
|
| 77 |
result = await run_in_thread(llm.invoke, summarize_doc(body.text))
|
| 78 |
result = JsonOutputParser().parse(result)
|
| 79 |
return jsonResponseFmt(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import APIRouter, status
|
| 2 |
from ..utils.response_fmt import jsonResponseFmt
|
| 3 |
from ..configs.thread_config import run_in_thread
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from ..configs.llm_config import llm
|
| 5 |
from ..prompts.prompt import (
|
| 6 |
classify_document_prompt,
|
| 7 |
summarizeNclassify_doc_prompt,
|
| 8 |
translate_BIZ_prompt,
|
| 9 |
translate_RnD_prompt,
|
| 10 |
+
grammar_check_prompt,
|
| 11 |
)
|
| 12 |
from pydantic import BaseModel, Field
|
| 13 |
from langchain_core.output_parsers import JsonOutputParser
|
| 14 |
import asyncio
|
| 15 |
+
|
| 16 |
router = APIRouter(prefix="/llm", tags=["LLM"])
|
| 17 |
|
| 18 |
# @router.post("/classify_document_type", status_code=status.HTTP_200_OK)
|
|
|
|
| 23 |
# result = await run_in_thread(type_classify, text)
|
| 24 |
# return jsonResponseFmt(result)
|
| 25 |
|
| 26 |
+
|
| 27 |
class SummaryNClassifyDocument(BaseModel):
|
| 28 |
text: str
|
| 29 |
+
|
| 30 |
+
|
| 31 |
@router.post("/summary", status_code=status.HTTP_200_OK)
|
| 32 |
async def summarize_doc(body: SummaryNClassifyDocument):
|
| 33 |
prompt = summarizeNclassify_doc_prompt(body.text)
|
|
|
|
| 44 |
|
| 45 |
@router.post("/translate_RnD", status_code=status.HTTP_200_OK)
|
| 46 |
async def translate_RnD(body: TranslateAbstract):
|
|
|
|
|
|
|
| 47 |
result = await run_in_thread(
|
| 48 |
llm.invoke, translate_RnD_prompt(body.abstract, body.sentence, body.word)
|
| 49 |
)
|
|
|
|
|
|
|
| 50 |
return jsonResponseFmt(result)
|
| 51 |
|
| 52 |
|
|
|
|
| 56 |
result = await run_in_thread(
|
| 57 |
translate_BIZ_prompt, body.abstract, body.sentence, body.word
|
| 58 |
)
|
|
|
|
| 59 |
print(result)
|
| 60 |
return jsonResponseFmt(result)
|
| 61 |
|
|
|
|
| 73 |
result = await run_in_thread(llm.invoke, summarize_doc(body.text))
|
| 74 |
result = JsonOutputParser().parse(result)
|
| 75 |
return jsonResponseFmt(result)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class GrammarCheck(BaseModel):
|
| 79 |
+
sentence: str = Field(description="Sentence to check", min_length=1)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
@router.post("grammar_check", status_code=status.HTTP_200_OK)
|
| 83 |
+
async def grammar_check(body: GrammarCheck):
|
| 84 |
+
result = await run_in_thread(llm.invoke, grammar_check_prompt(body.sentence))
|
| 85 |
+
result = JsonOutputParser().parse(result)
|
| 86 |
+
return jsonResponseFmt(result)
|