@ -139,6 +139,7 @@ async def _arun_llm(
* ,
tags : Optional [ List [ str ] ] = None ,
callbacks : Callbacks = None ,
input_mapper : Optional [ Callable [ [ Dict ] , Any ] ] = None ,
) - > Union [ LLMResult , ChatResult ] :
"""
Asynchronously run the language model .
@ -148,6 +149,7 @@ async def _arun_llm(
inputs : The input dictionary .
tags : Optional tags to add to the run .
callbacks : Optional callbacks to use during the run .
input_mapper : Optional function to map inputs to the expected format .
Returns :
The LLMResult or ChatResult .
@ -155,7 +157,13 @@ async def _arun_llm(
ValueError : If the LLM type is unsupported .
InputFormatError : If the input format is invalid .
"""
if isinstance ( llm , BaseLLM ) :
if input_mapper is not None :
if not isinstance ( llm , ( BaseLLM , BaseChatModel ) ) :
raise ValueError ( f " Unsupported LLM type { type ( llm ) . __name__ } " )
llm_output = await llm . agenerate (
input_mapper ( inputs ) , callbacks = callbacks , tags = tags
)
elif isinstance ( llm , BaseLLM ) :
try :
llm_prompts = _get_prompts ( inputs )
llm_output = await llm . agenerate (
@ -191,6 +199,7 @@ async def _arun_llm_or_chain(
* ,
tags : Optional [ List [ str ] ] = None ,
callbacks : Optional [ List [ BaseCallbackHandler ] ] = None ,
input_mapper : Optional [ Callable [ [ Dict ] , Any ] ] = None ,
) - > Union [ List [ dict ] , List [ str ] , List [ LLMResult ] , List [ ChatResult ] ] :
"""
Asynchronously run the Chain or language model .
@ -201,6 +210,7 @@ async def _arun_llm_or_chain(
n_repetitions : The number of times to run the model on each example .
tags : Optional tags to add to the run .
callbacks : Optional callbacks to use during the run .
input_mapper : Optional function to map the input to the expected format .
Returns :
A list of outputs .
@ -223,9 +233,13 @@ async def _arun_llm_or_chain(
example . inputs ,
tags = tags ,
callbacks = callbacks ,
input_mapper = input_mapper ,
)
else :
chain = llm_or_chain_factory ( )
if input_mapper is not None :
inputs_ = input_mapper ( example . inputs )
else :
inputs_ = example . inputs
if len ( inputs_ ) == 1 :
inputs_ = next ( iter ( inputs_ . values ( ) ) )
@ -333,6 +347,7 @@ async def arun_on_examples(
client : Optional [ LangChainPlusClient ] = None ,
tags : Optional [ List [ str ] ] = None ,
run_evaluators : Optional [ Sequence [ RunEvaluator ] ] = None ,
input_mapper : Optional [ Callable [ [ Dict ] , Any ] ] = None ,
) - > Dict [ str , Any ] :
"""
Asynchronously run the chain on examples and store traces
@ -354,6 +369,11 @@ async def arun_on_examples(
client will be created using the credentials in the environment .
tags : Tags to add to each run in the project .
run_evaluators : Evaluators to run on the results of the chain .
input_mapper : function to map to the inputs dictionary from an Example
to the format expected by the model to be evaluated . This is useful if
your model needs to deserialize more complex schema or if your dataset
has inputs with keys that differ from what is expected by your chain
or agent .
Returns :
A dictionary mapping example ids to the model outputs .
@ -377,6 +397,7 @@ async def arun_on_examples(
num_repetitions ,
tags = tags ,
callbacks = callbacks ,
input_mapper = input_mapper ,
)
results [ str ( example . id ) ] = result
job_state [ " num_processed " ] + = 1
@ -407,6 +428,7 @@ def run_llm(
callbacks : Callbacks ,
* ,
tags : Optional [ List [ str ] ] = None ,
input_mapper : Optional [ Callable [ [ Dict ] , Any ] ] = None ,
) - > Union [ LLMResult , ChatResult ] :
"""
Run the language model on the example .
@ -416,14 +438,18 @@ def run_llm(
inputs : The input dictionary .
callbacks : The callbacks to use during the run .
tags : Optional tags to add to the run .
input_mapper : function to map to the inputs dictionary from an Example
Returns :
The LLMResult or ChatResult .
Raises :
ValueError : If the LLM type is unsupported .
InputFormatError : If the input format is invalid .
"""
if isinstance ( llm , BaseLLM ) :
if input_mapper is not None :
if not isinstance ( llm , ( BaseLLM , BaseChatModel ) ) :
raise ValueError ( f " Unsupported LLM type { type ( llm ) . __name__ } " )
llm_output = llm . generate ( input_mapper ( inputs ) , callbacks = callbacks , tags = tags )
elif isinstance ( llm , BaseLLM ) :
try :
llm_prompts = _get_prompts ( inputs )
llm_output = llm . generate ( llm_prompts , callbacks = callbacks , tags = tags )
@ -455,6 +481,7 @@ def run_llm_or_chain(
* ,
tags : Optional [ List [ str ] ] = None ,
callbacks : Optional [ List [ BaseCallbackHandler ] ] = None ,
input_mapper : Optional [ Callable [ [ Dict ] , Any ] ] = None ,
) - > Union [ List [ dict ] , List [ str ] , List [ LLMResult ] , List [ ChatResult ] ] :
"""
Run the Chain or language model synchronously .
@ -483,10 +510,17 @@ def run_llm_or_chain(
try :
if isinstance ( llm_or_chain_factory , BaseLanguageModel ) :
output : Any = run_llm (
llm_or_chain_factory , example . inputs , callbacks , tags = tags
llm_or_chain_factory ,
example . inputs ,
callbacks ,
tags = tags ,
input_mapper = input_mapper ,
)
else :
chain = llm_or_chain_factory ( )
if input_mapper is not None :
inputs_ = input_mapper ( example . inputs )
else :
inputs_ = example . inputs
if len ( inputs_ ) == 1 :
inputs_ = next ( iter ( inputs_ . values ( ) ) )
@ -512,6 +546,7 @@ def run_on_examples(
client : Optional [ LangChainPlusClient ] = None ,
tags : Optional [ List [ str ] ] = None ,
run_evaluators : Optional [ Sequence [ RunEvaluator ] ] = None ,
input_mapper : Optional [ Callable [ [ Dict ] , Any ] ] = None ,
) - > Dict [ str , Any ] :
"""
Run the Chain or language model on examples and store
@ -532,6 +567,11 @@ def run_on_examples(
will be created using the credentials in the environment .
tags : Tags to add to each run in the project .
run_evaluators : Evaluators to run on the results of the chain .
input_mapper : A function to map to the inputs dictionary from an Example
to the format expected by the model to be evaluated . This is useful if
your model needs to deserialize more complex schema or if your dataset
has inputs with keys that differ from what is expected by your chain
or agent .
Returns :
A dictionary mapping example ids to the model outputs .
@ -552,6 +592,7 @@ def run_on_examples(
num_repetitions ,
tags = tags ,
callbacks = callbacks ,
input_mapper = input_mapper ,
)
if verbose :
print ( f " { i + 1 } processed " , flush = True , end = " \r " )
@ -599,6 +640,7 @@ async def arun_on_dataset(
client : Optional [ LangChainPlusClient ] = None ,
tags : Optional [ List [ str ] ] = None ,
run_evaluators : Optional [ Sequence [ RunEvaluator ] ] = None ,
input_mapper : Optional [ Callable [ [ Dict ] , Any ] ] = None ,
) - > Dict [ str , Any ] :
"""
Asynchronously run the Chain or language model on a dataset
@ -620,7 +662,11 @@ async def arun_on_dataset(
client will be created using the credentials in the environment .
tags : Tags to add to each run in the project .
run_evaluators : Evaluators to run on the results of the chain .
input_mapper : A function to map to the inputs dictionary from an Example
to the format expected by the model to be evaluated . This is useful if
your model needs to deserialize more complex schema or if your dataset
has inputs with keys that differ from what is expected by your chain
or agent .
Returns :
A dictionary containing the run ' s project name and the resulting model outputs.
"""
@ -638,6 +684,7 @@ async def arun_on_dataset(
client = client_ ,
tags = tags ,
run_evaluators = run_evaluators ,
input_mapper = input_mapper ,
)
return {
" project_name " : project_name ,
@ -655,6 +702,7 @@ def run_on_dataset(
client : Optional [ LangChainPlusClient ] = None ,
tags : Optional [ List [ str ] ] = None ,
run_evaluators : Optional [ Sequence [ RunEvaluator ] ] = None ,
input_mapper : Optional [ Callable [ [ Dict ] , Any ] ] = None ,
) - > Dict [ str , Any ] :
"""
Run the Chain or language model on a dataset and store traces
@ -676,6 +724,11 @@ def run_on_dataset(
will be created using the credentials in the environment .
tags : Tags to add to each run in the project .
run_evaluators : Evaluators to run on the results of the chain .
input_mapper : A function to map to the inputs dictionary from an Example
to the format expected by the model to be evaluated . This is useful if
your model needs to deserialize more complex schema or if your dataset
has inputs with keys that differ from what is expected by your chain
or agent .
Returns :
A dictionary containing the run ' s project name and the resulting model outputs.
@ -693,6 +746,7 @@ def run_on_dataset(
tags = tags ,
run_evaluators = run_evaluators ,
client = client_ ,
input_mapper = input_mapper ,
)
return {
" project_name " : project_name ,