luanpoppe commited on
Commit
3251505
·
1 Parent(s): 4cd3056

feat: colocando o output do resumo como json

Browse files
langchain_backend/main.py CHANGED
@@ -1,5 +1,5 @@
1
  import os
2
- from langchain_backend.utils import create_prompt_llm_chain, create_retriever, getPDF, create_llm
3
  from langchain_backend import utils
4
  from langchain.chains import create_retrieval_chain
5
  from langchain_huggingface import HuggingFaceEmbeddings
@@ -43,7 +43,7 @@ def get_llm_answer_summary(system_prompt, user_prompt, pdf_url, model, isIterati
43
  print('\n\n\n')
44
  pages = getPDF(pdf_url)
45
  if not isIterativeRefinement:
46
- rag_chain = create_prompt_llm_chain(system_prompt, model)
47
 
48
  results = rag_chain.invoke({"input": user_prompt, "context": pages})
49
 
 
1
  import os
2
+ from langchain_backend.utils import create_prompt_llm_chain, create_retriever, getPDF, create_llm, create_prompt_llm_chain_summary
3
  from langchain_backend import utils
4
  from langchain.chains import create_retrieval_chain
5
  from langchain_huggingface import HuggingFaceEmbeddings
 
43
  print('\n\n\n')
44
  pages = getPDF(pdf_url)
45
  if not isIterativeRefinement:
46
+ rag_chain = create_prompt_llm_chain_summary(system_prompt, model)
47
 
48
  results = rag_chain.invoke({"input": user_prompt, "context": pages})
49
 
langchain_backend/utils.py CHANGED
@@ -8,6 +8,9 @@ from langchain_core.prompts import ChatPromptTemplate
8
  from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEmbeddings
9
  from setup.environment import default_model
10
  from uuid import uuid4
 
 
 
11
 
12
  os.environ["LANGCHAIN_TRACING_V2"]="true"
13
  os.environ["LANGCHAIN_ENDPOINT"]="https://api.smith.langchain.com"
@@ -78,17 +81,37 @@ def create_llm(modelParam):
78
  )
79
 
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  DEFAULT_SYSTEM_PROMPT = """
82
 
83
  You are a highly knowledgeable legal assistant specializing in case summarization. Your task is to provide comprehensive and accurate summaries of legal cases while maintaining a professional and objective demeanor. Always approach each case with careful consideration and analytical rigor.
84
 
85
  First, you will be given a document to analyze:
86
 
87
- Next, you will receive a specific request for summarization:
88
-
89
- <summary_request>
90
- {{resuma esse memorial}}
91
- </summary_request>
92
 
93
  Before providing your summary, follow these steps:
94
 
@@ -102,18 +125,9 @@ Before providing your summary, follow these steps:
102
 
103
  3. Maximal Marginal Relevance: Apply the principles of Maximal Marginal Relevance to ensure your summary includes diverse, relevant information while avoiding redundancy. Prioritize information that is both relevant to the summary request and adds new insights not already covered.
104
 
105
- After completing these steps, provide your summary in the following format:
106
-
107
- <summary>
108
- {
109
- "nome_do_memorial": "",
110
- "argumentos": "",
111
- "jurisprudencia": "",
112
- "doutrina": "",
113
- "palavras_chave": [
114
- ]
115
- }
116
- </summary>
117
 
118
  Remember:
119
  - Always prioritize relevance to the summary request.
 
8
  from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEmbeddings
9
  from setup.environment import default_model
10
  from uuid import uuid4
11
+ from langchain_core.output_parsers import JsonOutputParser
12
+ from langchain_core.pydantic_v1 import BaseModel, Field
13
+ from typing import List
14
 
15
  os.environ["LANGCHAIN_TRACING_V2"]="true"
16
  os.environ["LANGCHAIN_ENDPOINT"]="https://api.smith.langchain.com"
 
81
  )
82
 
83
 
84
+ class Resumo(BaseModel):
85
+ nome_do_memorial: str = Field()
86
+ argumentos: str = Field()
87
+ jurisprudencia: str = Field()
88
+ doutrina: str = Field()
89
+ palavras_chave: List[str] = Field()
90
+
91
+ def create_prompt_llm_chain_summary(system_prompt, modelParam):
92
+ model = create_llm(modelParam)
93
+
94
+
95
+
96
+ system_prompt = system_prompt + "\n\n" + "{context}"
97
+ prompt = ChatPromptTemplate.from_messages(
98
+ [
99
+ ("system", system_prompt),
100
+ ("human", "{input}"),
101
+ ]
102
+ )
103
+ question_answer_chain = create_stuff_documents_chain(model, prompt)
104
+ final_chain = question_answer_chain | JsonOutputParser(pydantic_object=Resumo)
105
+ return final_chain
106
+
107
+
108
  DEFAULT_SYSTEM_PROMPT = """
109
 
110
  You are a highly knowledgeable legal assistant specializing in case summarization. Your task is to provide comprehensive and accurate summaries of legal cases while maintaining a professional and objective demeanor. Always approach each case with careful consideration and analytical rigor.
111
 
112
  First, you will be given a document to analyze:
113
 
114
+ Next, you will summarize a content provided.
 
 
 
 
115
 
116
  Before providing your summary, follow these steps:
117
 
 
125
 
126
  3. Maximal Marginal Relevance: Apply the principles of Maximal Marginal Relevance to ensure your summary includes diverse, relevant information while avoiding redundancy. Prioritize information that is both relevant to the summary request and adds new insights not already covered.
127
 
128
+ After completing these steps, provide your summary of around 5000 characteres in a JSON format with the keys and types: nome_do_memorial (string), argumentos(string), jurisprudencia(string), doutrina(string), palavras_chave(array of strings).
129
+
130
+ Please, make the format of the summary in BBcode and use as much as possible lists in BBcode format
 
 
 
 
 
 
 
 
 
131
 
132
  Remember:
133
  - Always prioritize relevance to the summary request.