-
Notifications
You must be signed in to change notification settings - Fork 4
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
LakshanWeerasinghe
wants to merge
3
commits into
ballerina-guides:main
from
LakshanWeerasinghe:sentiment
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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,3 @@ | ||
target | ||
generated | ||
Config.toml |
This file contains 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,5 @@ | ||
[package] | ||
org = "etl_samples" | ||
name = "sentiment_analysis" | ||
version = "0.1.0" | ||
distribution = "2201.8.2" |
This file contains 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,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"} | ||
] | ||
|
This file contains 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,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 | ||
``` |
This file contains 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 @@ | ||
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"; | ||
} | ||
} |
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.
Shall we remove the earlier one?
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.
removed from the README but didn't remove the sample.