Skip to content

Instantly share code, notes, and snippets.

@xlight05
Last active June 26, 2024 15:08
Show Gist options
  • Save xlight05/8c923bbe88daceee668c977b6bee158e to your computer and use it in GitHub Desktop.
Save xlight05/8c923bbe88daceee668c977b6bee158e to your computer and use it in GitHub Desktop.
import ballerina/http;
import ballerinax/azure.openai.chat;
configurable string apiKey = ?;
final chat:Client chatClient = check new (
config = {
auth: {apiKey: apiKey}
},
serviceUrl = "https://ballerina.openai.azure.com/openai"
);
final readonly & Article[] articles = [
{id: 1, title: "Article 1", author: "Author 1", content: "Content 1", tags: ["tag1", "tag2"]},
{id: 2, title: "Article 2", author: "Author 2", content: "Content 2", tags: ["tag1", "tag3"]},
{id: 3, title: "Article 3", author: "Author 3", content: "Content 3", tags: ["tag2", "tag3"]}
];
public type Article record {
int id;
string title;
string author;
string content;
string[] tags;
};
service on new http:Listener(9090) {
resource function get articles() returns Article[] {
return articles;
}
resource function get article/[int id]() returns Article|http:NotFound {
(Article & readonly)[] listResult = (from Article article in articles
where article.id == id
select article);
if listResult.length() == 0 {
return http:NOT_FOUND;
}
return listResult[0];
}
resource function get article/recommendations(string context) returns Article[]|error {
return getRecommendations(context);
}
}
isolated function getRecommendations(string context) returns Article[]|error {
string systemPrompt = "You are a recommendation system.";
string userPrompt = string
`Recommend two articles based on the behavior context of the user from the articles provided in the article list JSON.
# Context
${context}
# Article list
${articles.toJsonString()}
Only return using json array format. Keep the existing structure.
`;
return sendRecommendationAPICall(systemPrompt, userPrompt);
}
isolated function sendRecommendationAPICall(string systemPrompt, string userPrompt)returns Article[]|error {
chat:ChatCompletionRequestMessage[] questionMessages = [
<chat:ChatCompletionRequestMessageSystem>{role: "system", content: systemPrompt},
<chat:ChatCompletionRequestMessageUser>{role: "user", content: userPrompt}
];
chat:CreateChatCompletionRequest chatBody = {
messages: questionMessages,
temperature: 0,
response_format: {
'type: "json_object"
}
};
string deployment = "gpt4-turbo-prev";
chat:CreateChatCompletionResponse chatResult = check chatClient->/deployments/[deployment]/chat/completions.post("2023-08-01-preview", chatBody);
record {|chat:ChatCompletionResponseMessage message?; chat:ContentFilterChoiceResults content_filter_results?; int index?; string finish_reason?; anydata...;|}[]? choices = chatResult.choices;
if choices is () {
return error("No choices found in the response");
}
string resp = check choices[0].message?.content.ensureType();
json libJsonList = check resp.fromJsonString();
Article[] libList = check libJsonList.cloneWithType();
return libList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment