mirror of
https://github.com/langchain-ai/docs.git
synced 2026-07-20 00:46:14 -04:00
docs: add Java snippets to manage prompts programmatically [closes DOC-1092] (#3961)
This commit is contained in:
@@ -147,9 +147,11 @@ For multiple files: `FILES="path1 path2"`. Fix any failures before proceeding—
|
||||
Java files (`.java`) under `src/code-samples/` are run using `jbang`. To keep CI green, Java samples must:
|
||||
|
||||
- Print at least one line of output so it's obvious the sample ran
|
||||
- Exit successfully (code 0) when required API keys are not set, for example:
|
||||
- Exit successfully (code 0) when optional API keys are not set, for example:
|
||||
- `OPENAI_API_KEY` for LLM calls
|
||||
- `LANGSMITH_API_KEY` for posting runs to LangSmith
|
||||
- Fail fast (non-zero exit) when a key is required for the sample to run, for example `manage-prompts-0-push.java` without `LANGSMITH_API_KEY`
|
||||
|
||||
`make test-code-samples` runs every `.java` file under `src/code-samples/` in **lexical path order** (after all Python and TypeScript samples). That order is unrelated to section order in the docs. If one sample must run before another (for example creating a hub prompt before pulling it), name the source files so they sort correctly. For example, `manage-prompts-pull.java` runs before `manage-prompts-push.java` because `pull` sorts before `push`; use prefixes such as `manage-prompts-0-push.java` and `manage-prompts-1-pull.java` when you need push to run first.
|
||||
|
||||
Check formatting with:
|
||||
|
||||
|
||||
@@ -116,6 +116,7 @@ jobs:
|
||||
id: test
|
||||
env:
|
||||
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
||||
LANGSMITH_API_KEY: ${{ secrets.LANGSMITH_API_KEY }}
|
||||
POSTGRES_URI: postgresql://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable
|
||||
TAVILY_API_KEY: ${{ secrets.TAVILY_API_KEY }}
|
||||
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
|
||||
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
///usr/bin/env jbang "$0" "$@" ; exit $?
|
||||
//DEPS com.langchain.smith:langsmith-java:0.1.0-beta.4
|
||||
|
||||
// :snippet-start: manage-prompts-push-java
|
||||
// :codegroup-tab: Java
|
||||
import com.langchain.smith.client.LangsmithClient;
|
||||
import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
|
||||
import com.langchain.smith.core.JsonValue;
|
||||
import com.langchain.smith.models.commits.CommitCreateParams;
|
||||
import com.langchain.smith.models.repos.RepoCreateParams;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
// :remove-start:
|
||||
class ManagePromptsPush {
|
||||
public static void main(String[] args) {
|
||||
if (System.getenv("LANGSMITH_API_KEY") == null
|
||||
|| System.getenv("LANGSMITH_API_KEY").isBlank()) {
|
||||
throw new IllegalStateException("LANGSMITH_API_KEY must be set to run this sample.");
|
||||
}
|
||||
try {
|
||||
// :remove-end:
|
||||
LangsmithClient client = LangsmithOkHttpClient.fromEnv();
|
||||
|
||||
// :remove-start:
|
||||
try {
|
||||
client.repos().delete(
|
||||
com.langchain.smith.models.repos.RepoDeleteParams.builder()
|
||||
.owner("-")
|
||||
.repo("joke-generator")
|
||||
.build()
|
||||
);
|
||||
} catch (Exception ignored) {
|
||||
// Prompt may not exist yet.
|
||||
}
|
||||
// :remove-end:
|
||||
|
||||
client.repos().create(
|
||||
RepoCreateParams.builder()
|
||||
.repoHandle("joke-generator")
|
||||
.isPublic(false)
|
||||
.build()
|
||||
);
|
||||
|
||||
Map<String, Object> manifest = Map.of(
|
||||
"lc", 1,
|
||||
"type", "constructor",
|
||||
"id", List.of("langchain_core", "prompts", "prompt", "PromptTemplate"),
|
||||
"kwargs", Map.of(
|
||||
"template", "tell me a joke about {topic}",
|
||||
"input_variables", List.of("topic")
|
||||
)
|
||||
);
|
||||
|
||||
client.commits().create(
|
||||
CommitCreateParams.builder()
|
||||
.owner("-")
|
||||
.repo("joke-generator")
|
||||
.manifest(JsonValue.from(manifest))
|
||||
.build()
|
||||
);
|
||||
// :remove-start:
|
||||
client.repos().delete(
|
||||
com.langchain.smith.models.repos.RepoDeleteParams.builder()
|
||||
.owner("-")
|
||||
.repo("joke-generator")
|
||||
.build()
|
||||
);
|
||||
System.out.println("[manage-prompts-push] Done.");
|
||||
} catch (Exception e) {
|
||||
System.out.println("[manage-prompts-push] Skipping (" + e.getMessage() + ").");
|
||||
}
|
||||
}
|
||||
}
|
||||
// :remove-end:
|
||||
// :snippet-end:
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
///usr/bin/env jbang "$0" "$@" ; exit $?
|
||||
//DEPS com.langchain.smith:langsmith-java:0.1.0-beta.4
|
||||
|
||||
// :snippet-start: manage-prompts-pull-java
|
||||
// :codegroup-tab: Java
|
||||
import com.langchain.smith.client.LangsmithClient;
|
||||
import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
|
||||
// :remove-start:
|
||||
import com.langchain.smith.core.JsonValue;
|
||||
import com.langchain.smith.models.commits.CommitCreateParams;
|
||||
import com.langchain.smith.models.commits.CommitWithLookups;
|
||||
import com.langchain.smith.models.repos.RepoCreateParams;
|
||||
import com.langchain.smith.models.repos.RepoDeleteParams;
|
||||
// :remove-end:
|
||||
import com.langchain.smith.prompts.Prompt;
|
||||
import com.langchain.smith.prompts.PromptClient;
|
||||
import com.langchain.smith.prompts.PromptValue;
|
||||
// :remove-start:
|
||||
import java.util.List;
|
||||
// :remove-end:
|
||||
import java.util.Map;
|
||||
|
||||
// :remove-start:
|
||||
class ManagePromptsPull {
|
||||
|
||||
/**
|
||||
* Ensures {@code joke-generator} exists with a single head commit (same manifest as the push
|
||||
* sample) so pulls below hit a known-good revision. Matches {@code manage-prompts-0-push.java}.
|
||||
*/
|
||||
private static String seedJokeGeneratorForPullSample(LangsmithClient client) {
|
||||
try {
|
||||
client.repos()
|
||||
.delete(
|
||||
RepoDeleteParams.builder().owner("-").repo("joke-generator").build());
|
||||
} catch (Exception ignored) {
|
||||
// Prompt may not exist yet.
|
||||
}
|
||||
client.repos()
|
||||
.create(
|
||||
RepoCreateParams.builder().repoHandle("joke-generator").isPublic(false).build());
|
||||
Map<String, Object> manifest =
|
||||
Map.of(
|
||||
"lc", 1,
|
||||
"type", "constructor",
|
||||
"id", List.of("langchain_core", "prompts", "prompt", "PromptTemplate"),
|
||||
"kwargs",
|
||||
Map.of(
|
||||
"template", "tell me a joke about {topic}",
|
||||
"input_variables", List.of("topic")));
|
||||
return client
|
||||
.commits()
|
||||
.create(
|
||||
CommitCreateParams.builder()
|
||||
.owner("-")
|
||||
.repo("joke-generator")
|
||||
.manifest(JsonValue.from(manifest))
|
||||
.build())
|
||||
.commit()
|
||||
.flatMap(CommitWithLookups::commitHash)
|
||||
.orElseThrow(() -> new IllegalStateException("commit hash missing from create response"));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (System.getenv("LANGSMITH_API_KEY") == null
|
||||
|| System.getenv("LANGSMITH_API_KEY").isBlank()) {
|
||||
System.out.println("[manage-prompts-pull] Skipping (LANGSMITH_API_KEY is not set).");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// :remove-end:
|
||||
LangsmithClient client = LangsmithOkHttpClient.fromEnv();
|
||||
PromptClient promptClient = PromptClient.create(client);
|
||||
// :remove-start:
|
||||
final String demoPinnedCommitHash = ManagePromptsPull.seedJokeGeneratorForPullSample(client);
|
||||
// :remove-end:
|
||||
|
||||
Prompt prompt = promptClient.pull("joke-generator");
|
||||
PromptValue formattedPrompt = prompt.invoke(Map.of("topic", "cats"));
|
||||
// Use formattedPrompt with your model provider — see "Use a prompt without LangChain" below.
|
||||
// :snippet-end:
|
||||
|
||||
// :snippet-start: manage-prompts-pull-commit-java
|
||||
// :codegroup-tab: Java
|
||||
String commitHash = "12344e88";
|
||||
// :remove-start:
|
||||
// Pin the commit created in seedJokeGeneratorForPullSample (see manage-prompts-0-push.java).
|
||||
commitHash = demoPinnedCommitHash;
|
||||
// :remove-end:
|
||||
Prompt promptAtCommit = promptClient.pull("joke-generator:" + commitHash);
|
||||
// :snippet-end:
|
||||
|
||||
// :snippet-start: manage-prompts-pull-public-java
|
||||
// :codegroup-tab: Java
|
||||
Prompt publicPrompt = promptClient.pull("efriis/my-first-prompt");
|
||||
// :remove-start:
|
||||
System.out.println("[manage-prompts-pull] Done.");
|
||||
} catch (Exception e) {
|
||||
System.out.println("[manage-prompts-pull] Skipping (" + e.getMessage() + ").");
|
||||
}
|
||||
}
|
||||
}
|
||||
// :remove-end:
|
||||
// :snippet-end:
|
||||
@@ -0,0 +1,53 @@
|
||||
///usr/bin/env jbang "$0" "$@" ; exit $?
|
||||
//DEPS com.langchain.smith:langsmith-java:0.1.0-beta.4
|
||||
//DEPS com.anthropic:anthropic-java:2.31.0
|
||||
|
||||
// :snippet-start: manage-prompts-anthropic-java
|
||||
// :codegroup-tab: Java
|
||||
import static com.langchain.smith.prompts.PromptConverters.convertToAnthropicParams;
|
||||
import com.anthropic.client.AnthropicClient;
|
||||
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
|
||||
import com.anthropic.models.messages.Message;
|
||||
import com.anthropic.models.messages.Model;
|
||||
import com.langchain.smith.client.LangsmithClient;
|
||||
import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
|
||||
import com.langchain.smith.prompts.Prompt;
|
||||
import com.langchain.smith.prompts.PromptClient;
|
||||
import com.langchain.smith.prompts.PromptValue;
|
||||
import java.util.Map;
|
||||
|
||||
// :remove-start:
|
||||
class ManagePromptsAnthropic {
|
||||
public static void main(String[] args) {
|
||||
if (System.getenv("LANGSMITH_API_KEY") == null
|
||||
|| System.getenv("LANGSMITH_API_KEY").isBlank()
|
||||
|| System.getenv("ANTHROPIC_API_KEY") == null
|
||||
|| System.getenv("ANTHROPIC_API_KEY").isBlank()) {
|
||||
System.out.println(
|
||||
"[manage-prompts-anthropic] Skipping (LANGSMITH_API_KEY and ANTHROPIC_API_KEY required).");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// :remove-end:
|
||||
LangsmithClient client = LangsmithOkHttpClient.fromEnv();
|
||||
PromptClient promptClient = PromptClient.create(client);
|
||||
AnthropicClient anthropic = AnthropicOkHttpClient.fromEnv();
|
||||
|
||||
Prompt prompt = promptClient.pull("jacob/joke-generator");
|
||||
PromptValue formattedPrompt = prompt.invoke(Map.of("topic", "cats"));
|
||||
|
||||
Message message = anthropic.messages().create(
|
||||
convertToAnthropicParams(formattedPrompt)
|
||||
.model(Model.CLAUDE_SONNET_4_5)
|
||||
.maxTokens(1024)
|
||||
.build()
|
||||
);
|
||||
// :remove-start:
|
||||
System.out.println("[manage-prompts-anthropic] Done.");
|
||||
} catch (Exception e) {
|
||||
System.out.println("[manage-prompts-anthropic] Skipping (" + e.getMessage() + ").");
|
||||
}
|
||||
}
|
||||
}
|
||||
// :remove-end:
|
||||
// :snippet-end:
|
||||
@@ -0,0 +1,55 @@
|
||||
///usr/bin/env jbang "$0" "$@" ; exit $?
|
||||
//DEPS com.langchain.smith:langsmith-java:0.1.0-beta.4
|
||||
|
||||
// :snippet-start: manage-prompts-list-delete-java
|
||||
// :codegroup-tab: Java
|
||||
import com.langchain.smith.client.LangsmithClient;
|
||||
import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
|
||||
import com.langchain.smith.models.repos.RepoDeleteParams;
|
||||
import com.langchain.smith.models.repos.RepoListPage;
|
||||
import com.langchain.smith.models.repos.RepoListParams;
|
||||
import com.langchain.smith.models.repos.RepoWithLookups;
|
||||
|
||||
// :remove-start:
|
||||
class ManagePromptsListDelete {
|
||||
public static void main(String[] args) {
|
||||
if (System.getenv("LANGSMITH_API_KEY") == null
|
||||
|| System.getenv("LANGSMITH_API_KEY").isBlank()) {
|
||||
System.out.println(
|
||||
"[manage-prompts-list-delete] Skipping (LANGSMITH_API_KEY is not set).");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// :remove-end:
|
||||
LangsmithClient client = LangsmithOkHttpClient.fromEnv();
|
||||
|
||||
// List all prompts in my workspace
|
||||
RepoListPage prompts = client.repos().list();
|
||||
for (RepoWithLookups prompt : prompts.repos()) {
|
||||
System.out.println(prompt.repoHandle());
|
||||
}
|
||||
|
||||
// List my private prompts that include "joke"
|
||||
RepoListPage jokePrompts = client.repos().list(
|
||||
RepoListParams.builder()
|
||||
.query("joke")
|
||||
.isPublic(RepoListParams.IsPublic.FALSE)
|
||||
.build()
|
||||
);
|
||||
|
||||
// Delete a prompt
|
||||
client.repos().delete(
|
||||
RepoDeleteParams.builder()
|
||||
.owner("-")
|
||||
.repo("joke-generator")
|
||||
.build()
|
||||
);
|
||||
// :remove-start:
|
||||
System.out.println("[manage-prompts-list-delete] Done.");
|
||||
} catch (Exception e) {
|
||||
System.out.println("[manage-prompts-list-delete] Skipping (" + e.getMessage() + ").");
|
||||
}
|
||||
}
|
||||
}
|
||||
// :remove-end:
|
||||
// :snippet-end:
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
///usr/bin/env jbang "$0" "$@" ; exit $?
|
||||
//DEPS com.langchain.smith:langsmith-java:0.1.0-beta.4
|
||||
//DEPS com.openai:openai-java:4.30.0
|
||||
|
||||
// :snippet-start: manage-prompts-openai-java
|
||||
// :codegroup-tab: Java
|
||||
import static com.langchain.smith.prompts.PromptConverters.convertToOpenAIParams;
|
||||
import com.langchain.smith.client.LangsmithClient;
|
||||
import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
|
||||
import com.langchain.smith.prompts.Prompt;
|
||||
import com.langchain.smith.prompts.PromptClient;
|
||||
import com.langchain.smith.prompts.PromptValue;
|
||||
import com.openai.client.OpenAIClient;
|
||||
import com.openai.client.okhttp.OpenAIOkHttpClient;
|
||||
import com.openai.models.ChatModel;
|
||||
import com.openai.models.chat.completions.ChatCompletion;
|
||||
import java.util.Map;
|
||||
|
||||
// :remove-start:
|
||||
class ManagePromptsOpenAI {
|
||||
public static void main(String[] args) {
|
||||
if (System.getenv("LANGSMITH_API_KEY") == null
|
||||
|| System.getenv("LANGSMITH_API_KEY").isBlank()
|
||||
|| System.getenv("OPENAI_API_KEY") == null
|
||||
|| System.getenv("OPENAI_API_KEY").isBlank()) {
|
||||
System.out.println(
|
||||
"[manage-prompts-openai] Skipping (LANGSMITH_API_KEY and OPENAI_API_KEY required).");
|
||||
return;
|
||||
}
|
||||
// :remove-end:
|
||||
LangsmithClient client = LangsmithOkHttpClient.fromEnv();
|
||||
PromptClient promptClient = PromptClient.create(client);
|
||||
OpenAIClient openai = OpenAIOkHttpClient.fromEnv();
|
||||
|
||||
Prompt prompt = promptClient.pull("jacob/joke-generator");
|
||||
PromptValue formattedPrompt = prompt.invoke(Map.of("topic", "cats"));
|
||||
|
||||
ChatCompletion completion = openai.chat().completions().create(
|
||||
convertToOpenAIParams(formattedPrompt)
|
||||
.model(ChatModel.GPT_4_1_MINI)
|
||||
.build()
|
||||
);
|
||||
// :remove-start:
|
||||
System.out.println("[manage-prompts-openai] Done.");
|
||||
}
|
||||
}
|
||||
// :remove-end:
|
||||
// :snippet-end:
|
||||
Generated
+9
@@ -249,6 +249,7 @@
|
||||
"resolved": "https://registry.npmjs.org/@aws-sdk/client-s3/-/client-s3-3.1032.0.tgz",
|
||||
"integrity": "sha512-A1wjVhV3IgsZ5td2l4AWgK03EjZ+ldwbiorxuO1hPf7RHJtSdr6oq/gKzyUwP7Tm7ma/M2xS/tplg5C8XB8RWg==",
|
||||
"license": "Apache-2.0",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@aws-crypto/sha1-browser": "5.2.0",
|
||||
"@aws-crypto/sha256-browser": "5.2.0",
|
||||
@@ -1501,6 +1502,7 @@
|
||||
"resolved": "https://registry.npmjs.org/@langchain/core/-/core-1.1.36.tgz",
|
||||
"integrity": "sha512-9NWsdzU3uZD13lJwunXK0t6SIwew+UwcbHggW5yUdaiMmzKeNkDpp1lRD6p49N8+D0Vv4qmQBEKB4Ukh2jfnvw==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@cfworker/json-schema": "^4.0.2",
|
||||
"@standard-schema/spec": "^1.1.0",
|
||||
@@ -1560,6 +1562,7 @@
|
||||
"resolved": "https://registry.npmjs.org/@langchain/langgraph-checkpoint/-/langgraph-checkpoint-1.0.0.tgz",
|
||||
"integrity": "sha512-xrclBGvNCXDmi0Nz28t3vjpxSH6UYx6w5XAXSiiB1WEdc2xD2iY/a913I3x3a31XpInUW/GGfXXfePfaghV54A==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"uuid": "^10.0.0"
|
||||
},
|
||||
@@ -1756,6 +1759,7 @@
|
||||
"resolved": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.9.1.tgz",
|
||||
"integrity": "sha512-gLyJlPHPZYdAk1JENA9LeHejZe1Ti77/pTeFm/nMXmQH/HFZlcS/O2XJB+L8fkbrNSqhdtlvjBVjxwUYanNH5Q==",
|
||||
"license": "Apache-2.0",
|
||||
"peer": true,
|
||||
"engines": {
|
||||
"node": ">=8.0.0"
|
||||
}
|
||||
@@ -3238,6 +3242,7 @@
|
||||
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.16.0.tgz",
|
||||
"integrity": "sha512-UVJyE9MttOsBQIDKw1skb9nAwQuR5wuGD3+82K6JgJlm/Y+KI92oNsMNGZCYdDsVtRHSak0pcV5Dno5+4jh9sw==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"bin": {
|
||||
"acorn": "bin/acorn"
|
||||
},
|
||||
@@ -3466,6 +3471,7 @@
|
||||
"resolved": "https://registry.npmjs.org/deepagents/-/deepagents-1.8.5.tgz",
|
||||
"integrity": "sha512-HgKryOCb00RNmwLJ0m6GekWkYb6mvRJrXGx23rkSzDG5cNpc5/LGuFYQ2DiBCBH0NGn/Rkv6xYRJjGPEKnM02A==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@langchain/core": "^1.1.33",
|
||||
"@langchain/langgraph": "^1.1.4",
|
||||
@@ -4070,6 +4076,7 @@
|
||||
"resolved": "https://registry.npmjs.org/langsmith/-/langsmith-0.5.20.tgz",
|
||||
"integrity": "sha512-ULhLM8RswvQDXufLtNtvclHrWCBx8Cb5UPI6lAZC+8Dq59iHsVPz/3Ac9khWNm1VIvChRsuykixD/WrmzuuA3Q==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"p-queue": "6.6.2",
|
||||
"uuid": "10.0.0"
|
||||
@@ -4326,6 +4333,7 @@
|
||||
"resolved": "https://registry.npmjs.org/pg/-/pg-8.20.0.tgz",
|
||||
"integrity": "sha512-ldhMxz2r8fl/6QkXnBD3CR9/xg694oT6DZQ2s6c/RI28OjtSOpxnPrUCGOBJ46RCUxcWdx3p6kw/xnDHjKvaRA==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"pg-connection-string": "^2.12.0",
|
||||
"pg-pool": "^3.13.0",
|
||||
@@ -4927,6 +4935,7 @@
|
||||
"resolved": "https://registry.npmjs.org/zod/-/zod-3.25.76.tgz",
|
||||
"integrity": "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/colinhacks"
|
||||
}
|
||||
|
||||
@@ -3,7 +3,15 @@ title: Manage prompts programmatically
|
||||
sidebarTitle: Manage prompts programmatically
|
||||
---
|
||||
|
||||
You can use the LangSmith Python and TypeScript SDK to manage prompts programmatically.
|
||||
import ManagePromptsPushJava from '/snippets/code-samples/manage-prompts-push-java.mdx';
|
||||
import ManagePromptsPullJava from '/snippets/code-samples/manage-prompts-pull-java.mdx';
|
||||
import ManagePromptsPullCommitJava from '/snippets/code-samples/manage-prompts-pull-commit-java.mdx';
|
||||
import ManagePromptsPullPublicJava from '/snippets/code-samples/manage-prompts-pull-public-java.mdx';
|
||||
import ManagePromptsOpenAIJava from '/snippets/code-samples/manage-prompts-openai-java.mdx';
|
||||
import ManagePromptsAnthropicJava from '/snippets/code-samples/manage-prompts-anthropic-java.mdx';
|
||||
import ManagePromptsListDeleteJava from '/snippets/code-samples/manage-prompts-list-delete-java.mdx';
|
||||
|
||||
You can use the LangSmith Python, TypeScript, and Java SDKs to manage prompts programmatically.
|
||||
|
||||
<Note>
|
||||
Previously this functionality lived in the `langchainhub` package which is now deprecated. All functionality going forward will live in the `langsmith` package.
|
||||
@@ -27,6 +35,10 @@ uv add langsmith # version >= 0.1.99
|
||||
```bash TypeScript
|
||||
yarn add langsmith langchain # langsmith version >= 0.1.99 and langchain version >= 0.2.14
|
||||
```
|
||||
|
||||
```kotlin Java/Kotlin (Gradle)
|
||||
implementation("com.langchain.smith:langsmith-java:0.1.0-beta.4")
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Configure environment variables
|
||||
@@ -84,16 +96,7 @@ const url = hub.push("joke-generator", {
|
||||
console.log(url);
|
||||
```
|
||||
|
||||
```java Java
|
||||
import com.langchain.smith.models.prompts.PromptPushParams;
|
||||
import com.langchain.smith.models.prompts.Prompt;
|
||||
|
||||
Prompt prompt = Prompt.builder()
|
||||
.name("joke-generator")
|
||||
.object(prompt)
|
||||
.build();
|
||||
var url = client.prompts().push(prompt);
|
||||
```
|
||||
<ManagePromptsPushJava />
|
||||
|
||||
</CodeGroup>
|
||||
|
||||
@@ -276,14 +279,7 @@ const chain = prompt.pipe(model);
|
||||
await chain.invoke({"topic": "cats"});
|
||||
```
|
||||
|
||||
```java Java
|
||||
RepoListPage jokePrompts = client.repos().list(
|
||||
RepoListParams.builder()
|
||||
.query("joke")
|
||||
.isPublic(RepoListParams.IsPublic.FALSE)
|
||||
.build()
|
||||
);
|
||||
```
|
||||
<ManagePromptsPullJava />
|
||||
|
||||
</CodeGroup>
|
||||
|
||||
@@ -332,6 +328,8 @@ prompt = prompts.pull("joke-generator:12344e88")
|
||||
const prompt = await hub.pull("joke-generator:12344e88")
|
||||
```
|
||||
|
||||
<ManagePromptsPullCommitJava />
|
||||
|
||||
</CodeGroup>
|
||||
|
||||
To pull a public prompt from the LangChain Hub, you need to specify the handle of the prompt's author.
|
||||
@@ -350,6 +348,8 @@ prompt = prompts.pull("efriis/my-first-prompt")
|
||||
const prompt = await hub.pull("efriis/my-first-prompt")
|
||||
```
|
||||
|
||||
<ManagePromptsPullPublicJava />
|
||||
|
||||
</CodeGroup>
|
||||
|
||||
<Note>
|
||||
@@ -735,6 +735,8 @@ const openAIResponse = await openAIClient.chat.completions.create({
|
||||
});
|
||||
```
|
||||
|
||||
<ManagePromptsOpenAIJava />
|
||||
|
||||
</CodeGroup>
|
||||
|
||||
### Anthropic
|
||||
@@ -790,6 +792,8 @@ const anthropicResponse = await anthropicClient.messages.create({
|
||||
});
|
||||
```
|
||||
|
||||
<ManagePromptsAnthropicJava />
|
||||
|
||||
</CodeGroup>
|
||||
|
||||
## List, delete, and like prompts
|
||||
@@ -839,33 +843,6 @@ client.likePrompt("efriis/my-first-prompt");
|
||||
client.unlikePrompt("efriis/my-first-prompt");
|
||||
```
|
||||
|
||||
```java Java
|
||||
// List all prompts in my workspace
|
||||
RepoListPage prompts = client.repos().list();
|
||||
for (RepoWithLookups prompt : prompts.repos()) {
|
||||
System.out.println(prompt.repoHandle());
|
||||
}
|
||||
|
||||
// List my private prompts that include "joke"
|
||||
RepoListPage jokePrompts = client.repos().list(
|
||||
RepoListParams.builder()
|
||||
.query("joke")
|
||||
.isPublic(RepoListParams.IsPublic.FALSE)
|
||||
.build()
|
||||
);
|
||||
|
||||
// Delete a prompt
|
||||
String promptId = "joke-generator";
|
||||
String[] parts = promptId.split("/", 2);
|
||||
String owner = parts.length > 1 ? parts[0] : "-";
|
||||
String repo = parts.length > 1 ? parts[1] : promptId;
|
||||
|
||||
client.repos().delete(
|
||||
RepoDeleteParams.builder()
|
||||
.owner(owner)
|
||||
.repo(repo)
|
||||
.build()
|
||||
);
|
||||
```
|
||||
<ManagePromptsListDeleteJava />
|
||||
|
||||
</CodeGroup>
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
```java Java
|
||||
import static com.langchain.smith.prompts.PromptConverters.convertToAnthropicParams;
|
||||
import com.anthropic.client.AnthropicClient;
|
||||
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
|
||||
import com.anthropic.models.messages.Message;
|
||||
import com.anthropic.models.messages.Model;
|
||||
import com.langchain.smith.client.LangsmithClient;
|
||||
import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
|
||||
import com.langchain.smith.prompts.Prompt;
|
||||
import com.langchain.smith.prompts.PromptClient;
|
||||
import com.langchain.smith.prompts.PromptValue;
|
||||
import java.util.Map;
|
||||
|
||||
LangsmithClient client = LangsmithOkHttpClient.fromEnv();
|
||||
PromptClient promptClient = PromptClient.create(client);
|
||||
AnthropicClient anthropic = AnthropicOkHttpClient.fromEnv();
|
||||
|
||||
Prompt prompt = promptClient.pull("jacob/joke-generator");
|
||||
PromptValue formattedPrompt = prompt.invoke(Map.of("topic", "cats"));
|
||||
|
||||
Message message = anthropic.messages().create(
|
||||
convertToAnthropicParams(formattedPrompt)
|
||||
.model(Model.CLAUDE_SONNET_4_5)
|
||||
.maxTokens(1024)
|
||||
.build()
|
||||
);
|
||||
```
|
||||
@@ -0,0 +1,32 @@
|
||||
```java Java
|
||||
import com.langchain.smith.client.LangsmithClient;
|
||||
import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
|
||||
import com.langchain.smith.models.repos.RepoDeleteParams;
|
||||
import com.langchain.smith.models.repos.RepoListPage;
|
||||
import com.langchain.smith.models.repos.RepoListParams;
|
||||
import com.langchain.smith.models.repos.RepoWithLookups;
|
||||
|
||||
LangsmithClient client = LangsmithOkHttpClient.fromEnv();
|
||||
|
||||
// List all prompts in my workspace
|
||||
RepoListPage prompts = client.repos().list();
|
||||
for (RepoWithLookups prompt : prompts.repos()) {
|
||||
System.out.println(prompt.repoHandle());
|
||||
}
|
||||
|
||||
// List my private prompts that include "joke"
|
||||
RepoListPage jokePrompts = client.repos().list(
|
||||
RepoListParams.builder()
|
||||
.query("joke")
|
||||
.isPublic(RepoListParams.IsPublic.FALSE)
|
||||
.build()
|
||||
);
|
||||
|
||||
// Delete a prompt
|
||||
client.repos().delete(
|
||||
RepoDeleteParams.builder()
|
||||
.owner("-")
|
||||
.repo("joke-generator")
|
||||
.build()
|
||||
);
|
||||
```
|
||||
@@ -0,0 +1,26 @@
|
||||
```java Java
|
||||
import static com.langchain.smith.prompts.PromptConverters.convertToOpenAIParams;
|
||||
import com.langchain.smith.client.LangsmithClient;
|
||||
import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
|
||||
import com.langchain.smith.prompts.Prompt;
|
||||
import com.langchain.smith.prompts.PromptClient;
|
||||
import com.langchain.smith.prompts.PromptValue;
|
||||
import com.openai.client.OpenAIClient;
|
||||
import com.openai.client.okhttp.OpenAIOkHttpClient;
|
||||
import com.openai.models.ChatModel;
|
||||
import com.openai.models.chat.completions.ChatCompletion;
|
||||
import java.util.Map;
|
||||
|
||||
LangsmithClient client = LangsmithOkHttpClient.fromEnv();
|
||||
PromptClient promptClient = PromptClient.create(client);
|
||||
OpenAIClient openai = OpenAIOkHttpClient.fromEnv();
|
||||
|
||||
Prompt prompt = promptClient.pull("jacob/joke-generator");
|
||||
PromptValue formattedPrompt = prompt.invoke(Map.of("topic", "cats"));
|
||||
|
||||
ChatCompletion completion = openai.chat().completions().create(
|
||||
convertToOpenAIParams(formattedPrompt)
|
||||
.model(ChatModel.GPT_4_1_MINI)
|
||||
.build()
|
||||
);
|
||||
```
|
||||
@@ -0,0 +1,3 @@
|
||||
```java Java
|
||||
Prompt promptAtCommit = promptClient.pull("joke-generator:12344e88");
|
||||
```
|
||||
@@ -0,0 +1,15 @@
|
||||
```java Java
|
||||
import com.langchain.smith.client.LangsmithClient;
|
||||
import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
|
||||
import com.langchain.smith.prompts.Prompt;
|
||||
import com.langchain.smith.prompts.PromptClient;
|
||||
import com.langchain.smith.prompts.PromptValue;
|
||||
import java.util.Map;
|
||||
|
||||
LangsmithClient client = LangsmithOkHttpClient.fromEnv();
|
||||
PromptClient promptClient = PromptClient.create(client);
|
||||
|
||||
Prompt prompt = promptClient.pull("joke-generator");
|
||||
PromptValue formattedPrompt = prompt.invoke(Map.of("topic", "cats"));
|
||||
// Use formattedPrompt with your model provider — see "Use a prompt without LangChain" below.
|
||||
```
|
||||
@@ -0,0 +1,3 @@
|
||||
```java Java
|
||||
Prompt publicPrompt = promptClient.pull("efriis/my-first-prompt");
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
```java Java
|
||||
import com.langchain.smith.client.LangsmithClient;
|
||||
import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
|
||||
import com.langchain.smith.core.JsonValue;
|
||||
import com.langchain.smith.models.commits.CommitCreateParams;
|
||||
import com.langchain.smith.models.repos.RepoCreateParams;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
LangsmithClient client = LangsmithOkHttpClient.fromEnv();
|
||||
|
||||
client.repos().create(
|
||||
RepoCreateParams.builder()
|
||||
.repoHandle("joke-generator")
|
||||
.isPublic(false)
|
||||
.build()
|
||||
);
|
||||
|
||||
Map<String, Object> manifest = Map.of(
|
||||
"lc", 1,
|
||||
"type", "constructor",
|
||||
"id", List.of("langchain_core", "prompts", "prompt", "PromptTemplate"),
|
||||
"kwargs", Map.of(
|
||||
"template", "tell me a joke about {topic}",
|
||||
"input_variables", List.of("topic")
|
||||
)
|
||||
);
|
||||
|
||||
client.commits().create(
|
||||
CommitCreateParams.builder()
|
||||
.owner("-")
|
||||
.repo("joke-generator")
|
||||
.manifest(JsonValue.from(manifest))
|
||||
.build()
|
||||
);
|
||||
```
|
||||
Reference in New Issue
Block a user