llm processing of sentiment
This commit is contained in:
@@ -12,7 +12,7 @@ def dummy_sentiment_analysis(content, tag):
|
||||
|
||||
|
||||
|
||||
def ollama_sentiment_analysis(content, theme, client: Client, model) -> tuple[list[str], int, str]:
|
||||
def ollama_sentiment_analysis(content, theme, theme_description, client: Client, model) -> tuple[list[str], int, str]:
|
||||
"""
|
||||
Perform sentiment analysis using Ollama model.
|
||||
|
||||
@@ -24,79 +24,86 @@ def ollama_sentiment_analysis(content, theme, client: Client, model) -> tuple[li
|
||||
- sentiment score and reason
|
||||
"""
|
||||
prompt = f"""
|
||||
# Instructions
|
||||
You are an expert in sentiment analysis and natural language processing. You are given a quote from an interview along with a theme tag. Your task is to analyze the sentiment expressed in the quote in relation to the provided theme, and provide a short explanation for your assessment (max 10 words).
|
||||
|
||||
You need to deliver three pieces of information:
|
||||
1. A list of keywords from the quote quantify or qualify the theme, and that influenced your sentiment analysis (if any).
|
||||
2. A sentiment score: -1 for negative, 0 for neutral, and 1 for positive sentiment.
|
||||
3. A brief reason (max 10 words) explaining your sentiment score.
|
||||
|
||||
# Role
|
||||
You are an expert in sentiment analysis. Your task is to analyze the sentiment of a quote in relation to a specific theme.
|
||||
|
||||
# Guidelines
|
||||
Keywords should be directly relevant to the theme.
|
||||
|
||||
The reason should be extremely concise and to the point:
|
||||
- Does not need to be a full sentence.
|
||||
- Sentiment itself does not need to be stated in the explanation.
|
||||
- If keywords are present in the quote that directly capture the sentiment, give that as the reason..
|
||||
|
||||
|
||||
# Input
|
||||
|
||||
Theme: `{theme}`
|
||||
|
||||
Theme Description: `{theme_description}`
|
||||
Quote:
|
||||
```
|
||||
{content}
|
||||
```
|
||||
|
||||
|
||||
# Instructions
|
||||
1. Analyze the sentiment of the quote specifically regarding the theme.
|
||||
2. Extract relevant keywords or phrases from the quote. Prioritize specific descriptors found in the text that match or relate to the theme.
|
||||
3. Assign a sentiment score:
|
||||
- -1: Negative (complaint, dissatisfaction, criticism)
|
||||
- 0: Neutral (factual, mixed, or no strong opinion)
|
||||
- 1: Positive (praise, satisfaction, agreement)
|
||||
4. Provide a concise reason (max 10 words).
|
||||
|
||||
# Constraints
|
||||
- Return ONLY a valid JSON object.
|
||||
- Do not use Markdown formatting (no ```json blocks).
|
||||
- Do not write any Python code or explanations outside the JSON.
|
||||
- If the quote is irrelevant to the theme, return sentiment 0.
|
||||
|
||||
# Response Format
|
||||
Provide your response in the following JSON format:
|
||||
{{
|
||||
"keywords": ["<list_of_relevant_keywords_if_any>"],
|
||||
"sentiment": <sentiment_score>,
|
||||
"reason": "<brief_explanation_max_10_words>"
|
||||
"keywords": ["<list_of_keywords>"],
|
||||
"sentiment": <integer_score>,
|
||||
"reason": "<string_reason>"
|
||||
}}
|
||||
|
||||
|
||||
# Examples
|
||||
|
||||
** Example 1**
|
||||
- Theme: `Speed`
|
||||
- Quote: `It just was a little toned down. It was almost like he was talking like this. You know? It almost kind of this was a little slow for me.`
|
||||
|
||||
- Response: {{"keywords": ["slow"], "sentiment": -1, "reason": "States speed is slow, indicates dissatisfaction"}}
|
||||
|
||||
** Example 2**
|
||||
- Theme: `Friendliness / Empathy`
|
||||
- Quote: `Sound very welcoming`
|
||||
|
||||
- Response: {{ "keywords": ["welcoming"], "sentiment": 1, "reason": "Uses 'welcoming'" }}
|
||||
|
||||
Example 1:
|
||||
Theme: `Speed`
|
||||
Quote: `It was a little slow for me.`
|
||||
Response: {{"keywords": ["slow"], "sentiment": -1, "reason": "Dissatisfaction with speed"}}
|
||||
|
||||
Example 2:
|
||||
Theme: `Price`
|
||||
Quote: `It costs $50.`
|
||||
Response: {{"keywords": [], "sentiment": 0, "reason": "Factual statement"}}
|
||||
|
||||
Example 3:
|
||||
Theme: `Friendliness`
|
||||
Quote: `Sound very welcoming.`
|
||||
Response: {{"keywords": ["welcoming"], "sentiment": 1, "reason": "Positive descriptor used"}}
|
||||
"""
|
||||
|
||||
resp = client.generate(
|
||||
model=model,
|
||||
prompt=prompt,
|
||||
)
|
||||
|
||||
try:
|
||||
response_text = resp.response.strip()
|
||||
max_retries = 3
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
resp = client.generate(
|
||||
model=model,
|
||||
prompt=prompt,
|
||||
)
|
||||
|
||||
response_text = resp.response.strip()
|
||||
|
||||
# Extract JSON from response
|
||||
start_index = response_text.find('{')
|
||||
end_index = response_text.rfind('}') + 1
|
||||
json_str = response_text[start_index:end_index]
|
||||
# Extract JSON from response
|
||||
start_index = response_text.find('{')
|
||||
end_index = response_text.rfind('}') + 1
|
||||
|
||||
if start_index == -1 or end_index == 0:
|
||||
raise ValueError("No JSON found")
|
||||
|
||||
json_str = response_text[start_index:end_index]
|
||||
|
||||
response_json = json.loads(json_str)
|
||||
keywords = response_json.get('keywords', [])
|
||||
sentiment = response_json.get('sentiment', 'test')
|
||||
reason = response_json.get('reason', 'no reason provided')
|
||||
return keywords, sentiment, reason
|
||||
|
||||
response_json = json.loads(json_str)
|
||||
keywords = response_json.get('keywords', [])
|
||||
sentiment = response_json.get('sentiment', 'test')
|
||||
reason = response_json.get('reason', 'no reason provided')
|
||||
return keywords, sentiment, reason
|
||||
except Exception as e:
|
||||
print(f"Error parsing response: {e}")
|
||||
return [], None, 'parsing error'
|
||||
except Exception as e:
|
||||
print(f"Attempt {attempt + 1}/{max_retries} failed: {e}")
|
||||
if attempt == max_retries - 1:
|
||||
return [], None, 'parsing error'
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user