How to run a parallel chain with different config for using chat history #25533
Replies: 1 comment 12 replies
-
To run parallel chains with different configurations for each chain in the
Here's the updated code: def _demo_parallel():
config = GPTConfig(character_id="default", stream=False, tokenizer=None,
keep_dialog=None, model='gpt-4o-mini', temperature=0.8, max_tokens_output=None,
max_tokens_context=30000, api_key_path='./settings/config.json')
lc_module = LangChainModule(config)
# process_chain(self, llm, instructions, prompts, input_dict, output_dict=None, with_history=None):
# chain 1
input_dict = {"korean_word1": "양념", "korean_word2": "진득하다"}
config1 = {"configurable": {"user_id": "123", "conversation_id": "1"}}
instructions = "output should be json format and the keys are english_word1, english_word2"
prompts = "explain {korean_word1} and {korean_word2} using oxford dictionary to me in English."
output_dict = {"english_word1": "explain the first word", "english_word2": "explain the second word"}
main_chain = lc_module.process_chain(instructions=instructions, prompts=prompts,
input_dict=input_dict, output_dict=output_dict)
# chain 2
input_dict2 = {"korean_word3": "뭉실뭉실", "korean_word4": "개미허리"}
config2 = {"configurable": {"user_id": "33", "conversation_id": "1"}}
instructions2 = "output should be json format and the keys are english_word1, english_word2"
prompts2 = "explain {korean_word3} and {korean_word4} using oxford dictionary to me in English."
output_dict2 = {"english_word1": "explain the first word", "english_word2": "explain the second word"}
chain2 = lc_module.process_chain(instructions=instructions2, prompts=prompts2,
input_dict=input_dict2, output_dict=output_dict2)
# Combine both chains to run in parallel
map_chain = RunnableParallel(chain1=main_chain, chain2=chain2)
# Invoke the parallel chains with the input and their respective configurations
result = map_chain.invoke({
"chain1": {"input": input_dict, "config": config1},
"chain2": {"input": input_dict2, "config": config2}
})
print(result)
# test
if __name__ == "__main__":
_demo_parallel() In this example:
This approach ensures that each chain runs with its own configuration in parallel [1]. |
Beta Was this translation helpful? Give feedback.
12 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
in _demo_parallel function, I want to set config for two chains but individually.
So, [main_chain] should use [config] while [chain2] uses [config2]
How to do it? Please let me know.
Or please tell me better solution to run parallel chains with different parameters.
System Info
langchain==0.2.9
langchain-community==0.2.9
langchain-core==0.2.22
langchain-openai==0.1.10
langchain-text-splitters==0.2.2
langchainhub==0.1.21
linux(ubuntu)
python 3.9
Beta Was this translation helpful? Give feedback.
All reactions