-
-
Save muyiwexy/bbca1411fd10cdc94bc2dd1e28e6795b to your computer and use it in GitHub Desktop.
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
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