Skip to content
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

Add sample for sentiment analysis #13

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ Each subdirectory contains relevant code and instructions for the corresponding
#### Data Transformation (T)
- [Remove Duplicates](remove-duplicates): Sample code for deduplication.
- [Remove Approximate Duplicates](remove-approximate-duplicates): Sample code for approximate deduplication.
- [Error Correction](error-correction): Sample code for error correction in data.
- [Clean and Standardize Data](clean-and-standardize-data): Sample code for cleaning and standardizing data.
- [Data Mapper](data-mapper): Sample code for data mapping between different structures.
- [Enrich Data](enrich-data): Sample code to enrich data.
- [Sentiment Analysis](sentiment-analysis): Sample code for analyse feedbacks.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we remove the earlier one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed from the README but didn't remove the sample.


#### Data Loading (L)
- [Load Data to BigQuery](load-data-to-bigquery): Sample code for loading data into BigQuery.
Expand Down
3 changes: 3 additions & 0 deletions sentiment-analysis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
generated
Config.toml
5 changes: 5 additions & 0 deletions sentiment-analysis/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "etl_samples"
name = "sentiment_analysis"
version = "0.1.0"
distribution = "2201.8.2"
305 changes: 305 additions & 0 deletions sentiment-analysis/Dependencies.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
# AUTO-GENERATED FILE. DO NOT MODIFY.

# This file is auto-generated by Ballerina for managing dependency versions.
# It should not be modified by hand.

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.8.2"

[[package]]
org = "ballerina"
name = "auth"
version = "2.10.0"
dependencies = [
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.array"},
{org = "ballerina", name = "lang.string"},
{org = "ballerina", name = "log"}
]

[[package]]
org = "ballerina"
name = "cache"
version = "3.7.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "task"},
{org = "ballerina", name = "time"}
]

[[package]]
org = "ballerina"
name = "constraint"
version = "1.5.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "crypto"
version = "2.5.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "time"}
]

[[package]]
org = "ballerina"
name = "file"
version = "1.9.0"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "os"},
{org = "ballerina", name = "time"}
]

[[package]]
org = "ballerina"
name = "http"
version = "2.10.4"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "cache"},
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "file"},
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "jwt"},
{org = "ballerina", name = "lang.array"},
{org = "ballerina", name = "lang.decimal"},
{org = "ballerina", name = "lang.int"},
{org = "ballerina", name = "lang.regexp"},
{org = "ballerina", name = "lang.runtime"},
{org = "ballerina", name = "lang.string"},
{org = "ballerina", name = "lang.value"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "mime"},
{org = "ballerina", name = "oauth2"},
{org = "ballerina", name = "observe"},
{org = "ballerina", name = "time"},
{org = "ballerina", name = "url"}
]
modules = [
{org = "ballerina", packageName = "http", moduleName = "http"},
{org = "ballerina", packageName = "http", moduleName = "http.httpscerr"}
]

[[package]]
org = "ballerina"
name = "io"
version = "1.6.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.value"}
]

[[package]]
org = "ballerina"
name = "jballerina.java"
version = "0.0.0"

[[package]]
org = "ballerina"
name = "jwt"
version = "2.10.0"
dependencies = [
{org = "ballerina", name = "cache"},
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.int"},
{org = "ballerina", name = "lang.string"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "time"}
]

[[package]]
org = "ballerina"
name = "lang.__internal"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.object"}
]

[[package]]
org = "ballerina"
name = "lang.array"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.__internal"}
]

[[package]]
org = "ballerina"
name = "lang.decimal"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.int"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.__internal"},
{org = "ballerina", name = "lang.object"}
]

[[package]]
org = "ballerina"
name = "lang.object"
version = "0.0.0"

[[package]]
org = "ballerina"
name = "lang.regexp"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.runtime"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.string"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.regexp"}
]

[[package]]
org = "ballerina"
name = "lang.value"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "log"
version = "2.9.0"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.value"},
{org = "ballerina", name = "observe"}
]

[[package]]
org = "ballerina"
name = "mime"
version = "2.9.0"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.int"}
]

[[package]]
org = "ballerina"
name = "oauth2"
version = "2.10.0"
dependencies = [
{org = "ballerina", name = "cache"},
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "time"},
{org = "ballerina", name = "url"}
]

[[package]]
org = "ballerina"
name = "observe"
version = "1.2.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "os"
version = "1.8.0"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "task"
version = "2.5.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "time"}
]

[[package]]
org = "ballerina"
name = "time"
version = "2.4.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "url"
version = "2.4.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerinai"
name = "observe"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "observe"}
]

[[package]]
org = "ballerinax"
name = "openai.chat"
version = "1.1.2"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "http"},
{org = "ballerina", name = "url"},
{org = "ballerinai", name = "observe"}
]
modules = [
{org = "ballerinax", packageName = "openai.chat", moduleName = "openai.chat"}
]

[[package]]
org = "etl_samples"
name = "sentiment_analysis"
version = "0.1.0"
dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerinax", name = "openai.chat"}
]
modules = [
{org = "etl_samples", packageName = "sentiment_analysis", moduleName = "sentiment_analysis"}
]

20 changes: 20 additions & 0 deletions sentiment-analysis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Steps to run the sample

### 1. Configuring the Config.toml file
1. Create a `Config.toml` file in the root directory and add the below content.
```toml
openAIKey = "<OPEN_AI_KEY>"
```
2. Create an [OpenAI account](https://platform.openai.com) and obtain
an [API key](https://platform.openai.com/account/api-keys) and replace with `<OPEN_AI_KEY>`.

### 2. Running the sample
Run the following command from the root directory of the sample.
```sh
$ bal run
```

### 3. Test the sample
```sh
$ curl -X POST -d 'Amazing food, prompt service!' http://localhost:8080/api/reviews/sentiement-analyse
```
41 changes: 41 additions & 0 deletions sentiment-analysis/main.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import ballerina/http;
import ballerinax/openai.chat;

configurable string openAIKey = ?;

type Feedback record {|
"POSITIVE"|"NEGATIVE"|"NEUTRAL" feedback;
|};

final chat:Client openAiChat = check new ({auth: {token: openAIKey}});

isolated service /api on new http:Listener(8080) {

resource function post reviews/sentiement\-analyse(@http:Payload string cutomerReview)
returns "POSITIVE"|"NEGATIVE"|"NEUTRAL"|error? {
chat:CreateChatCompletionRequest request = {
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: string `
Analyze the given cutomer review and mention the feedback is positive or negative or netural.
Response should be in JSON in below format.
{
feedback: "POSITIVE"|"NEGATIVE"|"NEUTRAL"
}
cutomer review: ${cutomerReview}
`
}
]
};
chat:CreateChatCompletionResponse summary = check openAiChat->/chat/completions.post(request);
if summary.choices.length() > 0 {
string content = check summary.choices[0].message?.content.ensureType();
json jsonFeedback = check content.fromJsonString();
Feedback feedback = check jsonFeedback.cloneWithType();
return feedback.feedback;
}
return "NEUTRAL";
}
}