Update utils.py
Browse files
utils.py
CHANGED
|
@@ -291,19 +291,19 @@ def grade_documents_direct(prompt, documents):
|
|
| 291 |
|
| 292 |
# LLM
|
| 293 |
model = ChatOpenAI(temperature=0.3, model="gpt-4-0125-preview", streaming=True)
|
| 294 |
-
|
| 295 |
# Tool
|
| 296 |
grade_tool_oai = convert_to_openai_tool(grade)
|
| 297 |
-
|
| 298 |
# LLM with tool and enforce invocation
|
| 299 |
llm_with_tool = model.bind(
|
| 300 |
tools=[convert_to_openai_tool(grade_tool_oai)],
|
| 301 |
tool_choice={"type": "function", "function": {"name": "grade"}},
|
| 302 |
)
|
| 303 |
-
|
| 304 |
# Parser
|
| 305 |
parser_tool = PydanticToolsParser(tools=[grade])
|
| 306 |
-
|
| 307 |
# Prompt
|
| 308 |
prompt = PromptTemplate(
|
| 309 |
template="""Du bist ein Bewerter, der die Relevanz von einem erhaltenen Dokument zu einer Nutzeranfrage bewerten soll. \n
|
|
@@ -313,16 +313,18 @@ def grade_documents_direct(prompt, documents):
|
|
| 313 |
Gib eine binäre Bewertung von 'ja' oder 'nein' Bewertung, um anzuzeigen ob das Dokuemnt relevant ist zur Nutzeranfrage oder nicht.""",
|
| 314 |
input_variables=["context", "question"],
|
| 315 |
)
|
| 316 |
-
|
| 317 |
# Chain
|
| 318 |
chain = prompt | llm_with_tool | parser_tool
|
| 319 |
-
|
| 320 |
# Score
|
| 321 |
filtered_docs = []
|
| 322 |
|
| 323 |
for d in documents:
|
| 324 |
score = chain.invoke({"question": prompt, "context": d.page_content})
|
|
|
|
| 325 |
grade = score[0].binary_score
|
|
|
|
| 326 |
if grade == "ja":
|
| 327 |
print("---Bewertung: Dokument ist relevant---")
|
| 328 |
filtered_docs.append(d)
|
|
|
|
| 291 |
|
| 292 |
# LLM
|
| 293 |
model = ChatOpenAI(temperature=0.3, model="gpt-4-0125-preview", streaming=True)
|
| 294 |
+
|
| 295 |
# Tool
|
| 296 |
grade_tool_oai = convert_to_openai_tool(grade)
|
| 297 |
+
|
| 298 |
# LLM with tool and enforce invocation
|
| 299 |
llm_with_tool = model.bind(
|
| 300 |
tools=[convert_to_openai_tool(grade_tool_oai)],
|
| 301 |
tool_choice={"type": "function", "function": {"name": "grade"}},
|
| 302 |
)
|
| 303 |
+
|
| 304 |
# Parser
|
| 305 |
parser_tool = PydanticToolsParser(tools=[grade])
|
| 306 |
+
|
| 307 |
# Prompt
|
| 308 |
prompt = PromptTemplate(
|
| 309 |
template="""Du bist ein Bewerter, der die Relevanz von einem erhaltenen Dokument zu einer Nutzeranfrage bewerten soll. \n
|
|
|
|
| 313 |
Gib eine binäre Bewertung von 'ja' oder 'nein' Bewertung, um anzuzeigen ob das Dokuemnt relevant ist zur Nutzeranfrage oder nicht.""",
|
| 314 |
input_variables=["context", "question"],
|
| 315 |
)
|
| 316 |
+
|
| 317 |
# Chain
|
| 318 |
chain = prompt | llm_with_tool | parser_tool
|
| 319 |
+
|
| 320 |
# Score
|
| 321 |
filtered_docs = []
|
| 322 |
|
| 323 |
for d in documents:
|
| 324 |
score = chain.invoke({"question": prompt, "context": d.page_content})
|
| 325 |
+
print("hier1..................................")
|
| 326 |
grade = score[0].binary_score
|
| 327 |
+
print("hier2..................................")
|
| 328 |
if grade == "ja":
|
| 329 |
print("---Bewertung: Dokument ist relevant---")
|
| 330 |
filtered_docs.append(d)
|