Skip to content

Instantly share code, notes, and snippets.

@muyiwexy
Created February 20, 2024 11:52
Show Gist options
  • Save muyiwexy/6e3318ae0c07802c10f3cabc2450c69d to your computer and use it in GitHub Desktop.
Save muyiwexy/6e3318ae0c07802c10f3cabc2450c69d to your computer and use it in GitHub Desktop.
class OpenAIIndexingServicesImpl extends OpenAIIndexingServices {
late final http.Client client;
late final Connection connection;
OpenAIIndexingServicesImpl({
required this.client,
required this.connection,
});
Future<String> getCompletionFromMessages(messages,
{model = "gpt-3.5-turbo-1106", temperature = 0, maxtokens = 1000}) async {
final response = await client.post(
Uri.parse("https://api.openai.com/v1/chat/completions"),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ${dotenv.env['OPENAI_API_KEY']!}',
},
body: jsonEncode({
'model': model,
'messages': messages,
'max_tokens': maxtokens,
}),
);
if (response.statusCode == 200) {
final Map<String, dynamic> responseData = jsonDecode(response.body);
String content = responseData\['choices'\][0]\['message'\]['content'];
return content;
} else {
throw response.body;
}
}
Future<List<double>> getQueryEmbeddings(String query) async {
debugPrint("Embedding ....");
final response = await http.post(
Uri.parse("https://api.openai.com/v1/embeddings"),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ${dotenv.env['OPENAI_API_KEY']!}',
},
body: jsonEncode({
'model': 'babbage-002',
'input': query,
}),
);
if (response.statusCode == 200) {
final Map<String, dynamic> responseData = jsonDecode(response.body);
List<dynamic> embedding = responseData\['data'\][0]['embedding'];
List<double> embeddingdouble = embedding.map((item) {
if (item is double) {
return item;
} else {
throw const FormatException('Invalid format');
}
}).toList();
debugPrint("Embedding complete....");
return embeddingdouble;
} else {
throw response.body;
}
}
@override
Future<String> queryNeonTable(String tableName, String query) async {
print("Query started ...");
String delimiter = "```";
final embedQuery = await getQueryEmbeddings(query);
List<List<dynamic>> getSimilar = await connection.execute(
"SELECT *, 1 - (embedding <=> '$embedQuery') AS cosine_similarity FROM $tableName WHERE (1 - (embedding <=> '$embedQuery')) BETWEEN 0.3 AND 1.00 ORDER BY cosine_similarity DESC LIMIT 3;");
List<Metadata> csvMetadata = getSimilar
.map((item) => Metadata.fromJson(
json.decode(item[1]),
))
.toList();
if (csvMetadata.isNotEmpty) {
final concatPageContent = csvMetadata.map((e) {
return e.pageContent;
}).join(' ');
print(concatPageContent);
String systemMessage = """
You are a friendly chatbot. \n
You can answer questions about Implementing OAuth2 Clients with Flutter and Appwrite. \n
You respond in a concise, technically credible tone. \n
""";
List<Map<String, String>> messages = [
{"role": "system", "content": systemMessage},
{"role": "user", "content": "$delimiter$query$delimiter"},
{
"role": "assistant",
"content":
"Relevant Appwrite OAuth2 case studies \n $concatPageContent"
}
];
final finalResponse = await getCompletionFromMessages(messages);
print("Query Recieved: $finalResponse");
return finalResponse;
} else {
return "Couldn't find anything on that topic";
}
}
}
void debugPrint(String message) {
if (kDebugMode) {
print(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment