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

Update ai sample code #71

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
66 changes: 29 additions & 37 deletions answer_questions_using_vector_search_weaviate/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

[[package]]
org = "ai_samples"
Expand All @@ -23,20 +24,19 @@ modules = [
[[package]]
org = "ballerina"
name = "auth"
version = "2.6.0"
version = "2.12.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"},
{org = "ballerina", name = "regex"}
{org = "ballerina", name = "log"}
]

[[package]]
org = "ballerina"
name = "cache"
version = "3.4.0"
version = "3.8.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "jballerina.java"},
Expand All @@ -47,15 +47,15 @@ dependencies = [
[[package]]
org = "ballerina"
name = "constraint"
version = "1.1.0"
version = "1.5.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "crypto"
version = "2.3.0"
version = "2.7.2"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "time"}
Expand All @@ -64,20 +64,18 @@ dependencies = [
[[package]]
org = "ballerina"
name = "file"
version = "1.6.0"
version = "1.10.0"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "os"},
{org = "ballerina", name = "regex"},
{org = "ballerina", name = "time"}
]

[[package]]
org = "ballerina"
name = "http"
version = "2.6.1"
version = "2.12.0"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "cache"},
Expand All @@ -90,25 +88,26 @@ dependencies = [
{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 = "regex"},
{org = "ballerina", name = "time"},
{org = "ballerina", name = "url"}
]
modules = [
{org = "ballerina", packageName = "http", moduleName = "http"}
{org = "ballerina", packageName = "http", moduleName = "http"},
{org = "ballerina", packageName = "http", moduleName = "http.httpscerr"}
]

[[package]]
org = "ballerina"
name = "io"
version = "1.4.0"
version = "1.6.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.value"}
Expand All @@ -122,15 +121,15 @@ version = "0.0.0"
[[package]]
org = "ballerina"
name = "jwt"
version = "2.6.0"
version = "2.13.0"
dependencies = [
{org = "ballerina", name = "cache"},
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.int"},
{org = "ballerina", name = "lang.string"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "regex"},
{org = "ballerina", name = "time"}
]

Expand Down Expand Up @@ -165,7 +164,9 @@ org = "ballerina"
name = "lang.int"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.__internal"},
{org = "ballerina", name = "lang.object"}
]

[[package]]
Expand Down Expand Up @@ -209,7 +210,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "log"
version = "2.6.0"
version = "2.10.0"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand All @@ -220,17 +221,18 @@ dependencies = [
[[package]]
org = "ballerina"
name = "mime"
version = "2.6.0"
version = "2.10.0"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.int"}
{org = "ballerina", name = "lang.int"},
{org = "ballerina", name = "log"}
]

