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

Python: Passing output of step to next in SequentialChain #1476

Closed
gopitk opened this issue Jun 14, 2023 · 5 comments
Closed

Python: Passing output of step to next in SequentialChain #1476

gopitk opened this issue Jun 14, 2023 · 5 comments
Assignees
Labels
planner Anything related to planner or plans python Pull requests for the Python Semantic Kernel

Comments

@gopitk
Copy link

gopitk commented Jun 14, 2023

I want to rewrite a Langchain SimpleSequentialChain (in Python) where I am looking at a chain of semantic functions and call them sequentially. The output of the 1st function needs to be passed as input context in the second function.

How do I do this? Here is my code snippet (tried to use the $input as well as some other name for the context vars in the prompts below):

# prompt template 1
first_prompt = "What is the best name to describe \
    a company that makes {{ $product }}?"

# Semantic Function 1
func_one = kernel.create_semantic_function(first_prompt, temperature=0.9)

# prompt template 2
second_prompt = "Write a 20 words description for the following \
    company:{{ $company }}"

# Semantic Function 2
func_two = kernel.create_semantic_function(second_prompt, temperature=0.9)

# Now call functions in order
answer = await kernel.run_async(func_one, func_two, input_str="Queen Size Sheet Set")
print(answer)

The code is working and getting some output but likely not passing output of 1st function to the second. Also what is the procedure to debug/trace how these functions are called, the variables passed and what is sent to the LLM (gpt3.5 turbo chat completions in this case).

@evchaki evchaki added the planner Anything related to planner or plans label Jun 14, 2023
@lemillermicrosoft
Copy link
Member

What is the output? Why do you think the output is not passing from the first to the second? What happens if you name both of the variables in your *_prompt as {{ $input }}?

@lemillermicrosoft lemillermicrosoft added the python Pull requests for the Python Semantic Kernel label Jun 14, 2023
@gopitk
Copy link
Author

gopitk commented Jun 14, 2023

Output is :

A multinational technology company known for its innovative hardware and software products, including smartphones, laptops, and operating systems.

On langchain I get a description that looks more what is approp for the product.

Regal Linens is a premium provider of high-quality linens for hotels, restaurants, and home use, offering a wide range of colors and sizes.'

No major difference when I use $input as the prompt var. That is one reason I wanted some kind of trace to see what is being sent to the LLM in the 2nd step.

@gopitk
Copy link
Author

gopitk commented Jun 15, 2023

Hi @lemillermicrosoft - I retried with $input and now seem to get proper result. I was reasonably sure I had tried it. Anyway it does seem like if I use $input in both the prompt it seems to be chaining properly. I do suggest to add this info to the documents to use $input as I dont believe I could find it without going into the library source.

I also have a more general SequentialChain I am trying to rewrite it for SK where there is a need to define output context variable so that I can pass things between elements in the chain more flexibly (like pass output of 1st stage to 2,3,4. And pass output of stage 3 to Stage 5 in the sequential chain). Is this supported in Semantic Kernel? If so, can you please share a python sample? In LC it is done by specifying a output_key in the LLMChain.

@gopitk
Copy link
Author

gopitk commented Jun 16, 2023

Closing this issue. Thanks for the inputs.

Summary:

SimpleSequentialChain : Works fine when using $input as the variable
SequentialChain (where you need more flexibility in chaining stages): Needs a workaround currently to copy the step output to a different context variable name in a Native function.

@gopitk gopitk closed this as completed Jun 16, 2023
@lemillermicrosoft lemillermicrosoft self-assigned this Jun 22, 2023
@nacharya1 nacharya1 changed the title Passing output of step to next in SequentialChain Python: Passing output of step to next in SequentialChain Jul 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
planner Anything related to planner or plans python Pull requests for the Python Semantic Kernel
Projects
Archived in project
Status: Sprint: Done
Development

No branches or pull requests

3 participants