Skip to content

Commit

Permalink
Merge branch 'arakoodev:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
hemantDwivedi authored Sep 14, 2023
2 parents 1aab86c + 2969024 commit 3a97369
Show file tree
Hide file tree
Showing 25 changed files with 974 additions and 339 deletions.
64 changes: 37 additions & 27 deletions Examples/pinecone/PineconeExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ public class PineconeExample {

private static final String OPENAI_AUTH_KEY = ""; // YOUR OPENAI AUTH KEY
private static final String OPENAI_ORG_ID = ""; // YOUR OPENAI ORG ID

private static final String PINECONE_AUTH_KEY = "";
private static final String PINECONE_QUERY_API = "";
private static final String PINECONE_UPSERT_API = "";
private static final String PINECONE_DELETE = "";

private static OpenAiEndpoint ada002Embedding;
private static OpenAiEndpoint gpt3Endpoint;
private static OpenAiEndpoint gpt3StreamEndpoint;

private static PineconeEndpoint upsertPineconeEndpoint;
private static PineconeEndpoint queryPineconeEndpoint;
Expand All @@ -64,7 +66,7 @@ public static void main(String[] args) {

// Redis Configuration
properties.setProperty("redis.url", "");
properties.setProperty("redis.port", "");
properties.setProperty("redis.port", "12285");
properties.setProperty("redis.username", "default");
properties.setProperty("redis.password", "");
properties.setProperty("redis.ttl", "3600");
Expand Down Expand Up @@ -96,19 +98,40 @@ public static void main(String[] args) {
0.85,
new ExponentialDelay(3, 5, 2, TimeUnit.SECONDS));

gpt3StreamEndpoint =
new OpenAiEndpoint(
OPENAI_CHAT_COMPLETION_API,
OPENAI_AUTH_KEY,
OPENAI_ORG_ID,
"gpt-3.5-turbo",
"user",
0.85,
true,
new ExponentialDelay(3, 5, 2, TimeUnit.SECONDS));

upsertPineconeEndpoint =
new PineconeEndpoint(
PINECONE_UPSERT_API,
PINECONE_AUTH_KEY,
"machine-learning", // Passing namespace; read more on Pinecone documentation. You can
// pass empty string
new ExponentialDelay(3, 3, 2, TimeUnit.SECONDS));

queryPineconeEndpoint =
new PineconeEndpoint(
PINECONE_QUERY_API, PINECONE_AUTH_KEY, new ExponentialDelay(3, 3, 2, TimeUnit.SECONDS));
PINECONE_QUERY_API,
PINECONE_AUTH_KEY,
"machine-learning", // Passing namespace; read more on Pinecone documentation. You can
// pass empty string
new ExponentialDelay(3, 3, 2, TimeUnit.SECONDS));

deletePineconeEndpoint =
new PineconeEndpoint(
PINECONE_DELETE, PINECONE_AUTH_KEY, new FixedDelay(4, 5, TimeUnit.SECONDS));
PINECONE_DELETE,
PINECONE_AUTH_KEY,
"machine-learning", // Passing namespace; read more on Pinecone documentation. You can
// pass empty string
new FixedDelay(4, 5, TimeUnit.SECONDS));

