From 819262d25bf3213e623586b8cefc3864314b70fb Mon Sep 17 00:00:00 2001 From: r0r-5chach Date: Fri, 23 Feb 2024 02:08:05 +0000 Subject: [PATCH] ai chatbot --- .gitignore | 2 + __pycache__/api.cpython-311.pyc | Bin 0 -> 800 bytes bot.py | 84 ++++++++++++++++++++++++ plugins/SupportPlugin/Chat/config.json | 0 plugins/SupportPlugin/Chat/skprompt.txt | 11 ++++ plugins/mem/trial_regulations.json | 4 ++ requirements.txt | 2 + 7 files changed, 103 insertions(+) create mode 100644 .gitignore create mode 100644 __pycache__/api.cpython-311.pyc create mode 100644 bot.py create mode 100644 plugins/SupportPlugin/Chat/config.json create mode 100644 plugins/SupportPlugin/Chat/skprompt.txt create mode 100644 plugins/mem/trial_regulations.json create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dae70f6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +__pychache__ diff --git a/__pycache__/api.cpython-311.pyc b/__pycache__/api.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..02e27945223b42107a3b2f5a4ea619120b7a5402 GIT binary patch literal 800 zcmZ`%L2DC16n-{GL~sZkI4MJgtPlbe1Ubg)=YB7&P(hFSj6pR4oE{VG#5gg|_F#moaOD9Q zsUJy$L89~>E0DME-|Z699?QQf#eiMu#f3GK2>O|xu!Z*SEp*~bVHpEl`}l1G_}~$ zFE{l2FsXYMhI!arEu8WW7+E3 mD`(HCvNloP%)FjC2x=tPAi0<-WHAOzKN-*qLzP~+H}enh=d`2% literal 0 HcmV?d00001 diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..667db9e --- /dev/null +++ b/bot.py @@ -0,0 +1,84 @@ +import json, asyncio, websockets +import semantic_kernel as sk +from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion, OpenAITextEmbedding + +api_key, org_id = sk.openai_settings_from_dot_env() +EXIT_CHAT = "\n\nExiting chat..." + +async def init_kernel(): + kernel = sk.Kernel() + + kernel.add_chat_service( + "chat-gpt", + OpenAIChatCompletion( + ai_model_id="gpt-3.5-turbo-1106", + api_key=api_key, + org_id=org_id + ) + ) + + kernel.add_text_embedding_generation_service( + "ada", + OpenAITextEmbedding( + ai_model_id="text-embedding-ada-002", + api_key=api_key, + org_id=org_id + ) + ) + + kernel.register_memory_store(memory_store=sk.memory.VolatileMemoryStore()) + kernel.import_plugin(sk.core_plugins.TextMemoryPlugin(), "text_memory") + await populate_memory(kernel) + + return kernel + + +def init_context(): + context = kernel.create_new_context() + context["chat_history"] = "" + context[sk.core_plugins.TextMemoryPlugin.COLLECTION_PARAM] = "regulation" + context[sk.core_plugins.TextMemoryPlugin.RELEVANCE_PARAM] = "0.8" + + return context + +async def populate_memory(kernel): + memories = json.load(open("plugins/mem/trial_regulations.json")) + + for id, text in memories.items(): + await kernel.memory.save_information( + collection="regulations", + id=id, + text=text + ) + +async def chat(websocket, kernel, chat_function, context): + try: + websocket.send("User: ") + user_input = websocket.recv() + context["user_input"] = user_input + except KeyboardInterrupt: + websocket.send(EXIT_CHAT) + return False + + if user_input == "exit": + websocket.send(EXIT_CHAT) + return False + + answer = await kernel.run(chat_function, input_vars=context.variables) + context["chat_history"] += f"\nUser: {user_input}\nSupport: {answer}\n" + websocket.send(f"Support: {answer}") + return True + +async def init_chat(websocket, path): + kernel = await init_kernel() + context = init_context() + chat_function = kernel.import_semantic_plugin_from_directory("plugins/", "SupportPlugin")["Chat"] + populate_memory(kernel) + + chatting = True + while chatting: + chatting = await chat(kernel, chat_function, context) + +server = websockets.serve(init_chat, "localhost", 8000) +asyncio.get_event_loop().run_until_complete(server) +asyncio.get_event_loop().run_forever() diff --git a/plugins/SupportPlugin/Chat/config.json b/plugins/SupportPlugin/Chat/config.json new file mode 100644 index 0000000..e69de29 diff --git a/plugins/SupportPlugin/Chat/skprompt.txt b/plugins/SupportPlugin/Chat/skprompt.txt new file mode 100644 index 0000000..ff544ba --- /dev/null +++ b/plugins/SupportPlugin/Chat/skprompt.txt @@ -0,0 +1,11 @@ +SUPPORT HELPS THE USER BY RESPONDING TO QUESTIONS THAT THEY ASK. +NO BIAS OR BIGOTRY. +BE CONSISE. +EXPLAIN CONCEPTS SIMPLY AND CLEARLY. ++++++ +Welcome to support. Please ask any questions you may have. + +Chat: +{{$chat_history}} +User: {{$user_input}} +Support: diff --git a/plugins/mem/trial_regulations.json b/plugins/mem/trial_regulations.json new file mode 100644 index 0000000..0cd355b --- /dev/null +++ b/plugins/mem/trial_regulations.json @@ -0,0 +1,4 @@ +{ + "info1": "asdasdasd", + "info2": "asdasdsdas" +} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7959e04 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +semantic-kernel +websockets