-
Notifications
You must be signed in to change notification settings - Fork 67
removed langchain, replaced with semantic kernel #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d3f4ce7
a4cedcf
b19a04f
3904e2a
ac8dd1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| import fitz # PyMuPDF | ||
| from PIL import Image | ||
| from pathlib import Path | ||
| import io | ||
| import os | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,28 @@ | ||
| import base64 | ||
|
|
||
| from langchain.chains.transform import TransformChain | ||
| from langchain_openai import AzureChatOpenAI | ||
| from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion | ||
| from semantic_kernel.connectors.ai import PromptExecutionSettings | ||
|
|
||
| from ai_ocr.azure.config import get_config | ||
|
|
||
|
|
||
| def get_llm(): | ||
| def get_completion_service(): | ||
| api_key = get_config()['openai_api_key'] | ||
| if not api_key: | ||
| raise ValueError("openai_api_key environment variable is not set.") | ||
|
|
||
| return AzureChatOpenAI( | ||
| model=get_config()["openai_model_deployment"], | ||
| temperature=0, | ||
| max_tokens=4000, | ||
| verbose=True, | ||
| api_key=api_key, | ||
| api_version=get_config()["openai_api_version"] | ||
| ) | ||
|
|
||
| chat_completion_service = AzureChatCompletion( | ||
| deployment_name=get_config()["openai_model_deployment"], | ||
| api_key=api_key, | ||
| endpoint=get_config()["openai_api_endpoint"], | ||
| api_version=get_config()["openai_api_version"]) | ||
|
|
||
| req_settings = PromptExecutionSettings( | ||
| extension_data = { | ||
| "max_tokens": 4000, | ||
| "temperature": 0, | ||
| } | ||
| ) | ||
| return chat_completion_service, req_settings | ||
|
|
||
|
|
||
| def load_image(image_path) -> str: | ||
|
|
@@ -33,10 +36,3 @@ def get_size_of_base64_images(images): | |
| for img in images: | ||
| total_size += len(img) | ||
| return total_size | ||
|
|
||
|
|
||
| load_image_chain = TransformChain( | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could not find any place where this chain was referenced or used, so I removed it. If this was necessary for a larger piece of the solution, let me know so that I can be sure to include a Semantic Kernel replacement. |
||
| input_variables=["image_path"], | ||
| output_variables=["image"], | ||
| transform=load_image | ||
| ) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will need to incorporate the newest changes that are utilizing html instead of markdown |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,11 @@ | ||
| from langchain_core.messages import HumanMessage | ||
| from langchain_core.prompts import ChatPromptTemplate | ||
| from langchain_core.messages import SystemMessage | ||
| from langchain_core.prompts import HumanMessagePromptTemplate | ||
| from langchain_core.output_parsers import JsonOutputParser | ||
| from semantic_kernel.contents import ChatHistory, ChatMessageContent, ImageContent | ||
| from ai_ocr.azure.openai_ops import get_completion_service | ||
|
|
||
| from ai_ocr.azure.openai_ops import get_llm | ||
| import logging | ||
| import json | ||
|
|
||
| import logging, json | ||
|
|
||
|
|
||
| def get_structured_data(html_content: str, prompt: str, json_schema: str, images=[]) -> any: | ||
| async def get_structured_data(markdown_content: str, prompt: str, json_schema: str, images=[]) -> any: | ||
| system_message = f""" | ||
| Your task is to extract the JSON contents from a document using the provided materials: | ||
| 1. Custom instructions for the extraction process | ||
|
|
@@ -35,31 +31,30 @@ def get_structured_data(html_content: str, prompt: str, json_schema: str, images | |
| ``` | ||
| """ | ||
|
|
||
| chat_template = ChatPromptTemplate.from_messages( | ||
| [ | ||
| SystemMessage(content=system_message), | ||
| HumanMessagePromptTemplate.from_template("Here is the Document content (in html format):\n{html}"), | ||
| ] | ||
| ) | ||
|
|
||
| messages = chat_template.format_messages(html=html_content) | ||
| chat_history = ChatHistory(system_message = system_message) | ||
| chat_history.add_user_message(f"Here is the Document content (in markdown format):\n{markdown_content}") | ||
|
|
||
| if images: | ||
| messages.append(HumanMessage(content="Here are the images from the document:")) | ||
| chat_history.add_user_message("Here are the images from the document:") | ||
| for img in images: | ||
| messages.append(HumanMessage(content=[{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{img}"}}])) | ||
| chat_history.add_message( | ||
| ChatMessageContent( | ||
| role="user", | ||
| items=[ImageContent(uri=f"data:image/png;base64,{img}")] | ||
| ) | ||
| ) | ||
|
|
||
| model = get_llm() | ||
| return model.invoke(messages) | ||
| service, req_params = get_completion_service() | ||
| req_params.extension_data["response_format"] = {"type": "json_object"} | ||
| return await service.get_chat_message_content( | ||
| chat_history, | ||
| req_params | ||
| ) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| def perform_gpt_evaluation_and_enrichment(images: list, extracted_data: dict, json_schema: str) -> dict: | ||
| model = get_llm() | ||
|
|
||
| parser = JsonOutputParser() | ||
|
|
||
| async def perform_gpt_evaluation_and_enrichment(images: list, extracted_data: dict, json_schema: str) -> dict: | ||
| system_message = f""" | ||
| You are an AI assistant tasked with evaluating extracted data from a document. | ||
|
|
||
|
|
@@ -105,28 +100,32 @@ def perform_gpt_evaluation_and_enrichment(images: list, extracted_data: dict, js | |
| Here is the JSON schema template that was used for the extraction: | ||
|
|
||
| {json_schema} | ||
|
|
||
| ------ | ||
|
|
||
| {parser.get_format_instructions()} | ||
| """ | ||
|
|
||
| chat_template = ChatPromptTemplate.from_messages( | ||
| [ | ||
| SystemMessage(content=system_message), | ||
| HumanMessagePromptTemplate.from_template("Here is the extracted data :\n{extracted}"), | ||
| ] | ||
| ) | ||
| messages = chat_template.format_messages(extracted=json.dumps(extracted_data, indent=2)) | ||
| chat_history = ChatHistory(system_message = system_message) | ||
| chat_history.add_user_message(f"Here is the extracted data :\n{json.dumps(extracted_data, indent=2)}") | ||
|
|
||
| if images: | ||
| messages.append(HumanMessage(content="Here are the images from the document:")) | ||
| chat_history.add_user_message("Here are the images from the document:") | ||
| for img in images: | ||
| messages.append(HumanMessage(content=[{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{img}"}}])) | ||
| chat_history.add_message( | ||
| ChatMessageContent( | ||
| role="user", | ||
| items=[ImageContent(uri=f"data:image/png;base64,{img}")] | ||
| ) | ||
| ) | ||
|
|
||
| service, req_params = get_completion_service() | ||
| # Set the response format to JSON object | ||
| req_params.extension_data["response_format"] = {"type": "json_object"} | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assumes using a model that supports structured outputs - if this is not the desired route, this will require refactoring. |
||
| evaluation_result = await service.get_chat_message_content( | ||
| chat_history, | ||
| req_params | ||
| ) | ||
|
|
||
|
|
||
| evaluation_result = model.invoke(messages) | ||
| try: | ||
| return parser.parse(evaluation_result.content) | ||
| return json.loads(evaluation_result.content) | ||
| except Exception as e: | ||
| logging.error(f"Failed to parse GPT evaluation and enrichment result: {e}") | ||
| return { | ||
|
|
@@ -135,44 +134,33 @@ def perform_gpt_evaluation_and_enrichment(images: list, extracted_data: dict, js | |
| } | ||
|
|
||
|
|
||
| def get_summary_with_gpt(mkd_output_json: str) -> any: | ||
| async def get_summary_with_gpt(mkd_output_json: str) -> any: | ||
| reasoning_prompt = """ | ||
| Use the provided data represented in the schema to produce a summary in natural language. The format should be a few sentences summary of the document. | ||
|
|
||
| As an example, for the schema {"properties": {"foo": {"title": "Foo", "description": "a list of strings", "type": "array", "items": {"type": "string"}}}, "required": ["foo"]} | ||
| the object {"foo": ["bar", "baz"]} is a well-formatted instance of the schema. The object {"properties": {"foo": ["bar", "baz"]}} is not well-formatted. | ||
| """ | ||
|
|
||
| chat_template = ChatPromptTemplate.from_messages( | ||
| [ | ||
| SystemMessage( | ||
| content=( | ||
| reasoning_prompt | ||
| ) | ||
| ), | ||
| HumanMessagePromptTemplate.from_template("{text}"), | ||
| ] | ||
| ) | ||
| messages = chat_template.format_messages(text=mkd_output_json) | ||
|
|
||
| model = get_llm() | ||
| return model.invoke(messages) | ||
| chat_history = ChatHistory(system_message = reasoning_prompt) | ||
| chat_history.add_user_message(f"{mkd_output_json}") | ||
|
|
||
| service, req_params = get_completion_service() | ||
| return await service.get_chat_message_content( | ||
| chat_history, | ||
| req_params | ||
| ) | ||
|
|
||
|
|
||
|
|
||
| def classify_doc_with_llm(ocr_input: str, classification_system_prompt) -> any: | ||
| async def classify_doc_with_llm(ocr_input: str, classification_system_prompt) -> any: | ||
| prompt = classification_system_prompt | ||
|
|
||
| chat_template = ChatPromptTemplate.from_messages( | ||
| [ | ||
| SystemMessage( | ||
| content=( | ||
| prompt | ||
| ) | ||
| ), | ||
| HumanMessagePromptTemplate.from_template("{text}"), | ||
| ] | ||
| ) | ||
| messages = chat_template.format_messages(text=ocr_input) | ||
| chat_history = ChatHistory(system_message = prompt) | ||
| chat_history.add_user_message(f"{ocr_input}") | ||
|
|
||
| model = get_llm() | ||
| return model.invoke(messages) | ||
| service, req_params = get_completion_service() | ||
| return await service.get_chat_message_content( | ||
| chat_history, | ||
| req_params | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason that we allow only these four locations for the appservice?