Files
litellm/docs/extras/modules/chains/how_to/call_methods.ipynb
T
2023-07-29 07:12:19 -07:00

4.2 KiB

Different call methods

All classes inherited from Chain offer a few ways of running chain logic. The most direct one is by using __call__:

In [5]:
chat = ChatOpenAI(temperature=0)
prompt_template = "Tell me a {adjective} joke"
llm_chain = LLMChain(llm=chat, prompt=PromptTemplate.from_template(prompt_template))

llm_chain(inputs={"adjective": "corny"})
Out [5]:
{'adjective': 'corny',
 'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}

By default, __call__ returns both the input and output key values. You can configure it to only return output key values by setting return_only_outputs to True.

In [6]:
llm_chain("corny", return_only_outputs=True)
Out [6]:
{'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}

If the Chain only outputs one output key (i.e. only has one element in its output_keys), you can use run method. Note that run outputs a string instead of a dictionary.

In [7]:
# llm_chain only has one output key, so we can use run
llm_chain.output_keys
Out [7]:
['text']
In [8]:
llm_chain.run({"adjective": "corny"})
Out [8]:
'Why did the tomato turn red? Because it saw the salad dressing!'

In the case of one input key, you can input the string directly without specifying the input mapping.

In [9]:
# These two are equivalent
llm_chain.run({"adjective": "corny"})
llm_chain.run("corny")

# These two are also equivalent
llm_chain("corny")
llm_chain({"adjective": "corny"})
Out [9]:
{'adjective': 'corny',
 'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}

Tips: You can easily integrate a Chain object as a Tool in your Agent via its run method. See an example here.

In [ ]: