Skip to content

Commit

Permalink
updating calls to
Browse files Browse the repository at this point in the history
  • Loading branch information
wotey committed Aug 6, 2024
1 parent ab09ffa commit 6f8d042
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions app/backend/approaches/chatreadretrieveread.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,20 @@ async def run(self, history: Sequence[dict[str, str]], overrides: dict[str, Any]
# max_tokens=32, # setting it too low may cause malformed JSON
max_tokens=100,
n=1)
if not chat_completion or not chat_completion.choices[0].message.content:
raise ValueError("No choices returned in chat completion response")

# Initialize a list to collect filter reasons
filter_reasons = []

# Check for content filtering
if chat_completion.choices[0].finish_reason == 'content_filter':
for category, details in chat_completion.choices[0].content_filter_results.items():
if details['filtered']:
filter_reasons.append(f"{category} ({details['severity']})")

# Raise an error if any filters are triggered
if filter_reasons:
error_message = "The generated content was filtered due to triggering Azure OpenAI's content filtering system. Reason(s): The response contains content flagged as " + ", ".join(filter_reasons)
raise ValueError(error_message)

except Exception as e:
log.error(f"Error generating optimized keyword search: {str(e)}")
yield json.dumps({"error": f"Error generating optimized keyword search: {str(e)}"}) + "\n"
Expand Down Expand Up @@ -431,11 +442,22 @@ async def run(self, history: Sequence[dict[str, str]], overrides: dict[str, Any]
"thought_chain": thought_chain,
"work_citation_lookup": citation_lookup,
"web_citation_lookup": {}}) + "\n"

# STEP 4: Format the response
async for chunk in chat_completion:
# Check if there is at least one element and the first element has the key 'delta'
if len(chunk.choices) > 0:
filter_reasons = []
# Check for content filtering
if chunk.choices[0].finish_reason == 'content_filter':
for category, details in chunk.choices[0].content_filter_results.items():
if details['filtered']:
filter_reasons.append(f"{category} ({details['severity']})")

# Raise an error if any filters are triggered
if filter_reasons:
error_message = "The generated content was filtered due to triggering Azure OpenAI's content filtering system. Reason(s): The response contains content flagged as " + ", ".join(filter_reasons)
raise ValueError(error_message)
yield json.dumps({"content": chunk.choices[0].delta.content}) + "\n"
except Exception as e:
log.error(f"Error generating chat completion: {str(e)}")
Expand Down

0 comments on commit 6f8d042

Please sign in to comment.