Running a simple prompt
This section assumes you completed the prior sections and builds upon the same code. By now, you should have instantiated Semantic Kernel and loaded both the GPT-3.5 and the GPT-4 services into it in that order. When you submit a prompt, it will default to the first service, and will run the prompt on GPT-3.5.
When we send the prompt to the service, we will also send a parameter called temperature. The temperature parameter goes from 0.0
to 1.0
, and it controls how random the responses are. We’re going to explain the temperature parameter in more detail in later chapters. A temperature parameter of 0.8
generates a more creative response, while a temperature parameter of 0.2
generates a more precise response.
To send the prompt to the service, we will use a method called create_semantic_function
. For now, don’t worry about what a semantic function is. We’re going to explain it in the Using generative AI to solve simple problems section.
Running a simple prompt in Python
To run a prompt in Python, follow these steps:
- Load the prompt in a string variable:
prompt = "Finish the following knock-knock joke. Knock, knock. Who's there? Dishes. Dishes who?"
- Create a function by using the
add_function
method of the kernel. Thefunction_name
andplugin_name
parameters are required but are not used, so you can give your function and plugin whatever name you want:prompt_function = kernel.add_function(function_name="ex01", plugin_name="sample", prompt=prompt)
- Call the function. Note that all invocation methods are asynchronous, so you need to use
await
to wait for their return:response = await kernel.invoke(prompt_function, request=prompt)
- Print the response:
print(response)
The response is nondeterministic. Here’s a possible response:
Dishes the police, open up!
Running a simple prompt in C#
To run a prompt in C#, follow these steps:
- Load the prompt in a string variable:
string prompt = "Finish the following knock-knock joke. Knock, knock. Who's there? Dishes. Dishes who?";
- Call the prompt by using the
Kernel.InvokePromptAsync
function:var joke = await kernel.InvokePromptAsync(prompt);
- Print the response:
Console.Write(joke)
The response is nondeterministic. Here’s a possible response:
Dishes a very bad joke, but I couldn't resist!
We have now connected to an AI service, submitted a prompt to it, and obtained a response. We’re now ready to start creating our own functions.