-
-
Notifications
You must be signed in to change notification settings - Fork 76
Bias checking endpoint. #112
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,57 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||
| from groq import Groq | ||||||||||||||||||||||||||||||||||||||||||||
| from dotenv import load_dotenv | ||||||||||||||||||||||||||||||||||||||||||||
| import json | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| load_dotenv() | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| client = Groq(api_key=os.getenv("GROQ_API_KEY")) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def check_bias(text): | ||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||
| print(text) | ||||||||||||||||||||||||||||||||||||||||||||
| print(json.dumps(text)) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if not text: | ||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError("Missing or empty 'cleaned_text'") | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| chat_completion = client.chat.completions.create( | ||||||||||||||||||||||||||||||||||||||||||||
| messages=[ | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| "role": "system", | ||||||||||||||||||||||||||||||||||||||||||||
| "content": ( | ||||||||||||||||||||||||||||||||||||||||||||
| "You are an assistant that checks " | ||||||||||||||||||||||||||||||||||||||||||||
| "if given article is biased and give" | ||||||||||||||||||||||||||||||||||||||||||||
| "score to each based on biasness where 0 is lowest bias and 100 is highest bias" | ||||||||||||||||||||||||||||||||||||||||||||
| "Only return a number between 0 to 100 base on bias." | ||||||||||||||||||||||||||||||||||||||||||||
| "only return Number No Text" | ||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+22
to
+29
Contributor
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. 🛠️ Refactor suggestion Improve system prompt formatting and clarity. The system prompt has formatting and grammatical issues that could affect model performance. "content": (
- "You are an assistant that checks "
- "if given article is biased and give"
- "score to each based on biasness where 0 is lowest bias and 100 is highest bias"
- "Only return a number between 0 to 100 base on bias."
- "only return Number No Text"
+ "You are an assistant that analyzes articles for bias. "
+ "Rate the bias level on a scale from 0 to 100, where: "
+ "0 = completely unbiased/neutral, 100 = extremely biased. "
+ "Return ONLY a single number between 0 and 100. "
+ "Do not include any explanatory text."
),📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| "role": "user", | ||||||||||||||||||||||||||||||||||||||||||||
| "content": ( | ||||||||||||||||||||||||||||||||||||||||||||
| "Give bias score to the following article " | ||||||||||||||||||||||||||||||||||||||||||||
| f"\n\n{text}" | ||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||
| model="gemma2-9b-it", | ||||||||||||||||||||||||||||||||||||||||||||
| temperature=0.3, | ||||||||||||||||||||||||||||||||||||||||||||
| max_tokens=512, | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| bias_score = chat_completion.choices[0].message.content.strip() | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||
| "bias_score": bias_score, | ||||||||||||||||||||||||||||||||||||||||||||
| "status": "success", | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+44
to
+49
Contributor
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. 🛠️ Refactor suggestion Validate numeric response from AI model. The response should be validated to ensure it's actually a numeric value as requested. bias_score = chat_completion.choices[0].message.content.strip()
+
+ # Validate that the response is numeric
+ try:
+ bias_value = float(bias_score)
+ if not (0 <= bias_value <= 100):
+ raise ValueError(f"Bias score {bias_value} outside valid range 0-100")
+ bias_score = str(int(bias_value)) # Convert to integer string
+ except ValueError as ve:
+ raise ValueError(f"Invalid numeric response from AI model: {bias_score}")
return {
"bias_score": bias_score,
"status": "success",
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||
| print(f"Error in bias_detection: {e}") | ||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||
| "status": "error", | ||||||||||||||||||||||||||||||||||||||||||||
| "error_from": "bias_detection", | ||||||||||||||||||||||||||||||||||||||||||||
| "message": str(e), | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
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.
🛠️ Refactor suggestion
Remove debug print statements.
Debug print statements should be removed from production code or replaced with proper logging.
If logging is needed, use a proper logger:
📝 Committable suggestion
🤖 Prompt for AI Agents