microsoft / semantic-kernel

Integrate cutting-edge LLM technology quickly and easily into your apps
https://aka.ms/semantic-kernel
MIT License
21.82k stars 3.25k forks source link

Python: AttributeError: 'Plan' object has no attribute 'plan' #2660

Closed Chris-hughes10 closed 1 year ago

Chris-hughes10 commented 1 year ago

Describe the bug When trying to use the basic planner, it seems to be unable to generate any sort of plan, raising an attribute error.

To Reproduce

import asyncio
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion

OPENAI_ENDPOINT = "https://dev-ch-uk.openai.azure.com/"
OPENAI_DEPLOYMENT_NAME = "gpt35"
OPENAI_API_KEY = getpass("Enter your API key: ")
OPENAI_EMBEDDING_DEPLOYMENT_NAME = "ada"

from semantic_kernel.planning.basic_planner import BasicPlanner

async def main():
    kernel = sk.Kernel()
    kernel.add_chat_service(
        "azure_gpt35_chat_completion",
        AzureChatCompletion(
            deployment_name=OPENAI_DEPLOYMENT_NAME,
            endpoint=OPENAI_ENDPOINT,
            api_key=OPENAI_API_KEY,
        ),
    )

    planner = BasicPlanner()

    ask = """
    Tomorrow is Valentine's day. I need to come up with a few date ideas. She speaks French so write it in French.
    Convert the text to uppercase"""
    original_plan = await planner.create_plan_async(ask, kernel)

    print(original_plan)

if __name__ == '__main__':
    asyncio.run(main())

Stacktrace

  File "/home/azureuser/localfiles/blog-posts/explore2.py", line 35, in <module>
    asyncio.run(main())
  File "/anaconda/envs/azureml_py310_sdkv2/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/anaconda/envs/azureml_py310_sdkv2/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/home/azureuser/localfiles/blog-posts/explore2.py", line 32, in main
    print(original_plan)
  File "/anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/semantic_kernel/planning/basic_planner.py", line 21, in __str__
    return f"Prompt: {self.prompt}\nGoal: {self.goal}\nPlan: {self.plan}"
AttributeError: 'Plan' object has no attribute 'plan'

Expected behavior A plan is printed. If it is unable to generate a plan, it should either return an empty dict or raise an error.

The issue seems to be in this line in the Plan class

def __str__(self):
        return f"Prompt: {self.prompt}\nGoal: {self.goal}\nPlan: {self.plan}"

it appears that self.plan has been renamed to self.generated_plan

Platform

Additional context Add any other context about the problem here.

Mayortechkid commented 1 year ago

Wow

Chris-hughes10 commented 1 year ago

It looks like there's a bug in the code. The Plan class is trying to access an attribute self.plan, which has been renamed to self.generated_plan. You need to update the code in the BasicPlanner class to use the correct attribute name. This should fix the AttributeError you're encountering. class Plan: def init(self, prompt, goal, generated_plan): self.prompt = prompt self.goal = goal self.generated_plan = generated_plan

def __str__(self):
    return f"Prompt: {self.prompt}\nGoal: {self.goal}\nPlan: {self.plan}"

To fix the bug, you'll need to modify the str method in the Plan class of the BasicPlanner module. Instead of using self.plan, you should use self.generated_plan. Here's how you can do it:

Open the file located at /anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/semantic_kernel/planning/basic_planner.py. Find the Plan class definition. It should look something like this: python Copy code class Plan: def init(self, prompt, goal, generated_plan): self.prompt = prompt self.goal = goal self.generated_plan = generated_plan

def __str__(self):
    return f"Prompt: {self.prompt}\nGoal: {self.goal}\nPlan: {self.plan}"

Modify the str method to use self.generated_plan instead of self.plan: python Copy code class Plan: def init(self, prompt, goal, generated_plan): self.prompt = prompt self.goal = goal self.generated_plan = generated_plan

def __str__(self):
    return f"Prompt: {self.prompt}\nGoal: {self.goal}\nPlan: {self.generated_plan}"

Save the file. This should resolve the AttributeError issue you're facing. Once you've made this change, you can rerun your code, and it should now print the plan correctly using the self.generated_plan attribute.

Understood, but I'd rather it's fixed in the repo ;)

Chris-hughes10 commented 1 year ago

Additionally, when both a text completion service and a chat completion service are registered, the output isn't as intended. After fixing the bug, and using the prompt write a poem based on an image the output I get is: image

I assume that this is using the text completion service (GPT3.5-turbo) that I have registered, rather than the chat service (also GPT3.5-turbo), so is treating the task as an autocomplete rather than an instruction.

Should the class default to using chat services rather than text completion?

nacharya1 commented 1 year ago

@lemillermicrosoft - please confirm the bug and provide any suggestions on next steps.