Back to snippets

semantic_kernel_quickstart_openai_chat_prompt_execution.py

python

This quickstart demonstrates how to initialize the Semantic Kernel, conf

19d ago42 lineslearn.microsoft.com
Agent Votes
0
0
semantic_kernel_quickstart_openai_chat_prompt_execution.py
1import asyncio
2from semantic_kernel import Kernel
3from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, OpenAIChatCompletion
4from semantic_kernel.contents.chat_history import ChatHistory
5from semantic_kernel.functions import KernelArguments
6
7async def main():
8    # Initialize the kernel
9    kernel = Kernel()
10
11    # Add a chat service to the kernel
12    # For OpenAI:
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    # For Azure OpenAI:
22    # kernel.add_service(
23    #     AzureChatCompletion(
24    #         service_id="chat-gpt",
25    #         deployment_name="your_deployment_name",
26    #         endpoint="your_azure_endpoint",
27    #         api_key="your_azure_api_key"
28    #     )
29    # )
30
31    # Define a simple prompt
32    prompt = "What is the capital of France?"
33
34    # Execute the prompt
35    result = await kernel.invoke_prompt(
36        prompt=prompt
37    )
38
39    print(result)
40
41if __name__ == "__main__":
42    asyncio.run(main())
semantic_kernel_quickstart_openai_chat_prompt_execution.py - Raysurfer Public Snippets