Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build a bot for searching for the notion pages #237

Open
aditya-0508 opened this issue Sep 15, 2023 · 0 comments
Open

Build a bot for searching for the notion pages #237

aditya-0508 opened this issue Sep 15, 2023 · 0 comments

Comments

@aditya-0508
Copy link

package com.edgechain;

import static com.edgechain.lib.constants.EndpointConstants.OPENAI_CHAT_COMPLETION_API;
import java.util.concurrent.TimeUnit;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

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;
import com.edgechain.lib.jsonnet.impl.FileJsonnetLoader;
import com.edgechain.lib.request.ArkRequest;
import com.edgechain.lib.rxjava.retry.impl.ExponentialDelay;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

@SpringBootApplication
public class CodeInterpreter {

private static final String OPENAI_AUTH_KEY = "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";
private static OpenAiEndpoint userChatEndpoint;
private static final ObjectMapper objectMapper = new ObjectMapper();
private static JsonnetLoader loader = new FileJsonnetLoader("./code-interpreter.jsonnet");
private static final String NOTION_API_KEY = "secret_uzMQSTHsBPIgn2v7ZjWltfIzurkfCbCqwhlp0ta8Y7M";

public static void main(String[] args) {
System.setProperty("server.port", "8080");
new SpringApplicationBuilder(CodeInterpreter.class).run(args);
}

private static JSONObject getPage(String pageId) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(NOTION_API_KEY);
headers.set("Notion-Version", "2022-06-28"); // Set the desired API version here
HttpEntity entity = new HttpEntity<>("parameters", headers);
ResponseEntity response = restTemplate.exchange(
"https://api.notion.com/v1/pages/" + pageId,
HttpMethod.GET,
entity,
String.class);
return new JSONObject(response.getBody());
}

@RestController
@RequestMapping("/v1/examples")
public class Interpreter {

@PostMapping("/code-interpreter")
public double interpret(ArkRequest arkRequest) throws JSONException {

JSONObject json = arkRequest.getBody();

// Retrieve page from notion
//String pageId = json.getString("pageId");
String pageId = "your_generated_uuid";
JSONObject pageContent = getPage(pageId);

// Add the page content to the prompt
loader.put("prompt", new JsonnetArgs(DataType.STRING, pageContent.toString())).loadOrReload();
String prompt = loader.get("extract");

userChatEndpoint =
    new OpenAiEndpoint(
        OPENAI_CHAT_COMPLETION_API,
        OPENAI_AUTH_KEY,
        "gpt-3.5-turbo",
        "user",
        0.7,
        new ExponentialDelay(3, 5, 2, TimeUnit.SECONDS));

while (true) {

    String response =
        userChatEndpoint
            .chatCompletion(prompt, "code-interpreter", arkRequest)
            .blockingFirst()
            .getChoices()
            .get(0)
            .getMessage()
            .getContent();

    try {
      JsonNode jsonNode = objectMapper.readTree(response);
      System.out.println("Valid JSON from chatgpt " + jsonNode);

      String tool = jsonNode.get("action").get("tool").asText();
      String arg = jsonNode.get("action").get("arg").asText();

      if ("CodeInterpreter".equals(tool)) {
        Double val = Eval.evaluateExpression(arg);
        System.out.println("CodeInterpreter: " + val);

        prompt +=
            String.format("\n%s\nObservation: CodeInterpreter returned %s", response, val);
        } else if ("Finish".equals(tool)) {
            System.out.println("FINAL ANSWER: " + arg);
            return Double.parseDouble(arg);
        }
    } catch (JsonProcessingException e) {
        System.out.println("Invalid JSON from chatgpt " + response);
        e.printStackTrace();
    }
}

}
}
}
Screenshot (69)

The code is running but giving me error during sending the request in postman .

@sandys sandys transferred this issue from arakoodev/doc Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant