[update][abaoxomtieu]: remove unnecessary componenents
Browse files- apis/V1/prompts/document_type_clf.py +0 -78
- apis/V1/prompts/grammarly.py +0 -82
- apis/V1/prompts/summarize_doc.py +0 -19
- apis/V1/prompts/translate.py +0 -60
- apis/V1/prompts/translate_abstract_BIZ.py +0 -74
- apis/V1/prompts/translate_abstract_RnD.py +0 -71
- apis/V1/prompts/translate_detail.py +1 -1
- apis/V1/prompts/translate_test.py +0 -72
- apis/V1/providers/__init__.py +0 -2
- apis/V1/providers/llm_provider.py +0 -60
apis/V1/prompts/document_type_clf.py
DELETED
|
@@ -1,78 +0,0 @@
|
|
| 1 |
-
from langchain.prompts import PromptTemplate, ChatPromptTemplate
|
| 2 |
-
from langchain_core.output_parsers import JsonOutputParser
|
| 3 |
-
from langchain_core.messages import HumanMessage, SystemMessage
|
| 4 |
-
from ..configs.llm_config import llm
|
| 5 |
-
from ..interfaces.llm_interface import ClassifyDocumentOutput
|
| 6 |
-
|
| 7 |
-
# System message for initial context and instructions
|
| 8 |
-
system_instructions = SystemMessage(
|
| 9 |
-
content="""You are an expert in classifying document types. Identify the type of document based on short content as Business or RnD."""
|
| 10 |
-
)
|
| 11 |
-
|
| 12 |
-
# Example system message to clarify format with examples
|
| 13 |
-
system_examples = SystemMessage(
|
| 14 |
-
content="""
|
| 15 |
-
Examples:
|
| 16 |
-
|
| 17 |
-
1.
|
| 18 |
-
Document:
|
| 19 |
-
"A central goal of machine learning is the development of systems that can solve many problems in as many data domains as possible. Current architectures, however, cannot be applied beyond a small set of stereotyped settings, as they bake in domain & task assumptions or scale poorly to large inputs or outputs. In this work, we propose Perceiver IO, a general-purpose architecture that handles data from arbitrary settings while scaling linearly with the size of inputs and outputs."
|
| 20 |
-
Type: RnD
|
| 21 |
-
|
| 22 |
-
2.
|
| 23 |
-
Document:
|
| 24 |
-
"The global economy is projected to grow at a slower pace this year, as rising inflation and supply chain disruptions continue to impact markets worldwide. Central banks are adjusting their monetary policies in response to these challenges."
|
| 25 |
-
Type: Business
|
| 26 |
-
|
| 27 |
-
3.
|
| 28 |
-
Document:
|
| 29 |
-
"The company’s latest financial report shows a significant increase in net income, driven by cost reduction strategies and increased sales in emerging markets. Investors are optimistic about the future performance given these strong quarterly results."
|
| 30 |
-
Type: Business
|
| 31 |
-
|
| 32 |
-
4.
|
| 33 |
-
Document:
|
| 34 |
-
"Our research introduces a novel approach to quantum computing, focusing on error correction and scalability. The proposed methods significantly improve the stability of qubits, which is crucial for the practical implementation of quantum algorithms."
|
| 35 |
-
Type: RnD
|
| 36 |
-
|
| 37 |
-
5.
|
| 38 |
-
Document:
|
| 39 |
-
"An analysis of the stock market reveals that technology stocks have outperformed other sectors over the past quarter. This trend is attributed to the rapid digital transformation across industries and the increasing demand for tech solutions."
|
| 40 |
-
Type: Business
|
| 41 |
-
|
| 42 |
-
6.
|
| 43 |
-
Document:
|
| 44 |
-
"The startup has launched an innovative mobile app designed to streamline project management for remote teams. The app includes features such as real-time collaboration, task tracking, and performance analytics, making it a comprehensive tool for businesses."
|
| 45 |
-
Type: Business
|
| 46 |
-
"""
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
# Function to create the prompt template dynamically based on input
|
| 50 |
-
def create_prompt(document):
|
| 51 |
-
user_message = HumanMessage(
|
| 52 |
-
content=f"""
|
| 53 |
-
Document: {document}
|
| 54 |
-
Output format:
|
| 55 |
-
"type": type of document,
|
| 56 |
-
|
| 57 |
-
If the document is Business, please type "Business" else type "RnD".
|
| 58 |
-
{{format_instructions}}
|
| 59 |
-
"""
|
| 60 |
-
)
|
| 61 |
-
# Combine all messages into a conversation
|
| 62 |
-
messages = [system_instructions, system_examples, user_message]
|
| 63 |
-
chat_prompt = ChatPromptTemplate(messages=messages)
|
| 64 |
-
|
| 65 |
-
return chat_prompt.format()
|
| 66 |
-
|
| 67 |
-
# Use the JSON output parser
|
| 68 |
-
parser = JsonOutputParser(pydantic_object=ClassifyDocumentOutput)
|
| 69 |
-
|
| 70 |
-
# Create the prompt template
|
| 71 |
-
prompt = PromptTemplate(
|
| 72 |
-
input_variables=["document"],
|
| 73 |
-
template=create_prompt("{document}"),
|
| 74 |
-
partial_variables={"format_instructions": parser.get_format_instructions()},
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
-
# Chain the prompt with the LLM and parser
|
| 78 |
-
chain_type_classify = prompt | llm | parser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apis/V1/prompts/grammarly.py
DELETED
|
@@ -1,82 +0,0 @@
|
|
| 1 |
-
from langchain.prompts import PromptTemplate, ChatPromptTemplate
|
| 2 |
-
from langchain_core.output_parsers import JsonOutputParser
|
| 3 |
-
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
| 4 |
-
from ..configs.llm_config import llm
|
| 5 |
-
from ..interfaces.llm_interface import GrammarlyOutput
|
| 6 |
-
|
| 7 |
-
# System message for initial context and instructions
|
| 8 |
-
system_instructions = SystemMessage(
|
| 9 |
-
content="""You are an expert in checking grammar errors. Identify the errors and provide corrections."""
|
| 10 |
-
)
|
| 11 |
-
|
| 12 |
-
# Example system message to clarify format with examples
|
| 13 |
-
system_examples = SystemMessage(
|
| 14 |
-
content="""
|
| 15 |
-
Examples:
|
| 16 |
-
|
| 17 |
-
1.
|
| 18 |
-
Sentence: "She don't know nothing about the new project, and there is many details that needs to be explained."
|
| 19 |
-
Incorrect: [don't know nothing, is, needs]
|
| 20 |
-
Correct: [doesn't know anything, are, need]
|
| 21 |
-
Corrected sentence: "She doesn't know anything about the new project, and there are many details that need to be explained."
|
| 22 |
-
|
| 23 |
-
2.
|
| 24 |
-
Sentence: "He go to the store every day, but he never buy nothing."
|
| 25 |
-
Incorrect: [go, buy nothing]
|
| 26 |
-
Correct: [goes, buys anything]
|
| 27 |
-
Corrected sentence: "He goes to the store every day, but he never buys anything."
|
| 28 |
-
|
| 29 |
-
3.
|
| 30 |
-
Sentence: "She quickly ran to the store, bought some groceries, and returning home."
|
| 31 |
-
Incorrect: [returning]
|
| 32 |
-
Correct: [returned]
|
| 33 |
-
Corrected sentence: "She quickly ran to the store, bought some groceries, and returned home."
|
| 34 |
-
|
| 35 |
-
4.
|
| 36 |
-
Sentence: "Each of the students have completed their assignments, but the teacher is not satisfied with their works."
|
| 37 |
-
Incorrect: [have, works]
|
| 38 |
-
Correct: [has, work]
|
| 39 |
-
Corrected sentence: "Each of the students has completed their assignments, but the teacher is not satisfied with their work."
|
| 40 |
-
|
| 41 |
-
5.
|
| 42 |
-
Sentence: "If he was more careful, he will not make so many mistakes, which is causing problems for the team."
|
| 43 |
-
Incorrect: [was, will, is]
|
| 44 |
-
Correct: [were, would, are]
|
| 45 |
-
Corrected sentence: "If he were more careful, he would not make so many mistakes, which are causing problems for the team."
|
| 46 |
-
"""
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
# Function to create the prompt template dynamically based on input
|
| 51 |
-
def create_prompt(sentence):
|
| 52 |
-
user_message = HumanMessage(
|
| 53 |
-
content=f"""
|
| 54 |
-
My sentence: {sentence}
|
| 55 |
-
Output format:
|
| 56 |
-
"sentence": my sentence,
|
| 57 |
-
"incorrect": [list of incorrect words or phrases],
|
| 58 |
-
"correct": [list of correct words or phrases],
|
| 59 |
-
"corrected_sentence": corrected sentence
|
| 60 |
-
|
| 61 |
-
If no corrections are needed, return the original sentence as the corrected sentence and empty lists for incorrect and correct.
|
| 62 |
-
{{format_instructions}}
|
| 63 |
-
"""
|
| 64 |
-
)
|
| 65 |
-
# Combine all messages into a conversation
|
| 66 |
-
messages = [system_instructions, system_examples, user_message]
|
| 67 |
-
chat_prompt = ChatPromptTemplate(messages=messages)
|
| 68 |
-
return chat_prompt.format()
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
# Use the JSON output parser
|
| 72 |
-
parser = JsonOutputParser(pydantic_object=GrammarlyOutput)
|
| 73 |
-
|
| 74 |
-
# Create the prompt template
|
| 75 |
-
prompt = PromptTemplate(
|
| 76 |
-
input_variables=["sentence"],
|
| 77 |
-
template=create_prompt("{sentence}"),
|
| 78 |
-
partial_variables={"format_instructions": parser.get_format_instructions()},
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
# Chain the prompt with the LLM and parser
|
| 82 |
-
chain_grammarly = prompt | llm | parser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apis/V1/prompts/summarize_doc.py
DELETED
|
@@ -1,19 +0,0 @@
|
|
| 1 |
-
from ..configs.llm_config import llm
|
| 2 |
-
from langchain_core.output_parsers import JsonOutputParser
|
| 3 |
-
|
| 4 |
-
from langchain_core.prompts import PromptTemplate
|
| 5 |
-
from ..interfaces.llm_interface import ClassifyAndSummarizeOutput
|
| 6 |
-
|
| 7 |
-
# Define prompt
|
| 8 |
-
prompt_template = """Write a concise summary of the following:
|
| 9 |
-
"{text}" and classify the document type as Business or RnD.
|
| 10 |
-
Return the answer as: \n{format_instructions}.
|
| 11 |
-
"""
|
| 12 |
-
parser = JsonOutputParser(pydantic_object=ClassifyAndSummarizeOutput)
|
| 13 |
-
prompt = PromptTemplate.from_template(
|
| 14 |
-
template=prompt_template,
|
| 15 |
-
partial_variables={"format_instructions": parser.get_format_instructions()},
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
# Define LLM chain
|
| 19 |
-
chain_summarize = prompt | llm | parser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apis/V1/prompts/translate.py
DELETED
|
@@ -1,60 +0,0 @@
|
|
| 1 |
-
from langchain.prompts import PromptTemplate, ChatPromptTemplate
|
| 2 |
-
from langchain_core.output_parsers import JsonOutputParser
|
| 3 |
-
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
| 4 |
-
from ..configs.llm_config import llm
|
| 5 |
-
from ..interfaces.llm_interface import TranslateOutput
|
| 6 |
-
|
| 7 |
-
# System message for initial context and instructions
|
| 8 |
-
system_instructions = SystemMessage(
|
| 9 |
-
content="""You are a master in translation. You are given a sentence and a word.
|
| 10 |
-
Translate the word to Vietnamese based on the context of the sentence. """
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
# Example system message to clarify format with examples
|
| 14 |
-
system_examples = SystemMessage(
|
| 15 |
-
content="""
|
| 16 |
-
Example 1:
|
| 17 |
-
My sentence: "outperforms a Transformer-based BERT baseline on the GLUE language benchmark"
|
| 18 |
-
My word: "outperforms"
|
| 19 |
-
Answer: "vượt trội"
|
| 20 |
-
Example 2:
|
| 21 |
-
My sentence: "I love my dog so much, I was interested in him the first time I saw him"
|
| 22 |
-
My word: "so much"
|
| 23 |
-
Answer: "rất nhiều"
|
| 24 |
-
"""
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Function to create the prompt template dynamically based on input
|
| 29 |
-
def create_prompt(sentence, word):
|
| 30 |
-
user_message = HumanMessage(
|
| 31 |
-
content=f"""
|
| 32 |
-
My sentence: {sentence}
|
| 33 |
-
My word: {word}
|
| 34 |
-
|
| 35 |
-
If this word is not in dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 36 |
-
Else, Translate the word to Vietnamese based on the context of the sentence. Return the answer as the translated word.
|
| 37 |
-
I want output to have the format: "answer": translated word, "options": list of options.
|
| 38 |
-
Options is a list of words consisting of the answer and 3 random words which can be used to create flashcards for learning.
|
| 39 |
-
"""
|
| 40 |
-
)
|
| 41 |
-
ai_response = AIMessage(content="Answer: [Your AI will provide the answer here]")
|
| 42 |
-
# Combine all messages into a conversation
|
| 43 |
-
messages = [system_instructions, system_examples, user_message, ai_response]
|
| 44 |
-
chat_prompt = ChatPromptTemplate(messages=messages)
|
| 45 |
-
|
| 46 |
-
return chat_prompt.format()
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# Use the JSON output parser
|
| 50 |
-
parser = JsonOutputParser(pydantic_object=TranslateOutput)
|
| 51 |
-
|
| 52 |
-
# Create the prompt template
|
| 53 |
-
prompt = PromptTemplate(
|
| 54 |
-
input_variables=["sentence", "word"],
|
| 55 |
-
template=create_prompt("{sentence}", "{word}"),
|
| 56 |
-
partial_variables={"format_instructions": parser.get_format_instructions()},
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
# Chain the prompt with the LLM and parser
|
| 60 |
-
chain_translate = prompt | llm | parser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apis/V1/prompts/translate_abstract_BIZ.py
DELETED
|
@@ -1,74 +0,0 @@
|
|
| 1 |
-
from langchain.prompts import PromptTemplate, ChatPromptTemplate
|
| 2 |
-
from langchain_core.output_parsers import JsonOutputParser
|
| 3 |
-
from langchain_core.messages import HumanMessage, SystemMessage
|
| 4 |
-
from ..configs.llm_config import llm
|
| 5 |
-
from ..interfaces.llm_interface import TranslateOutput
|
| 6 |
-
|
| 7 |
-
# System message for initial context and instructions
|
| 8 |
-
system_instructions = SystemMessage(
|
| 9 |
-
content="""You are an expert in translating English into Vietnamese for Finance and Economics topics. Translate the given word or phrase based on the context of the abstract and sentence.
|
| 10 |
-
1. Read the abstract to understand the context.
|
| 11 |
-
2. Translate the given word or phrase into Vietnamese using the context.
|
| 12 |
-
3. If the word or phrase is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 13 |
-
4. If the word or phrase is an acronym, expand it and provide the translation.
|
| 14 |
-
5. Output must be in Vietnamese.
|
| 15 |
-
"""
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
# Example system message to clarify format with examples
|
| 19 |
-
system_examples = SystemMessage(
|
| 20 |
-
content="""
|
| 21 |
-
**Examples:**
|
| 22 |
-
|
| 23 |
-
Abstract: "The efficient market hypothesis (EMH) suggests that financial markets are informationally efficient, meaning that asset prices fully reflect all available information."
|
| 24 |
-
Sentence: "The efficient market hypothesis (EMH) suggests that financial markets are informationally efficient."
|
| 25 |
-
Word: "efficient"
|
| 26 |
-
hiệu quả
|
| 27 |
-
|
| 28 |
-
Abstract: "Quantitative easing (QE) is a monetary policy whereby a central bank purchases government securities or other securities from the market in order to increase the money supply and encourage lending and investment."
|
| 29 |
-
Sentence: "Quantitative easing (QE) is a monetary policy whereby a central bank purchases government securities."
|
| 30 |
-
Word: "Quantitative easing"
|
| 31 |
-
Nới lỏng định lượng
|
| 32 |
-
|
| 33 |
-
Abstract: "Inflation is the rate at which the general level of prices for goods and services rises, eroding purchasing power."
|
| 34 |
-
Sentence: "Inflation is the rate at which the general level of prices for goods and services rises."
|
| 35 |
-
Word: "Inflation"
|
| 36 |
-
lạm phát
|
| 37 |
-
|
| 38 |
-
Abstract: "GDP, or Gross Domestic Product, measures the total value of all goods and services produced within a country in a given period."
|
| 39 |
-
Sentence: "GDP, or Gross Domestic Product, measures the total value of all goods and services produced within a country."
|
| 40 |
-
Word: "GDP"
|
| 41 |
-
Tổng sản phẩm quốc nội
|
| 42 |
-
"""
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# Function to create the prompt template dynamically based on input
|
| 47 |
-
def create_prompt(abstract, sentence, word):
|
| 48 |
-
user_message = HumanMessage(
|
| 49 |
-
content=f"""
|
| 50 |
-
Abstract: {abstract}
|
| 51 |
-
Sentence: {sentence}
|
| 52 |
-
Word: {word}
|
| 53 |
-
|
| 54 |
-
Translate the word to Vietnamese based on the context of the sentence and abstract.
|
| 55 |
-
If the word is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 56 |
-
If it is an acronym, expand and translate it.
|
| 57 |
-
"""
|
| 58 |
-
)
|
| 59 |
-
messages = [system_instructions, system_examples, user_message]
|
| 60 |
-
chat_prompt = ChatPromptTemplate(messages=messages)
|
| 61 |
-
|
| 62 |
-
return chat_prompt.format()
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
# Use the JSON output parser
|
| 66 |
-
|
| 67 |
-
# Create the prompt template
|
| 68 |
-
prompt = PromptTemplate(
|
| 69 |
-
input_variables=["abstract", "sentence", "word"],
|
| 70 |
-
template=create_prompt("{abstract}", "{sentence}", "{word}"),
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
# Chain the prompt with the LLM and parser
|
| 74 |
-
chain_BIZ = prompt | llm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apis/V1/prompts/translate_abstract_RnD.py
DELETED
|
@@ -1,71 +0,0 @@
|
|
| 1 |
-
from langchain.prompts import PromptTemplate, ChatPromptTemplate
|
| 2 |
-
from langchain_core.output_parsers import JsonOutputParser
|
| 3 |
-
from langchain_core.messages import HumanMessage, SystemMessage
|
| 4 |
-
from ..configs.llm_config import llm
|
| 5 |
-
from ..interfaces.llm_interface import TranslateOutput
|
| 6 |
-
|
| 7 |
-
system_instructions = SystemMessage(
|
| 8 |
-
content="""You are an expert in translating English into Vietnamese for R&D topics. Translate the given word or phrase based on the context of the abstract and sentence.
|
| 9 |
-
1. Read the abstract to understand the context.
|
| 10 |
-
2. Translate the given word or phrase into Vietnamese using the context.
|
| 11 |
-
3. If the word or phrase is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 12 |
-
4. If the word or phrase is an acronym, expand it and provide the translation.
|
| 13 |
-
5. Output must be in Vietnamese.
|
| 14 |
-
"""
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
system_examples = SystemMessage(
|
| 18 |
-
content="""
|
| 19 |
-
**Examples:**
|
| 20 |
-
|
| 21 |
-
Abstract: "In recent years, there has been significant progress in the development of Transformer-based models for natural language processing tasks. These models, such as BERT, have set new benchmarks in various language understanding tasks."
|
| 22 |
-
Sentence: "outperforms a Transformer-based BERT baseline on the GLUE language benchmark"
|
| 23 |
-
Word: "outperforms"
|
| 24 |
-
vượt trội
|
| 25 |
-
|
| 26 |
-
Abstract: "This paper explores the use of reinforcement learning in optimizing neural network architectures. We demonstrate that our approach achieves state-of-the-art results on several benchmark datasets."
|
| 27 |
-
Sentence: "Our approach achieves state-of-the-art results on several benchmark datasets."
|
| 28 |
-
Word: "approach"
|
| 29 |
-
phương pháp
|
| 30 |
-
|
| 31 |
-
Abstract: "The integration of machine learning techniques in medical diagnosis has shown promising results. Our study focuses on the application of deep learning to detect early signs of diseases."
|
| 32 |
-
Sentence: "The integration of machine learning techniques in medical diagnosis has shown promising results."
|
| 33 |
-
Word: "integration"
|
| 34 |
-
tích hợp
|
| 35 |
-
|
| 36 |
-
Abstract: "Transformer-based models like BERT have transformed NLP tasks by achieving new state-of-the-art results."
|
| 37 |
-
Sentence: "Transformer-based models like BERT have transformed NLP tasks."
|
| 38 |
-
Word: "BERT"
|
| 39 |
-
Bidirectional Encoder Representations from Transformers
|
| 40 |
-
"""
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
def create_prompt(abstract, sentence, word):
|
| 45 |
-
user_message = HumanMessage(
|
| 46 |
-
content=f"""
|
| 47 |
-
Abstract: {abstract}
|
| 48 |
-
Sentence: {sentence}
|
| 49 |
-
Word: {word}
|
| 50 |
-
|
| 51 |
-
Translate the word to Vietnamese based on the context of the sentence and abstract.
|
| 52 |
-
If the word is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 53 |
-
If it is an acronym, expand and translate it.
|
| 54 |
-
"""
|
| 55 |
-
)
|
| 56 |
-
messages = [system_instructions, system_examples, user_message]
|
| 57 |
-
chat_prompt = ChatPromptTemplate(messages=messages)
|
| 58 |
-
|
| 59 |
-
return chat_prompt.format()
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
# Use the JSON output parser
|
| 63 |
-
|
| 64 |
-
# Create the prompt template
|
| 65 |
-
prompt = PromptTemplate(
|
| 66 |
-
input_variables=["abstract", "sentence", "word"],
|
| 67 |
-
template=create_prompt("{abstract}", "{sentence}", "{word}"),
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
# Chain the prompt with the LLM and parser
|
| 71 |
-
chain_RnD = prompt | llm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apis/V1/prompts/translate_detail.py
CHANGED
|
@@ -23,7 +23,7 @@ Explanation: "Thuật toán gradient descent là một phương pháp tối ưu
|
|
| 23 |
For the word "overfitting":
|
| 24 |
Explanation: "Overfitting là hiện tượng một mô hình học máy biểu hiện quá mức các dữ liệu huấn luyện, làm giảm khả năng dự đoán chính xác dữ liệu mới. Điều này thường xảy ra khi mô hình quá phức tạp so với dữ liệu."
|
| 25 |
|
| 26 |
-
Now, follow the steps and provide the explanation in Vietnamese for the word "{word}".
|
| 27 |
Explanation:
|
| 28 |
"""
|
| 29 |
|
|
|
|
| 23 |
For the word "overfitting":
|
| 24 |
Explanation: "Overfitting là hiện tượng một mô hình học máy biểu hiện quá mức các dữ liệu huấn luyện, làm giảm khả năng dự đoán chính xác dữ liệu mới. Điều này thường xảy ra khi mô hình quá phức tạp so với dữ liệu."
|
| 25 |
|
| 26 |
+
Now, follow the steps and provide the explanation in Vietnamese for the word: "{word}".
|
| 27 |
Explanation:
|
| 28 |
"""
|
| 29 |
|
apis/V1/prompts/translate_test.py
DELETED
|
@@ -1,72 +0,0 @@
|
|
| 1 |
-
from langchain.prompts import PromptTemplate, ChatPromptTemplate
|
| 2 |
-
from langchain_core.output_parsers import JsonOutputParser
|
| 3 |
-
from langchain_core.messages import HumanMessage, SystemMessage
|
| 4 |
-
from ..configs.llm_config import llm
|
| 5 |
-
from pydantic import BaseModel, Field
|
| 6 |
-
|
| 7 |
-
class TranslateOutput(BaseModel):
|
| 8 |
-
answer: str = Field(description="translated word")
|
| 9 |
-
word: str = Field(description="word to be translated")
|
| 10 |
-
|
| 11 |
-
system_instructions = SystemMessage(
|
| 12 |
-
content="""You are a master at translating English into Vietnamese. You are provided with an abstract of a paper, a sentence containing the word you want to translate, and the word itself. Translate the word into Vietnamese based on the context of the document and the sentence.
|
| 13 |
-
1. Read the abstract to understand the context.
|
| 14 |
-
2. Use the context to translate the given word into Vietnamese.
|
| 15 |
-
3. If the word is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 16 |
-
4. If the word is an acronym, expand it and provide the translation.
|
| 17 |
-
"""
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
system_examples = SystemMessage(
|
| 21 |
-
content="""
|
| 22 |
-
**Examples:**
|
| 23 |
-
|
| 24 |
-
Abstract: "In recent years, there has been significant progress in the development of Transformer-based models for natural language processing tasks. These models, such as BERT, have set new benchmarks in various language understanding tasks."
|
| 25 |
-
Sentence: "outperforms a Transformer-based BERT baseline on the GLUE language benchmark"
|
| 26 |
-
Word: "outperforms"
|
| 27 |
-
Answer: "vượt trội"
|
| 28 |
-
|
| 29 |
-
Abstract: "This paper explores the use of reinforcement learning in optimizing neural network architectures. We demonstrate that our approach achieves state-of-the-art results on several benchmark datasets."
|
| 30 |
-
Sentence: "Our approach achieves state-of-the-art results on several benchmark datasets."
|
| 31 |
-
Word: "approach"
|
| 32 |
-
Answer: "phương pháp"
|
| 33 |
-
|
| 34 |
-
Abstract: "The integration of machine learning techniques in medical diagnosis has shown promising results. Our study focuses on the application of deep learning to detect early signs of diseases."
|
| 35 |
-
Sentence: "The integration of machine learning techniques in medical diagnosis has shown promising results."
|
| 36 |
-
Word: "integration"
|
| 37 |
-
Answer: "tích hợp"
|
| 38 |
-
|
| 39 |
-
Abstract: "Transformer-based models like BERT have transformed NLP tasks by achieving new state-of-the-art results."
|
| 40 |
-
Sentence: "Transformer-based models like BERT have transformed NLP tasks."
|
| 41 |
-
Word: "BERT"
|
| 42 |
-
Answer: "Bidirectional Encoder Representations from Transformers"
|
| 43 |
-
"""
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
def create_prompt(abstract, sentence, word):
|
| 47 |
-
user_message = HumanMessage(
|
| 48 |
-
content=f"""
|
| 49 |
-
Abstract: {abstract}
|
| 50 |
-
Sentence: {sentence}
|
| 51 |
-
Word: {word}
|
| 52 |
-
|
| 53 |
-
Translate the word to Vietnamese based on the context of the sentence and abstract.
|
| 54 |
-
If the word is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 55 |
-
If it is an acronym, expand and translate it.
|
| 56 |
-
Return the answer as: "answer": Vietnamese word, "word": input word (English).
|
| 57 |
-
"""
|
| 58 |
-
)
|
| 59 |
-
messages = [system_instructions, system_examples, user_message]
|
| 60 |
-
chat_prompt = ChatPromptTemplate(messages=messages)
|
| 61 |
-
|
| 62 |
-
return chat_prompt.format()
|
| 63 |
-
|
| 64 |
-
parser = JsonOutputParser(pydantic_object=TranslateOutput)
|
| 65 |
-
|
| 66 |
-
prompt = PromptTemplate(
|
| 67 |
-
input_variables=["abstract", "sentence", "word"],
|
| 68 |
-
template=create_prompt("{abstract}", "{sentence}", "{word}"),
|
| 69 |
-
partial_variables={"format_instructions": parser.get_format_instructions()},
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
chain_test_RnD = prompt | llm | parser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apis/V1/providers/__init__.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
from .jwt_provider import JWTProvider
|
| 2 |
-
from .llm_provider import chain
|
| 3 |
|
| 4 |
jwt = JWTProvider()
|
| 5 |
-
llm = chain
|
|
|
|
| 1 |
from .jwt_provider import JWTProvider
|
|
|
|
| 2 |
|
| 3 |
jwt = JWTProvider()
|
|
|
apis/V1/providers/llm_provider.py
DELETED
|
@@ -1,60 +0,0 @@
|
|
| 1 |
-
from langchain.prompts import PromptTemplate, ChatPromptTemplate
|
| 2 |
-
from langchain_core.output_parsers import JsonOutputParser
|
| 3 |
-
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
| 4 |
-
from ..configs.llm_config import llm
|
| 5 |
-
from ..interfaces.llm_interface import TranslateOutput
|
| 6 |
-
|
| 7 |
-
# System message for initial context and instructions
|
| 8 |
-
system_instructions = SystemMessage(
|
| 9 |
-
content="""You are a master in translation. You are given a sentence and a word.
|
| 10 |
-
Translate the word to Vietnamese based on the context of the sentence. """
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
# Example system message to clarify format with examples
|
| 14 |
-
system_examples = SystemMessage(
|
| 15 |
-
content="""
|
| 16 |
-
Example 1:
|
| 17 |
-
My sentence: "outperforms a Transformer-based BERT baseline on the GLUE language benchmark"
|
| 18 |
-
My word: "outperforms"
|
| 19 |
-
Answer: "vượt trội"
|
| 20 |
-
Example 2:
|
| 21 |
-
My sentence: "I love my dog so much, I was interested in him the first time I saw him"
|
| 22 |
-
My word: "so much"
|
| 23 |
-
Answer: "rất nhiều"
|
| 24 |
-
"""
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Function to create the prompt template dynamically based on input
|
| 29 |
-
def create_prompt(sentence, word):
|
| 30 |
-
user_message = HumanMessage(
|
| 31 |
-
content=f"""
|
| 32 |
-
My sentence: {sentence}
|
| 33 |
-
My word: {word}
|
| 34 |
-
|
| 35 |
-
If this word is not in dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 36 |
-
Else, Translate the word to Vietnamese based on the context of the sentence. Return the answer as the translated word.
|
| 37 |
-
I want output to have the format: "word": input word ,"answer": translated word, "options": list of options.
|
| 38 |
-
Options is a list of words consisting of the answer and 3 random words which can be used to create flashcards for learning.
|
| 39 |
-
"""
|
| 40 |
-
)
|
| 41 |
-
ai_response = AIMessage(content="Answer: [Your AI will provide the answer here]")
|
| 42 |
-
# Combine all messages into a conversation
|
| 43 |
-
messages = [system_instructions, system_examples, user_message, ai_response]
|
| 44 |
-
chat_prompt = ChatPromptTemplate(messages=messages)
|
| 45 |
-
|
| 46 |
-
return chat_prompt.format()
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# Use the JSON output parser
|
| 50 |
-
parser = JsonOutputParser(pydantic_object=TranslateOutput)
|
| 51 |
-
|
| 52 |
-
# Create the prompt template
|
| 53 |
-
prompt = PromptTemplate(
|
| 54 |
-
input_variables=["sentence", "word"],
|
| 55 |
-
template=create_prompt("{sentence}", "{word}"),
|
| 56 |
-
partial_variables={"format_instructions": parser.get_format_instructions()},
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
# Chain the prompt with the LLM and parser
|
| 60 |
-
chain = prompt | llm | parser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|