Skip to content

Commit

Permalink
Added Java code into ts branch (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh4902 authored Dec 18, 2023
1 parent ad18f97 commit 043a6e1
Show file tree
Hide file tree
Showing 179 changed files with 9,489 additions and 2,672 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified JS/.DS_Store
Binary file not shown.
Binary file added Java/.DS_Store
Binary file not shown.
Binary file modified Java/Examples/.DS_Store
Binary file not shown.
134 changes: 134 additions & 0 deletions Java/Examples/airtable/AirtableExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package com.edgechain;

import com.edgechain.lib.endpoint.impl.integration.AirtableEndpoint;
import com.edgechain.lib.integration.airtable.query.AirtableQueryBuilder;
import com.edgechain.lib.integration.airtable.query.SortOrder;
import com.edgechain.lib.request.ArkRequest;
import com.edgechain.lib.response.ArkResponse;
import com.edgechain.lib.rxjava.transformer.observable.EdgeChain;
import dev.fuxing.airtable.AirtableRecord;
import dev.fuxing.airtable.formula.AirtableFormula;
import dev.fuxing.airtable.formula.LogicalOperator;
import org.json.JSONObject;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.bind.annotation.*;

import java.util.Properties;

/**
* For the purpose, of this example, create a simple table using AirTable i.e, "Speakers" Following
* are some basic fields: "speaker_name", "designation", "organization", "biography",
* "speaker_photo", "rating To get, your BASE_ID of specific database; use the following API
* https://api.airtable.com/v0/meta/bases --header 'Authorization: Bearer
* YOUR_PERSONAL_ACCESS_TOKEN' You can create, complex tables using Airtable; also define
* relationships b/w tables via lookups.
*/
@SpringBootApplication
public class AirtableExample {
private static final String AIRTABLE_API_KEY = "";
private static final String AIRTABLE_BASE_ID = "";

private static AirtableEndpoint airtableEndpoint;

public static void main(String[] args) {

System.setProperty("server.port", "8080");

Properties properties = new Properties();
properties.setProperty("cors.origins", "http://localhost:4200");

new SpringApplicationBuilder(AirtableExample.class).properties(properties).run(args);

airtableEndpoint = new AirtableEndpoint(AIRTABLE_BASE_ID, AIRTABLE_API_KEY);
}

@RestController
@RequestMapping("/airtable")
public class AirtableController {

@GetMapping("/findAll")
public ArkResponse findAll(ArkRequest arkRequest) {

int pageSize = arkRequest.getIntQueryParam("pageSize");
String sortSpeakerName = arkRequest.getQueryParam("sortName");
String offset = arkRequest.getQueryParam("offset");

AirtableQueryBuilder queryBuilder = new AirtableQueryBuilder();
queryBuilder.pageSize(pageSize); // pageSize --> no. of records in each request
queryBuilder.sort("speaker_name", SortOrder.fromValue(sortSpeakerName).getValue());
queryBuilder.offset(offset); // move to next page by passing offset returned in response;

// Return only those speakers which have rating Greater Than Eq to 3;
queryBuilder.filterByFormula(
LogicalOperator.GTE,
AirtableFormula.Object.field("rating"),
AirtableFormula.Object.value(3));

return new EdgeChain<>(airtableEndpoint.findAll("Speakers", queryBuilder)).getArkResponse();
}

@GetMapping("/find")
public ArkResponse findById(ArkRequest arkRequest) {
String id = arkRequest.getQueryParam("id");
return new EdgeChain<>(airtableEndpoint.findById("Speakers", id)).getArkResponse();
}

@PostMapping("/create")
public ArkResponse create(ArkRequest arkRequest) {

JSONObject body = arkRequest.getBody();
String speakerName = body.getString("name");
String designation = body.getString("designation");
int rating = body.getInt("rating");
String organization = body.getString("organization");
String biography = body.getString("biography");

// Airtable API doesn't allow to upload blob files directly; therefore, you would require to
// upload it
// to some cloud storage i.e, S3 and then set the URL in Airtable.

AirtableRecord record = new AirtableRecord();
record.putField("speaker_name", speakerName);
record.putField("designation", designation);
record.putField("rating", rating);
record.putField("organization", organization);
record.putField("biography", biography);

return new EdgeChain<>(airtableEndpoint.create("Speakers", record)).getArkResponse();
}

@PostMapping("/update")
public ArkResponse update(ArkRequest arkRequest) {

JSONObject body = arkRequest.getBody();
String id = body.getString("id");
String speakerName = body.getString("name");
String designation = body.getString("designation");
int rating = body.getInt("rating");
String organization = body.getString("organization");
String biography = body.getString("biography");

// Airtable API doesn't allow to upload blob files directly; therefore, you would require to
// upload it
// to some cloud storage i.e, S3 and then set the URL in Airtable.

AirtableRecord record = new AirtableRecord();
record.setId(id);
record.putField("speaker_name", speakerName);
record.putField("designation", designation);
record.putField("rating", rating);
record.putField("organization", organization);
record.putField("biography", biography);

return new EdgeChain<>(airtableEndpoint.update("Speakers", record)).getArkResponse();
}

@DeleteMapping("/delete")
public ArkResponse delete(ArkRequest arkRequest) {
JSONObject body = arkRequest.getBody();
String id = body.getString("id");
return new EdgeChain<>(airtableEndpoint.delete("Speakers", id)).getArkResponse();
}
}
}
6 changes: 3 additions & 3 deletions Java/Examples/code-interpreter/CodeInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.concurrent.TimeUnit;

import com.edgechain.lib.endpoint.impl.llm.OpenAiChatEndpoint;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -13,7 +14,6 @@
import org.springframework.web.bind.annotation.RestController;

import com.edgechain.lib.codeInterpreter.Eval;
import com.edgechain.lib.endpoint.impl.OpenAiEndpoint;
import com.edgechain.lib.jsonnet.JsonnetArgs;
import com.edgechain.lib.jsonnet.JsonnetLoader;
import com.edgechain.lib.jsonnet.enums.DataType;
Expand All @@ -28,7 +28,7 @@
public class CodeInterpreter {

private static final String OPENAI_AUTH_KEY = "";
private static OpenAiEndpoint userChatEndpoint;
private static OpenAiChatEndpoint userChatEndpoint;
private static final ObjectMapper objectMapper = new ObjectMapper();
private static JsonnetLoader loader =
new FileJsonnetLoader("./code-interpreter/code-interpreter.jsonnet");
Expand All @@ -48,7 +48,7 @@ public double interpret(ArkRequest arkRequest) throws JSONException {
JSONObject json = arkRequest.getBody();

userChatEndpoint =
new OpenAiEndpoint(
new OpenAiChatEndpoint(
OPENAI_CHAT_COMPLETION_API,
OPENAI_AUTH_KEY,
"gpt-3.5-turbo",
Expand Down
60 changes: 30 additions & 30 deletions Java/Examples/htmx-ui-demo/ChatMessage.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package com.edgechain;

public class ChatMessage {
String role;
String content;

public ChatMessage(String role, String content) {
this.role = role;
this.content = content;
}

public ChatMessage() {}

public String getRole() {
return role;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

@Override
public String toString() {
return "ChatMessage{" + "role='" + role + '\'' + ", content='" + content + '\'' + '}';
}
}
package com.edgechain;

public class ChatMessage {
String role;
String content;

public ChatMessage(String role, String content) {
this.role = role;
this.content = content;
}

public ChatMessage() {}

public String getRole() {
return role;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

@Override
public String toString() {
return "ChatMessage{" + "role='" + role + '\'' + ", content='" + content + '\'' + '}';
}
}
3 changes: 1 addition & 2 deletions Java/Examples/htmx-ui-demo/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.edgechain;
public class User {
class User {
public String email;
public String password;

Expand Down
10 changes: 5 additions & 5 deletions Java/Examples/json/JsonFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.edgechain.lib.endpoint.impl.llm.OpenAiChatEndpoint;
import org.json.JSONObject;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
Expand All @@ -17,7 +18,6 @@
import org.springframework.web.client.RestTemplate;

import com.edgechain.lib.constants.EndpointConstants;
import com.edgechain.lib.endpoint.impl.OpenAiEndpoint;
import com.edgechain.lib.jsonFormat.request.FunctionRequest;
import com.edgechain.lib.jsonFormat.request.Message;
import com.edgechain.lib.jsonFormat.request.OpenApiFunctionRequest;
Expand All @@ -41,7 +41,7 @@ public class JsonFormat {
// need only for situation endpoint
private static final String OPENAI_ORG_ID = "";

private static OpenAiEndpoint userChatEndpoint;
private static OpenAiChatEndpoint userChatEndpoint;
private static JsonnetLoader loader = new FileJsonnetLoader("./json/json-format.jsonnet");
private static JsonnetLoader functionLoader = new FileJsonnetLoader("./json/function.jsonnet");
private static final ObjectMapper objectMapper = new ObjectMapper();
Expand Down Expand Up @@ -84,7 +84,7 @@ public class ExampleController {
public String extract(ArkRequest arkRequest) {

userChatEndpoint =
new OpenAiEndpoint(
new OpenAiChatEndpoint(
OPENAI_CHAT_COMPLETION_API,
OPENAI_AUTH_KEY,
"gpt-3.5-turbo",
Expand Down Expand Up @@ -129,8 +129,8 @@ public String situation(ArkRequest arkRequest) {

JSONObject json = arkRequest.getBody();

OpenAiEndpoint userChat =
new OpenAiEndpoint(
OpenAiChatEndpoint userChat =
new OpenAiChatEndpoint(
EndpointConstants.OPENAI_CHAT_COMPLETION_API,
OPENAI_AUTH_KEY,
OPENAI_ORG_ID,
Expand Down
Loading

0 comments on commit 043a6e1

Please sign in to comment.