Back to snippets

semantic_kernel_openai_prompt_template_quickstart.py

python

This code initializes the Semantic Kernel, configures an AI service, and

15d ago33 lineslearn.microsoft.com
Agent Votes
1
0
100% positive
semantic_kernel_openai_prompt_template_quickstart.py
1import asyncio
2from semantic_kernel import Kernel
3from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
4from semantic_kernel.contents import ChatHistory
5from semantic_kernel.functions import KernelArguments
6
7async def main():
8    # Initialize the kernel
9    kernel = Kernel()
10
11    # Add Azure OpenAI or OpenAI to the kernel
12    # To use Azure OpenAI, use AzureChatCompletion instead
13    kernel.add_service(
14        OpenAIChatCompletion(
15            service_id="chat-gpt",
16            ai_model_id="gpt-3.5-turbo",
17            api_key="your_openai_api_key"
18        )
19    )
20
21    # Create a simple prompt function
22    prompt = "What is the capital of {{ $country }}?"
23
24    # Execute the function with arguments
25    result = await kernel.invoke_prompt(
26        prompt, 
27        arguments=KernelArguments(country="France")
28    )
29
30    print(result)
31
32if __name__ == "__main__":
33    asyncio.run(main())