microsoft / semantic-kernel

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

Python: how to set system message when using StepwisePlanner? #4891

Closed LeonardoSanBenitez closed 8 months ago

LeonardoSanBenitez commented 8 months ago

I would like to set a system message, for example "you are a funny AI assistant that answer with jokes". I found some issues mentioning system messages, like #2722 and #4648, but I have not understood how to set the message.

For example, I would like to include a system message in the following code:

import os
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.core_plugins.math_plugin import MathPlugin
from semantic_kernel.planning import StepwisePlanner
from semantic_kernel.planning.stepwise_planner.stepwise_planner_config import StepwisePlannerConfig

kernel = sk.Kernel()
kernel.add_chat_service(
    "chat_completion",
    AzureChatCompletion(
        os.getenv('AZURE_OPENAI_DEPLOYMENT_NAME_CHAT'),
        endpoint= os.getenv('AZURE_OPENAI_ENDPOINT'),
        api_key = os.getenv('AZURE_OPENAI_KEY'),
    ),
)

kernel.import_plugin(MathPlugin(), "math")

planner = StepwisePlanner(kernel, StepwisePlannerConfig(max_iterations=10, min_iteration_time_ms=1000))

ask = 'How much is 1 plus one'
plan = planner.create_plan(goal=ask)
result = await plan.invoke()

Considering the env variables AZURE_OPENAI_DEPLOYMENT_NAME_CHAT, AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_KEY properly set.

Semantic kernel version: 0.5.0.dev0

moonbox3 commented 8 months ago

Hi @LeonardoSanBenitez, thanks for your question. Currently in Python, there isn't a native chat mode planner, as is available in dotnet. We're working on getting the SK Python SDK to v1 (dotnet is at 1.3 now). There will be more planner work in the near future, as we working on aligning things with dotnet, so please stay tuned.

LeonardoSanBenitez commented 8 months ago

Thank you. But even when using text-completion models, it should still be possible to modify/expand the base prompt to include a sentence like "answer with jokes", correct? Do you have an example on how to configure that?

moonbox3 commented 8 months ago

@LeonardoSanBenitez you can add system/assistant messages via a ChatPromptTemplate which you can find an example of here.

moonbox3 commented 8 months ago

Closing this issue. Please re-open if you need help with this specific issue, otherwise please create a new issue for the related item.

LeonardoSanBenitez commented 8 months ago

but when using StepwisePlanner we can not set the messages individually, and they have to be included in the prompt template, for example here: https://github.com/microsoft/semantic-kernel/blob/eed51932f2fb5879de47e76b752f9051cb868331/dotnet/src/Planners/Planners.Core/Stepwise/Plugin/StepwiseStep/skprompt.txt

Am I understanding this correctly? If so, could you provide me an example on how to set the system message (or equivalente instruction prompt) when using StepwisePlanner?