-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add sentiment classification task #1346 #1480
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c5720b1
sentiment analysis task integration with search
kartik-raj7 6fc6750
Merge branch 'topoteretes:main' into main
kartik-raj7 bf4ba18
chore: requested cleanups
kartik-raj7 3e9cbda
Merge branch 'main' of https://github.com/kartik-raj7/cognee
kartik-raj7 28fa9bf
Chore: changes suggested by coderrabit
kartik-raj7 0a53eec
Merge branch 'main' into main
kartik-raj7 32034d5
Merge branch 'topoteretes:main' into main
kartik-raj7 afd1ddf
chore: changes in sentiment analysis task
kartik-raj7 f240035
Merge branch 'dev' into main
kartik-raj7 bba89ba
chore: changes requested in review
kartik-raj7 79dec5f
Merge branch 'dev' into main
kartik-raj7 3f74811
Merge branch 'dev' into main
kartik-raj7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .sentiment_analysis import run_sentiment_analysis |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # tasks/run_sentiment_analysis.py | ||
|
|
||
| from cognee.infrastructure.llm import LLMGateway | ||
| from cognee.tasks.storage import add_data_points | ||
| from cognee.modules.engine.models import NodeSet | ||
| from uuid import uuid5, NAMESPACE_OID | ||
| from typing import Optional | ||
| from cognee.modules.retrieval.utils.models import CogneeSearchSentiment | ||
| from cognee.modules.users.models import User | ||
|
|
||
| async def run_sentiment_analysis(prev_question: str, prev_answer: str, current_question: str, user: User): | ||
| text_input = f""" | ||
| Previous Q: {prev_question} | ||
| Answer: {prev_answer} | ||
| Current Q: {current_question} | ||
| """ | ||
| user_id = str(user.id) | ||
kartik-raj7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Call LLM to classify sentiment | ||
| sentiment_result = await LLMGateway.acreate_structured_output( | ||
| text_input=text_input, | ||
| system_prompt="""Classify the user's reaction as Positive, Neutral, or Negative with a score (-5 to 5).Return the result as valid JSON like:{"sentiment": "Positive","score": 3}""", | ||
| response_model= CogneeSearchSentiment | ||
| ) | ||
| sentiment_data_point = CogneeSearchSentiment( | ||
| id=uuid5(NAMESPACE_OID, name=user_id + current_question), | ||
| prev_question=prev_question, | ||
| prev_answer=prev_answer, | ||
| current_question=current_question, | ||
| sentiment=sentiment_result.sentiment, | ||
| score=sentiment_result.score, | ||
| user_id=user_id, | ||
| belongs_to_set=NodeSet(id=uuid5(NAMESPACE_OID, "CogneeSearchSentiment"), name="CogneeSearchSentiment") | ||
| ) | ||
| await add_data_points(data_points=[sentiment_data_point], update_edge_collection=True) | ||
|
||
| return { | ||
| "prev_question" : prev_question, | ||
| "prev_answer" : prev_answer, | ||
| "current_question" :current_question, | ||
| "sentiment": sentiment_result.sentiment, | ||
| "score": sentiment_result.score | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import cognee | ||
| import asyncio | ||
|
|
||
| async def main(): | ||
| await cognee.prune.prune_data() | ||
| await cognee.prune.prune_system(metadata=True) | ||
|
|
||
| text = "Cognee turns documents into AI memory." | ||
|
|
||
| await cognee.add(text) | ||
| await cognee.cognify() | ||
|
|
||
| queries = [ | ||
| "What does Cognee do?", | ||
| "How does Cognee store data?" | ||
| ] | ||
|
|
||
| all_results = {} | ||
|
|
||
| for q in queries: | ||
| results = await cognee.search( | ||
| query_text=q, | ||
| save_interaction=True, | ||
| ) | ||
| all_results[q] = results | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Print results | ||
| for query, res in all_results.items(): | ||
| print(f"\nQuery: {query}") | ||
| for r in res: | ||
| print(f" - {r}") | ||
|
|
||
| if __name__ == '__main__': | ||
| asyncio.run(main()) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Running sentiment analysis on search request will slow down the search. We are currently trying to speed it up, so would be good to have this running somehow independently.
When/where do you need that sentiment analysis? What is the use case?