Files
semantic-kernel/python/samples/kernel-syntax-examples/chat.py
T
0a477b198c Python: Update examples and notebooks to stop using text-davinci-003 (#2359)
### Motivation and Context

text-davinci-003 is being retired by Azure OpenAI and is no longer
supported for new deployments

Resolves - https://github.com/microsoft/semantic-kernel/issues/2309

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

Update examples and notebooks to use gpt-35-turbo / gpt-3.5-turbo
instead

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄

Signed-off-by: karthikmurali60 <[email protected]>
Co-authored-by: Lee Miller <[email protected]>
Co-authored-by: Gil LaHaye <[email protected]>
2023-08-23 17:24:56 +00:00

70 lines
1.7 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
import asyncio
import semantic_kernel as sk
import semantic_kernel.connectors.ai.open_ai as sk_oai
sk_prompt = """
ChatBot can have a conversation with you about any topic.
It can give explicit instructions or say 'I don't know'
when it doesn't know the answer.
{{$chat_history}}
User:> {{$user_input}}
ChatBot:>
"""
kernel = sk.Kernel()
api_key, org_id = sk.openai_settings_from_dot_env()
kernel.add_chat_service(
"chat-gpt", sk_oai.OpenAIChatCompletion("gpt-3.5-turbo", api_key, org_id)
)
prompt_config = sk.PromptTemplateConfig.from_completion_parameters(
max_tokens=2000, temperature=0.7, top_p=0.4
)
prompt_template = sk.PromptTemplate(
sk_prompt, kernel.prompt_template_engine, prompt_config
)
function_config = sk.SemanticFunctionConfig(prompt_config, prompt_template)
chat_function = kernel.register_semantic_function("ChatBot", "Chat", function_config)
async def chat(context_vars: sk.ContextVariables) -> bool:
try:
user_input = input("User:> ")
context_vars["user_input"] = user_input
except KeyboardInterrupt:
print("\n\nExiting chat...")
return False
except EOFError:
print("\n\nExiting chat...")
return False
if user_input == "exit":
print("\n\nExiting chat...")
return False
answer = await kernel.run_async(chat_function, input_vars=context_vars)
context_vars["chat_history"] += f"\nUser:> {user_input}\nChatBot:> {answer}\n"
print(f"ChatBot:> {answer}")
return True
async def main() -> None:
context = sk.ContextVariables()
context["chat_history"] = ""
chatting = True
while chatting:
chatting = await chat(context)
if __name__ == "__main__":
asyncio.run(main())