Skip to content

Instantly share code, notes, and snippets.

@muyiwexy
Created November 29, 2023 01:31
Show Gist options
  • Save muyiwexy/bbca1411fd10cdc94bc2dd1e28e6795b to your computer and use it in GitHub Desktop.
Save muyiwexy/bbca1411fd10cdc94bc2dd1e28e6795b to your computer and use it in GitHub Desktop.
class LangchainServicesImpl extends LangchainService {
final Connection connection;
final OpenAIEmbeddings embeddings;
final OpenAI openAI;
LangchainServicesImpl({
required this.connection,
required this.embeddings,
required this.openAI,
});
// do something
@override
Future<String> queryNeonTable(String tableName, String query) async {
final embedQuery = await embeddings.embedQuery(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 10;");
List<Metadata> pdfMetadata = getSimilar
.map((item) => Metadata.fromJson(json.decode(item[1])))
.toList();
if (pdfMetadata.isNotEmpty) {
final concatPageContent = pdfMetadata.map((e) {
return e.pageContent;
}).join(' ');
final docChain = StuffDocumentsQAChain(llm: openAI);
final response = await docChain.call({
'input_documents': [
Document(pageContent: concatPageContent),
],
'question': query,
});
return response['output'];
} 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