Skip to content

Commit d1003bd

Browse files
changes v2
1 parent 5c7ab3b commit d1003bd

2 files changed

Lines changed: 16 additions & 21 deletions

File tree

src/ContentProcessor/src/libs/pipeline/handlers/evaluate_handler.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
import json
55

6-
from openai.types.chat.parsed_chat_completion import ParsedChatCompletion
7-
86
from libs.application.application_context import AppContext
97
from libs.azure_helper.model.content_understanding import AnalyzedResult
108
from libs.pipeline.entities.pipeline_file import ArtifactType, PipelineLogEntry
@@ -44,19 +42,17 @@ async def execute(self, context: MessageContext) -> StepResult:
4442
**json.loads(output_file_json_string_from_extract)
4543
)
4644

47-
# Get the result from Map step handler - OpenAI
45+
# Get the result from Map step handler - Azure AI Foundry
4846
output_file_json_string_from_map = self.download_output_file_to_json_string(
4947
processed_by="map",
5048
artifact_type=ArtifactType.SchemaMappedData,
5149
)
5250

53-
# Deserialize the result to ParsedChatCompletion (Azure OpenAI)
54-
gpt_result = ParsedChatCompletion(
55-
**json.loads(output_file_json_string_from_map)
56-
)
51+
# Deserialize the result from Azure AI Foundry SDK response
52+
gpt_result = json.loads(output_file_json_string_from_map)
5753

58-
# Mapped Result by GPT
59-
parsed_message_from_gpt = gpt_result.choices[0].message.parsed
54+
# Mapped Result from Azure AI Foundry
55+
parsed_message_from_gpt = gpt_result["choices"][0]["message"]["parsed"]
6056

6157
# Convert the parsed message to a dictionary
6258
gpt_evaluate_confidence_dict = parsed_message_from_gpt
@@ -69,7 +65,7 @@ async def execute(self, context: MessageContext) -> StepResult:
6965

7066
# Evaluate Confidence Score - GPT
7167
gpt_confidence_score = gpt_confidence(
72-
gpt_evaluate_confidence_dict, gpt_result.choices[0]
68+
gpt_evaluate_confidence_dict, gpt_result["choices"][0]
7369
)
7470

7571
# Merge the confidence scores - Content Understanding and GPT results.
@@ -89,8 +85,8 @@ async def execute(self, context: MessageContext) -> StepResult:
8985
extracted_result=gpt_evaluate_confidence_dict,
9086
confidence=merged_confidence_score,
9187
comparison_result=result_data,
92-
prompt_tokens=gpt_result.usage.prompt_tokens,
93-
completion_tokens=gpt_result.usage.completion_tokens,
88+
prompt_tokens=gpt_result["usage"]["prompt_tokens"],
89+
completion_tokens=gpt_result["usage"]["completion_tokens"],
9490
execution_time=0,
9591
)
9692

src/ContentProcessor/src/libs/pipeline/handlers/logics/evaluate_handler/openai_confidence_evaluator.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
import math
55

66
import tiktoken
7-
from openai.types.chat.chat_completion import Choice
87

98
from libs.pipeline.handlers.logics.evaluate_handler.confidence import (
109
get_confidence_values,
1110
)
1211

1312

14-
def evaluate_confidence(extract_result: dict, choice: Choice, model: str = "gpt-4o"):
13+
def evaluate_confidence(extract_result: dict, choice: dict, model: str = "gpt-4o"):
1514
"""
16-
Evaluate confidence for each field value in the extracted result based on the logprobs of the response from Azure OpenAI.
15+
Evaluate confidence for each field value in the extracted result based on the logprobs of the response from Azure AI Foundry.
1716
1817
Args:
1918
extract_result: The extraction result.
20-
choice: The choice object from the OpenAI response.
19+
choice: The choice dictionary from the Azure AI Foundry response.
2120
model: The model used for the response.
2221
2322
Returns:
@@ -30,16 +29,16 @@ def evaluate_confidence(extract_result: dict, choice: Choice, model: str = "gpt-
3029
encoding = tiktoken.encoding_for_model(model)
3130

3231
# To perform the confidence evaluation, we need the original text from the response, not just the object result.
33-
generated_text = choice.message.content
32+
generated_text = choice["message"]["content"]
3433

35-
if choice.logprobs is None:
34+
if choice.get("logprobs") is None:
3635
confidence["_overall"] = 0.0
3736
return confidence
3837

39-
logprobs = choice.logprobs.content
38+
logprobs = choice["logprobs"]["content"]
4039

41-
tokens = [token_logprob.token for token_logprob in logprobs]
42-
token_logprobs = [token_logprob.logprob for token_logprob in logprobs]
40+
tokens = [token_logprob["token"] for token_logprob in logprobs]
41+
token_logprobs = [token_logprob["logprob"] for token_logprob in logprobs]
4342

4443
# Encode the entire generated text to map tokens to character positions
4544
token_offsets = []

0 commit comments

Comments
 (0)