Skip to content

Commit

Permalink
Merge pull request #7 from facebookincubator/hunter/openai
Browse files Browse the repository at this point in the history
Get OpenAI working
  • Loading branch information
hunterjackson authored Sep 8, 2023
2 parents 67c9815 + fad18f0 commit af5bd56
Show file tree
Hide file tree
Showing 22 changed files with 1,367 additions and 137 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>com.knuddels</groupId>
<artifactId>jtokkit</artifactId>
<version>0.6.1</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/meta/chatbridge/Configuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.meta.chatbridge;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Configuration {
public static final ObjectMapper MAPPER =
new ObjectMapper()
.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.enable(DeserializationFeature.WRAP_EXCEPTIONS)
.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
.enable(DeserializationFeature.USE_LONG_FOR_INTS)
.disable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
.disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
}
6 changes: 6 additions & 0 deletions src/main/java/com/meta/chatbridge/Identifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Objects;
import java.util.UUID;
import org.jetbrains.annotations.NotNull;

public class Identifier implements Comparable<Identifier> {
Expand All @@ -21,6 +22,11 @@ private Identifier(byte[] id) {
this.id = id;
}

public static Identifier random() {
UUID uuid = UUID.randomUUID();
return new Identifier(uuid.toString().getBytes(StandardCharsets.UTF_8));
}

public static Identifier from(String id) {
return new Identifier(id.getBytes(StandardCharsets.UTF_8));
}
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/com/meta/chatbridge/Pipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

package com.meta.chatbridge;

import com.meta.chatbridge.llm.LLMHandler;
import com.meta.chatbridge.llm.LLMPlugin;
import com.meta.chatbridge.message.Message;
import com.meta.chatbridge.message.MessageHandler;
import com.meta.chatbridge.message.MessageStack;
import com.meta.chatbridge.store.ChatStore;
import com.meta.chatbridge.store.MessageStack;
import io.javalin.Javalin;
import io.javalin.http.Context;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.*;
Expand All @@ -27,15 +28,15 @@ public class Pipeline<T extends Message> {
private final ExecutorService executorService = Executors.newCachedThreadPool();
private final MessageHandler<T> handler;
private final ChatStore<T> store;
private final LLMHandler<T> llmHandler;
private final LLMPlugin<T> llmPlugin;

private final String path;

public Pipeline(
ChatStore<T> store, MessageHandler<T> handler, LLMHandler<T> llmHandler, String path) {
ChatStore<T> store, MessageHandler<T> handler, LLMPlugin<T> llmPlugin, String path) {
this.handler = Objects.requireNonNull(handler);
this.store = Objects.requireNonNull(store);
this.llmHandler = llmHandler;
this.llmPlugin = llmPlugin;
this.path = path;
}

Expand All @@ -61,12 +62,18 @@ public MessageHandler<T> messageHandler() {
}

private void execute(MessageStack<T> stack) {
T llmResponse = llmHandler.handle(stack);
T llmResponse;
try {
llmResponse = llmPlugin.handle(stack);
} catch (IOException e) {
LOGGER.error("failed to communicate with LLM", e);
return;
}
store.add(llmResponse);
try {
handler.respond(llmResponse);
} catch (Exception e) {
LOGGER.error("failed to respond to user", e);
// we log in the handler where we have the body context
// TODO: create transactional store add
// TODO: implement retry with exponential backoff
}
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/meta/chatbridge/PipelinesRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@ public static PipelinesRunner newInstance() {
return new PipelinesRunner();
}

@This
public PipelinesRunner start() {
public @This PipelinesRunner start() {
if (!started) {
started = true;
app.start(port);
}
return this;
}

@This
public PipelinesRunner pipeline(Pipeline<?> pipeline) {
public @This PipelinesRunner pipeline(Pipeline<?> pipeline) {
Preconditions.checkState(!started, "cannot add pipeline, server already started");
if (pipelines.add(pipeline)) {
pipeline.register(app);
Expand All @@ -64,8 +62,7 @@ public int port() {
* @param port the port the server will start on
* @return this
*/
@This
public PipelinesRunner port(int port) {
public @This PipelinesRunner port(int port) {
Preconditions.checkState(!started, "cannot change port, server already started");
this.port = port;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
package com.meta.chatbridge.llm;

import com.meta.chatbridge.message.Message;
import com.meta.chatbridge.store.MessageStack;
import com.meta.chatbridge.message.MessageStack;
import java.io.IOException;

public interface LLMHandler<T extends Message> {
public interface LLMPlugin<T extends Message> {

T handle(MessageStack<T> messageStack);
T handle(MessageStack<T> messageStack) throws IOException;
}
Loading

0 comments on commit af5bd56

Please sign in to comment.