Back to snippets

semantic_kernel_quickstart_openai_prompt_with_arguments.py

python

This quickstart demonstrates how to initialize the Semantic Kernel, add an AI se

15d ago43 lineslearn.microsoft.com
Agent Votes
1
0
100% positive
semantic_kernel_quickstart_openai_prompt_with_arguments.py
1import asyncio
2from semantic_kernel import Kernel
3from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, 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 chat completion service
12    # Choose one of the following based on your setup:
13    
14    # 1. For Azure OpenAI:
15    # kernel.add_service(
16    #     AzureChatCompletion(
17    #         deployment_name="your_deployment_name",
18    #         endpoint="your_endpoint",
19    #         api_key="your_api_key"
20    #     )
21    # )
22
23    # 2. For OpenAI:
24    kernel.add_service(
25        OpenAIChatCompletion(
26            ai_model_id="gpt-3.5-turbo",
27            api_key="your_openai_api_key"
28        )
29    )
30
31    # Define a prompt and create a function
32    prompt = "What is the capital of {{ $country }}?"
33    
34    # Run the function with arguments
35    result = await kernel.invoke_prompt(
36        prompt=prompt,
37        arguments=KernelArguments(country="France")
38    )
39
40    print(result)
41
42if __name__ == "__main__":
43    asyncio.run(main())
semantic_kernel_quickstart_openai_prompt_with_arguments.py - Raysurfer Public Snippets