from langsmith import Clientfrom langchain_core.prompts import ChatPromptTemplateclient = Client()prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")url = client.push_prompt("joke-generator", object=prompt)# url is a link to the prompt in the UIprint(url)
from langsmith import Client# Obtain a reference to the global cache just for logging metricsfrom langsmith.prompt_cache import prompt_cache_singleton# Caching is enabled by default using the global singletonclient = Client()# First pull - fetches from API and cachesprompt = client.pull_prompt("joke-generator")# Subsequent pulls - returns cached version instantlyprompt = client.pull_prompt("joke-generator")# Check cache metricsprint(f"Cache hits: {prompt_cache_singleton.metrics.hits}")print(f"Cache misses: {prompt_cache_singleton.metrics.misses}")print(f"Hit rate: {prompt_cache_singleton.metrics.hit_rate:.1%}")
from langsmith import Clientfrom langsmith.prompt_cache import ( configure_global_prompt_cache, prompt_cache_singleton,)# Configure global cache before creating any clientsconfigure_global_prompt_cache( max_size=200, # Cache up to 200 prompts ttl_seconds=7200, # Consider prompts stale after 2 hours refresh_interval_seconds=600, # Check for stale prompts every 10 minutes)# All clients will use these settingsclient1 = Client()client2 = Client()# Both clients share the same global cache with your custom settingsprompt1 = client1.pull_prompt("prompt-1")prompt2 = client2.pull_prompt("prompt-2")# Check global cache metricsprint(f"Global cache hits: {prompt_cache_singleton.metrics.hits}")print(f"Global cache misses: {prompt_cache_singleton.metrics.misses}")
from langsmith import Client# Disable caching for this clientclient = Client(disable_prompt_cache=True)# Every pull will fetch from the APIprompt = client.pull_prompt("joke-generator")
from langsmith import Clientfrom langsmith.prompt_cache import prompt_cache_singleton# Create client (caching is enabled by default)client = Client()# Pull the prompts you needclient.pull_prompt("prompt-1")client.pull_prompt("prompt-2")client.pull_prompt("prompt-3")# Export cache to a fileprompt_cache_singleton.dump("prompts_cache.json")
步骤 2:在离线环境中加载缓存文件
from langsmith import Clientfrom langsmith.prompt_cache import ( configure_global_prompt_cache, prompt_cache_singleton,)# Configure cache with infinite TTL (never expire, no background refresh)configure_global_prompt_cache(ttl_seconds=None)# Load the cache fileprompt_cache_singleton.load("prompts_cache.json")# Create client (uses the loaded cache)client = Client()# Uses cached version without any API callsprompt = client.pull_prompt("prompt-1")
from langsmith import Clientfrom langsmith.prompt_cache import prompt_cache_singletonclient = Client()# Invalidate a specific prompt from cacheprompt_cache_singleton.invalidate("joke-generator:latest")# Clear all cached promptsprompt_cache_singleton.clear()# Reset metricsprompt_cache_singleton.reset_metrics()# Check if cache is running background refresh# (only runs if ttl_seconds is not None)if prompt_cache_singleton._refresh_thread is not None: print("Background refresh is active")
# List all prompts in my workspaceprompts = client.list_prompts()# List my private prompts that include "joke"prompts = client.list_prompts(query="joke", is_public=False)# Delete a promptclient.delete_prompt("joke-generator")# Like a promptclient.like_prompt("efriis/my-first-prompt")# Unlike a promptclient.unlike_prompt("efriis/my-first-prompt")