Skip to content

Commit

Permalink
Improve support for pulling structured prompts w/ model info (#1210)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw authored Nov 25, 2024
1 parent c3053e6 commit 758cdf6
Show file tree
Hide file tree
Showing 3 changed files with 742 additions and 3 deletions.
32 changes: 31 additions & 1 deletion python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5594,9 +5594,12 @@ def pull_prompt(
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 @@ -5645,6 +5648,33 @@ def suppress_langchain_beta_warning():
"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
3 changes: 2 additions & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langsmith"
version = "0.1.145"
version = "0.1.146"
description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform."
authors = ["LangChain <[email protected]>"]
license = "MIT"
Expand Down 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

0 comments on commit 758cdf6

Please sign in to comment.