Skip to content

Commit

Permalink
Merge pull request #31 from langchain-ai/ankush/stop-pass-nulls
Browse files Browse the repository at this point in the history
Update builder patterns and don't pass `null`s
  • Loading branch information
agola11 committed Mar 21, 2024
2 parents 78c00fc + a6af50c commit 29f6bb2
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 209 deletions.
Binary file modified .gradle/8.0.2/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/8.0.2/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/8.0.2/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/8.0.2/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/8.0.2/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
64 changes: 2 additions & 62 deletions README.md
Expand Up @@ -74,68 +74,8 @@ If unspecified, runs will be posted to the `default` project.

### Example: using the RunTree API (experimental)
The RunTree API is currently the recommended way to post runs to LangSmith.
```java
package com.langsmith.example;

import com.langsmith.runtree.RunTree;
import com.langsmith.runtree.RunTreeConfigBuilder;
import com.theokanning.openai.service.OpenAiService;
import com.theokanning.openai.completion.chat.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class OpenAiExample {
public static void main(String[] args) {
String apiKey = System.getenv("OPENAI_API_KEY");
OpenAiService service = new OpenAiService(apiKey);

String question = "Can you summarize this morning's meetings?";

// Create a top-level run
RunTreeConfigBuilder pipelineConfigBuilder = new RunTreeConfigBuilder()
.setName("Chat Pipeline")
.setRunType("chain")
.setInputs(Collections.singletonMap("question", question));

RunTree pipeline = new RunTree(pipelineConfigBuilder.build());

var context = "During this morning's meeting, we solved all world conflict.";

List<ChatMessage> messages = new ArrayList<>();
var systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), "You are a helpful assistant. Please respond to the user's request only based on the given context.");
messages.add(systemMessage);
var userMessage = new ChatMessage(ChatMessageRole.USER.value(), "Question: " + question + "\nContext: " + context);
messages.add(userMessage);

var completionRequest = ChatCompletionRequest
.builder()
.model("gpt-3.5-turbo")
.messages(messages)
.build();

// Create a child run
RunTreeConfigBuilder childConfigBuilder = new RunTreeConfigBuilder()
.setName("OpenAI Call")
.setRunType("llm")
.setInputs(Collections.singletonMap("messages", messages))
.setParentRun(pipeline);

RunTree childTree = pipeline.createChild(childConfigBuilder.build());

ChatMessage responseMessage = service.createChatCompletion(completionRequest).getChoices().get(0).getMessage();
System.out.println("Response: " + responseMessage.getContent());

// End the child run
childTree.end(Collections.singletonMap("response", responseMessage), null, null);

// End the parent run
pipeline.end(Collections.singletonMap("response", responseMessage.getContent()), null, null);
childTree.postRunAsync().join();
pipeline.postRunAsync().join();
}
}
```

See a full example in the [here](./examples/src/main/java/com/langsmith/example/OpenAiExample.java).

### Example: creating a resource

Expand Down
Binary file modified buildSrc/.gradle/8.0.2/executionHistory/executionHistory.lock
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/build.gradle.kts
Expand Up @@ -32,7 +32,7 @@ tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
}

// Dynamically create a task for each example
val examples = listOf("RunTreeExample", "OpenAiExample") // Add the names of your example classes here
val examples = listOf("OpenAiExample") // Add the names of your example classes here

examples.forEach { example ->
tasks.create("run$example", org.gradle.api.tasks.JavaExec::class.java) {
Expand Down
30 changes: 21 additions & 9 deletions examples/src/main/java/com/langsmith/example/OpenAiExample.java
@@ -1,9 +1,11 @@
package com.langsmith.example;

import com.langsmith.runtree.EndOptions;
import com.langsmith.runtree.RunTree;
import com.langsmith.runtree.RunTreeConfigBuilder;
import com.langsmith.runtree.RunTreeConfig;
import com.theokanning.openai.service.OpenAiService;
import com.theokanning.openai.completion.chat.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -16,12 +18,14 @@ public static void main(String[] args) {
String question = "Can you summarize this morning's meetings?";

// Create a top-level run
RunTreeConfigBuilder pipelineConfigBuilder = new RunTreeConfigBuilder()
RunTreeConfig pipelineConfig = RunTreeConfig
.builder()
.setName("Chat Pipeline")
.setRunType("chain")
.setInputs(Collections.singletonMap("question", question));
.setInputs(Collections.singletonMap("question", question))
.build();

RunTree pipeline = new RunTree(pipelineConfigBuilder.build());
RunTree pipeline = new RunTree(pipelineConfig);

var context = "During this morning's meeting, we solved all world conflict.";

Expand All @@ -38,22 +42,30 @@ public static void main(String[] args) {
.build();

// Create a child run
RunTreeConfigBuilder childConfigBuilder = new RunTreeConfigBuilder()
RunTreeConfig childConfig = RunTreeConfig
.builder()
.setName("OpenAI Call")
.setRunType("llm")
.setInputs(Collections.singletonMap("messages", messages))
.setParentRun(pipeline);
.setParentRun(pipeline)
.build();

RunTree childTree = pipeline.createChild(childConfigBuilder.build());
RunTree childTree = pipeline.createChild(childConfig);

ChatMessage responseMessage = service.createChatCompletion(completionRequest).getChoices().get(0).getMessage();
System.out.println("Response: " + responseMessage.getContent());

// End the child run
childTree.end(Collections.singletonMap("response", responseMessage), null, null);
childTree.end(EndOptions
.builder()
.setOutputs(Collections.singletonMap("response", responseMessage))
.build());

// End the parent run
pipeline.end(Collections.singletonMap("response", responseMessage.getContent()), null, null);
pipeline.end(EndOptions
.builder()
.setOutputs(Collections.singletonMap("response", responseMessage.getContent()))
.build());
childTree.postRunAsync().join();
pipeline.postRunAsync().join();
}
Expand Down
69 changes: 0 additions & 69 deletions examples/src/main/java/com/langsmith/example/RunTreeExample.java

This file was deleted.

0 comments on commit 29f6bb2

Please sign in to comment.