contextEndpoint =
new RedisHistoryContextEndpoint(new ExponentialDelay(2, 2, 2, TimeUnit.SECONDS));
Expand Down Expand Up @@ -158,13 +181,8 @@ public class PineconeController {
// Namespace is optional (if not provided, it will be using Empty String "")
@PostMapping("/pinecone/upsert") // /v1/examples/openai/upsert?namespace=machine-learning
public void upsertPinecone(ArkRequest arkRequest) throws IOException {

String namespace = arkRequest.getQueryParam("namespace");
InputStream file = arkRequest.getMultiPart("file").getInputStream();

// Configure Pinecone
upsertPineconeEndpoint.setNamespace(namespace);

String[] arr = pdfReader.readByChunkSize(file, 512);

/**
Expand All @@ -181,13 +199,9 @@ public void upsertPinecone(ArkRequest arkRequest) throws IOException {
@PostMapping(value = "/pinecone/query")
public ArkResponse query(ArkRequest arkRequest) {

String namespace = arkRequest.getQueryParam("namespace");
String query = arkRequest.getBody().getString("query");
int topK = arkRequest.getIntQueryParam("topK");

// Configure Pinecone
queryPineconeEndpoint.setNamespace(namespace);

// Step 1: Chain ==> Get Embeddings From Input & Then Query To Pinecone
EdgeChain<WordEmbeddings> embeddingsChain =
new EdgeChain<>(ada002Embedding.embeddings(query, arkRequest));
Expand Down Expand Up @@ -216,15 +230,8 @@ public ArkResponse chatWithPinecone(ArkRequest arkRequest) {

String contextId = arkRequest.getQueryParam("id");
String query = arkRequest.getBody().getString("query");
String namespace = arkRequest.getQueryParam("namespace");
boolean stream = arkRequest.getBooleanHeader("stream");

// Configure Pinecone
queryPineconeEndpoint.setNamespace(namespace);

// Configure GPT3endpoint
gpt3Endpoint.setStream(stream);

// Get HistoryContext
HistoryContext historyContext = contextEndpoint.get(contextId);

Expand Down Expand Up @@ -262,16 +269,16 @@ public ArkResponse chatWithPinecone(ArkRequest arkRequest) {
EdgeChain<String> promptChain =
queryChain.transform(queries -> chatFn(historyContext.getResponse(), queries));

// Chain 5 ==> Pass the Prompt To Gpt3
EdgeChain<ChatCompletionResponse> gpt3Chain =
new EdgeChain<>(
gpt3Endpoint.chatCompletion(promptChain.get(), "PineconeChatChain", arkRequest));

// (FOR NON STREAMING)
// If it's not stream ==>
// Query(What is the collect stage for data maturity) + OpenAiResponse + Prev. ChatHistory
if (!stream) {

// Chain 5 ==> Pass the Prompt To Gpt3
EdgeChain<ChatCompletionResponse> gpt3Chain =
new EdgeChain<>(
gpt3Endpoint.chatCompletion(promptChain.get(), "RedisChatChain", arkRequest));

// Chain 6
EdgeChain<ChatCompletionResponse> historyUpdatedChain =
gpt3Chain.doOnNext(
Expand All @@ -288,8 +295,13 @@ public ArkResponse chatWithPinecone(ArkRequest arkRequest) {
// For STREAMING Version
else {

// Chain 5 ==> Pass the Prompt To Gpt3
EdgeChain<ChatCompletionResponse> gpt3Chain =
new EdgeChain<>(
gpt3StreamEndpoint.chatCompletion(promptChain.get(), "RedisChatChain", arkRequest));

/* As the response is in stream, so we will use StringBuilder to append the response
and once GPT chain indicates that it is finished, we will save the following into Postgres
and once GPT chain indicates that it is finished, we will save the following into Redis
Query(What is the collect stage for data maturity) + OpenAiResponse + Prev. ChatHistory
*/

Expand Down Expand Up @@ -318,8 +330,6 @@ public ArkResponse chatWithPinecone(ArkRequest arkRequest) {
// Namespace is optional (if not provided, it will be using Empty String "")
@DeleteMapping("/pinecone/deleteAll")
public ArkResponse deletePinecone(ArkRequest arkRequest) {
String namespace = arkRequest.getQueryParam("namespace");
deletePineconeEndpoint.setNamespace(namespace);
return new EdgeChain<>(deletePineconeEndpoint.deleteAll()).getArkResponse();
}

Expand Down
57 changes: 36 additions & 21 deletions Examples/postgresql/PostgreSQLExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.edgechain.lib.endpoint.impl.*;
import com.edgechain.lib.index.domain.PostgresWordEmbeddings;
import com.edgechain.lib.index.enums.PostgresDistanceMetric;
import com.edgechain.lib.index.enums.PostgresLanguage;
import com.edgechain.lib.jsonnet.JsonnetArgs;
import com.edgechain.lib.jsonnet.JsonnetLoader;
import com.edgechain.lib.jsonnet.enums.DataType;
Expand Down Expand Up @@ -39,6 +40,7 @@ public class PostgreSQLExample {

private static OpenAiEndpoint ada002Embedding;
private static OpenAiEndpoint gpt3Endpoint;
private static OpenAiEndpoint gpt3StreamEndpoint;
private static PostgresEndpoint postgresEndpoint;
private static PostgreSQLHistoryContextEndpoint contextEndpoint;

Expand Down Expand Up @@ -85,8 +87,21 @@ public static void main(String[] args) {
0.85,
new ExponentialDelay(3, 5, 2, TimeUnit.SECONDS));

gpt3StreamEndpoint =
new OpenAiEndpoint(
OPENAI_CHAT_COMPLETION_API,
OPENAI_AUTH_KEY,
OPENAI_ORG_ID,
"gpt-3.5-turbo",
"user",
0.85,
true,
new ExponentialDelay(3, 5, 2, TimeUnit.SECONDS));

// Defining tablename and namespace...
postgresEndpoint =
new PostgresEndpoint("spring_vectors", new ExponentialDelay(5, 5, 2, TimeUnit.SECONDS));
new PostgresEndpoint(
"pg_vectors", "machine-learning", new ExponentialDelay(5, 5, 2, TimeUnit.SECONDS));
contextEndpoint = new PostgreSQLHistoryContextEndpoint(new FixedDelay(2, 3, TimeUnit.SECONDS));
}

Expand Down Expand Up @@ -137,19 +152,22 @@ public class PostgreSQLController {
*/
@PostMapping("/postgres/upsert")
public void upsert(ArkRequest arkRequest) throws IOException {

String namespace = arkRequest.getQueryParam("namespace");
String filename = arkRequest.getMultiPart("file").getSubmittedFileName();
InputStream file = arkRequest.getMultiPart("file").getInputStream();

postgresEndpoint.setNamespace(namespace);

String[] arr = pdfReader.readByChunkSize(file, 512);

PostgresRetrieval retrieval =
new PostgresRetrieval(arr, ada002Embedding, postgresEndpoint, 1536, filename, arkRequest);
new PostgresRetrieval(
arr,
ada002Embedding,
postgresEndpoint,
1536,
filename,
PostgresLanguage.ENGLISH,
arkRequest);

// retrieval.setBatchSize(100); // Modifying batchSize....(Default is 50)
// retrieval.setBatchSize(50); // Modifying batchSize....(Default is 30)

// Getting ids from upsertion... Internally, it automatically parallelizes the operation...
List<String> ids = retrieval.upsert();
Expand All @@ -162,12 +180,9 @@ public void upsert(ArkRequest arkRequest) throws IOException {
@PostMapping(value = "/postgres/query")
public ArkResponse query(ArkRequest arkRequest) {

String namespace = arkRequest.getQueryParam("namespace");
String query = arkRequest.getBody().getString("query");
int topK = arkRequest.getIntQueryParam("topK");

postgresEndpoint.setNamespace(namespace);

// Chain 1==> Get Embeddings From Input & Then Query To PostgreSQL
EdgeChain<WordEmbeddings> embeddingsChain =
new EdgeChain<>(ada002Embedding.embeddings(query, arkRequest));
Expand Down Expand Up @@ -195,15 +210,9 @@ public ArkResponse chat(ArkRequest arkRequest) {
String contextId = arkRequest.getQueryParam("id");

String query = arkRequest.getBody().getString("query");
String namespace = arkRequest.getQueryParam("namespace");

boolean stream = arkRequest.getBooleanHeader("stream");

// Configure PostgresEndpoint
postgresEndpoint.setNamespace(namespace);

gpt3Endpoint.setStream(stream);

// Get HistoryContext
HistoryContext historyContext = contextEndpoint.get(contextId);

Expand Down Expand Up @@ -242,16 +251,16 @@ public ArkResponse chat(ArkRequest arkRequest) {
EdgeChain<String> promptChain =
queryChain.transform(queries -> chatFn(historyContext.getResponse(), queries));

// Chain 5 ==> Pass the Prompt To Gpt3
EdgeChain<ChatCompletionResponse> gpt3Chain =
new EdgeChain<>(
gpt3Endpoint.chatCompletion(promptChain.get(), "PostgresChatChain", arkRequest));

// (FOR NON STREAMING)
// If it's not stream ==>
// Query(What is the collect stage for data maturity) + OpenAiResponse + Prev. ChatHistory
if (!stream) {

// Chain 5 ==> Pass the Prompt To Gpt3
EdgeChain<ChatCompletionResponse> gpt3Chain =
new EdgeChain<>(
gpt3Endpoint.chatCompletion(promptChain.get(), "PostgresChatChain", arkRequest));

// Chain 6
EdgeChain<ChatCompletionResponse> historyUpdatedChain =
gpt3Chain.doOnNext(
Expand All @@ -268,6 +277,12 @@ public ArkResponse chat(ArkRequest arkRequest) {
// For STREAMING Version
else {

// Chain 5 ==> Pass the Prompt To Gpt3
EdgeChain<ChatCompletionResponse> gpt3Chain =
new EdgeChain<>(
gpt3StreamEndpoint.chatCompletion(
promptChain.get(), "PostgresChatChain", arkRequest));

/* As the response is in stream, so we will use StringBuilder to append the response
and once GPT chain indicates that it is finished, we will save the following into Postgres
Query(What is the collect stage for data maturity) + OpenAiResponse + Prev. ChatHistory
Expand Down
Loading

0 comments on commit 3a97369

Please sign in to comment.