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

Improve support for pulling structured prompts w/ model info #1210

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion python/langsmith/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Client for interacting with the LangSmith API.

Check notice on line 1 in python/langsmith/client.py

View workflow job for this annotation

GitHub Actions / benchmark

Benchmark results

......................................... create_5_000_run_trees: Mean +- std dev: 623 ms +- 51 ms ......................................... create_10_000_run_trees: Mean +- std dev: 1.21 sec +- 0.06 sec ......................................... create_20_000_run_trees: Mean +- std dev: 1.20 sec +- 0.06 sec ......................................... dumps_class_nested_py_branch_and_leaf_200x400: Mean +- std dev: 706 us +- 12 us ......................................... dumps_class_nested_py_leaf_50x100: Mean +- std dev: 25.1 ms +- 0.3 ms ......................................... dumps_class_nested_py_leaf_100x200: Mean +- std dev: 104 ms +- 2 ms ......................................... dumps_dataclass_nested_50x100: Mean +- std dev: 25.7 ms +- 0.3 ms ......................................... WARNING: the benchmark result may be unstable * the standard deviation (16.7 ms) is 25% of the mean (67.1 ms) Try to rerun the benchmark with more runs, values and/or loops. Run 'python -m pyperf system tune' command to reduce the system jitter. Use pyperf stats, pyperf dump and pyperf hist to analyze results. Use --quiet option to hide these warnings. dumps_pydantic_nested_50x100: Mean +- std dev: 67.1 ms +- 16.7 ms ......................................... WARNING: the benchmark result may be unstable * the standard deviation (30.4 ms) is 14% of the mean (218 ms) Try to rerun the benchmark with more runs, values and/or loops. Run 'python -m pyperf system tune' command to reduce the system jitter. Use pyperf stats, pyperf dump and pyperf hist to analyze results. Use --quiet option to hide these warnings. dumps_pydanticv1_nested_50x100: Mean +- std dev: 218 ms +- 30 ms

Check notice on line 1 in python/langsmith/client.py

View workflow job for this annotation

GitHub Actions / benchmark

Comparison against main

+-------------------------------+----------+------------------------+ | Benchmark | main | changes | +===============================+==========+========================+ | dumps_dataclass_nested_50x100 | 25.4 ms | 25.7 ms: 1.01x slower | +-------------------------------+----------+------------------------+ | create_20_000_run_trees | 1.18 sec | 1.20 sec: 1.02x slower | +-------------------------------+----------+------------------------+ | create_10_000_run_trees | 1.19 sec | 1.21 sec: 1.02x slower | +-------------------------------+----------+------------------------+ | create_5_000_run_trees | 611 ms | 623 ms: 1.02x slower | +-------------------------------+----------+------------------------+ | Geometric mean | (ref) | 1.01x slower | +-------------------------------+----------+------------------------+ Benchmark hidden because not significant (5): dumps_class_nested_py_leaf_100x200, dumps_pydanticv1_nested_50x100, dumps_class_nested_py_leaf_50x100, dumps_class_nested_py_branch_and_leaf_200x400, dumps_pydantic_nested_50x100

Use the client to customize API keys / workspace ocnnections, SSl certs,
etc. for tracing.
Expand Down Expand Up @@ -5551,9 +5551,12 @@
Any: The prompt object in the specified format.
"""
try:
from langchain_core.language_models.base import BaseLanguageModel
from langchain_core.load.load import loads
from langchain_core.output_parsers import BaseOutputParser
from langchain_core.prompts import BasePromptTemplate
from langchain_core.runnables.base import RunnableSequence
from langchain_core.prompts.structured import StructuredPrompt
from langchain_core.runnables.base import RunnableBinding, RunnableSequence
except ImportError:
raise ImportError(
"The client.pull_prompt function requires the langchain_core"
Expand Down Expand Up @@ -5602,6 +5605,33 @@
"lc_hub_commit_hash": prompt_object.commit_hash,
}
)
if (
include_model
and isinstance(prompt, RunnableSequence)
and isinstance(prompt.first, StructuredPrompt)
# Make forward-compatible in case we let update the response type
and (
len(prompt.steps) == 2 and not isinstance(prompt.last, BaseOutputParser)
)
):
if isinstance(prompt.last, RunnableBinding) and isinstance(
prompt.last.bound, BaseLanguageModel
):
seq = cast(RunnableSequence, prompt.first | prompt.last.bound)
if len(seq.steps) == 3: # prompt | bound llm | output parser
rebound_llm = seq.steps[1]
prompt = RunnableSequence(
prompt.first,
rebound_llm.bind(**{**prompt.last.kwargs}),
seq.last,
)
else:
prompt = seq # Not sure

elif isinstance(prompt.last, BaseLanguageModel):
prompt: RunnableSequence = prompt.first | prompt.last # type: ignore[no-redef, assignment]
else:
pass

return prompt

Expand Down
1 change: 1 addition & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ convention = "google"
"langsmith/client.py" = ["E501"]
"langsmith/schemas.py" = ["E501"]
"tests/evaluation/__init__.py" = ["E501"]
"tests/unit_tests/test_client.py" = ["E501"]
"tests/*" = ["D", "UP"]
"bench/*" = ["D", "UP", "T"]
"docs/*" = ["T", "D"]
Expand Down
Loading
Loading