[[package]]
org = "ballerina"
name = "oauth2"
version = "2.6.1"
version = "2.12.0"
dependencies = [
{org = "ballerina", name = "cache"},
{org = "ballerina", name = "crypto"},
Expand All @@ -243,33 +245,24 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.0.7"
version = "1.3.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

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

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

[[package]]
org = "ballerina"
name = "task"
version = "2.3.1"
version = "2.5.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "time"}
Expand All @@ -278,15 +271,15 @@ dependencies = [
[[package]]
org = "ballerina"
name = "time"
version = "2.2.4"
version = "2.5.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "url"
version = "2.2.3"
version = "2.4.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand All @@ -306,7 +299,7 @@ modules = [
[[package]]
org = "ballerinax"
name = "openai.embeddings"
version = "1.0.2"
version = "1.0.5"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "http"},
Expand All @@ -320,7 +313,7 @@ modules = [
[[package]]
org = "ballerinax"
name = "weaviate"
version = "1.0.0"
version = "1.0.2"
dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "url"},
Expand All @@ -329,4 +322,3 @@ dependencies = [
modules = [
{org = "ballerinax", packageName = "weaviate", moduleName = "weaviate"}
]

5 changes: 3 additions & 2 deletions answer_questions_using_vector_search_weaviate/main.bal
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import ballerina/http;
import ballerinax/openai.embeddings;
import ballerinax/weaviate;
import ballerina/io;

configurable string openAIToken = ?;
configurable string weaviateToken = ?;
configurable string weaviateURL = ?;

const CLASS_NAME = "QuestionAnswerStore";
const MODEL = "text-embedding-ada-002";
const MODEL = "text-embedding-3-small";

final embeddings:Client openai = check new ({auth: {token: openAIToken}});
final weaviate:Client weaviate = check new ({auth: {token: weaviateToken}}, weaviateURL);
Expand Down Expand Up @@ -42,7 +43,7 @@ service / on new http:Listener(8080) {
}`;

weaviate:GraphQLResponse results = check weaviate->/graphql.post({query: graphQLQuery});

io:println("Results: ", results);
return results.data;
}
}
1 change: 0 additions & 1 deletion audio_to_text_summarization_using_openai/Ballerina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ distribution = "2201.4.0"

[build-options]
observabilityIncluded = true

1 change: 0 additions & 1 deletion audio_to_text_summarization_using_openai/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,3 @@ modules = [
{org = "ballerinax", packageName = "twitter", moduleName = "twitter"},
{org = "ballerinax", packageName = "twitter", moduleName = "twitter.listener"}
]

40 changes: 17 additions & 23 deletions audio_to_text_summarization_using_openai/main.bal
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
import ballerina/http;
import ballerina/io;
import ballerinax/openai.text;
import ballerinax/openai.chat;
import ballerinax/openai.audio;
import ballerinax/twitter;

configurable string openAIToken = ?;

configurable string twitterApiKey = ?;
configurable string twitterApiSecret = ?;
configurable string twitterAccessToken = ?;
configurable string twitterAccessTokenSecret = ?;
configurable string token = ?;

const string AUDIO_FILE = "test.mp3";
const string AUDIO_FILE_PATH = "podcast-clips/" + AUDIO_FILE;
const string AUDIO_FILE_PATH = "./audo_clips/" + AUDIO_FILE;
const int BINARY_LENGTH = 1000000;

// Twitter API client configuration
twitter:ConnectionConfig twitterConfig = {
apiKey: twitterApiKey,
apiSecret: twitterApiSecret,
accessToken: twitterAccessToken,
accessTokenSecret: twitterAccessTokenSecret
};

public function main(string podcastURL) returns error? {
// Creates a HTTP client to download the audio file
http:Client podcastEP = check new (podcastURL);
http:Response httpResp = check podcastEP->/get();
http:Response httpResp = check podcastEP->get("");
DevAakif marked this conversation as resolved.
Show resolved Hide resolved
byte[] audioBytes = check httpResp.getBinaryPayload();
check io:fileWriteBytes(AUDIO_FILE_PATH, audioBytes);

// Creates a request to translate the audio file to text (English)
audio:CreateTranscriptionRequest transcriptionsReq = {
file: {fileContent: (check io:fileReadBytes(AUDIO_FILE_PATH)).slice(0, BINARY_LENGTH), fileName: AUDIO_FILE},
file: {fileContent: (check io:fileReadBytes(AUDIO_FILE_PATH)), fileName: AUDIO_FILE},
model: "whisper-1"
};

Expand All @@ -41,10 +31,14 @@ public function main(string podcastURL) returns error? {
audio:CreateTranscriptionResponse transcriptionsRes = check openAIAudio->/audio/transcriptions.post(transcriptionsReq);
io:println("Text from the audio :", transcriptionsRes.text);


// Creates a request to summarize the text
text:CreateCompletionRequest textCompletionReq = {
model: "text-davinci-003",
prompt: string `Summarize the following text to 100 characters : ${transcriptionsRes.text}`,
chat:CreateChatCompletionRequest request = {
model: "gpt-4o-mini",
messages: [{
role: "user",
content: string `Summarize the following text to 100 characters : ${transcriptionsRes.text}`
}],
temperature: 0.7,
max_tokens: 256,
top_p: 1,
Expand All @@ -53,17 +47,17 @@ public function main(string podcastURL) returns error? {
};

// Summarizes the text using OpenAI text completion API
text:Client openAIText = check new ({auth: {token: openAIToken}});
text:CreateCompletionResponse completionRes = check openAIText->/completions.post(textCompletionReq);
final chat:Client openAIChat = check new({auth: {token:openAIToken}});
chat:CreateChatCompletionResponse completionRes = check openAIChat->/chat/completions.post(request);

string? summerizedText = completionRes.choices[0].text;
string? summerizedText = completionRes.choices[0].message.content;
if summerizedText is () {
return error("Failed to summarize the given audio.");
}
io:println("Summarized text: ", summerizedText);

// Tweet it out!
twitter:Client twitter = check new (twitterConfig);
var tweet = check twitter->tweet(summerizedText);
final twitter:Client twitter = check new({auth: {token:token}});
twitter:TweetCreateResponse tweet = check twitter->/tweets.post({text: summerizedText});
io:println("Tweet: ", tweet);
}
4 changes: 2 additions & 2 deletions compare_similarity_between_two_texts_using_openai/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public function main() returns error? {
final embeddings:Client openAIEmbeddings = check new ({auth: {token: openAIToken}});

string text1 = "What are you thinking?";
string text2 = "What is on your mind?";
string text2 = "you are playing cricket";
embeddings:CreateEmbeddingRequest embeddingReq = {
model: "text-embedding-ada-002",
model: "text-embedding-3-small",
input: [text1, text2]
};
embeddings:CreateEmbeddingResponse embeddingRes = check openAIEmbeddings->/embeddings.post(embeddingReq);
Expand Down
Loading