From e063dc943d1ec6a483818a962031a0a6d28e519a Mon Sep 17 00:00:00 2001 From: Hussain Nagaria Date: Wed, 21 Jun 2023 13:11:58 +0530 Subject: [PATCH] feat: select openAI model in settings --- doppio_bot/api.py | 10 ++++- .../doctype/__init__.py | 0 .../doctype/doppiobot_settings/__init__.py | 0 .../doppiobot_settings/doppiobot_settings.js | 8 ++++ .../doppiobot_settings.json | 42 +++++++++++++++++++ .../doppiobot_settings/doppiobot_settings.py | 9 ++++ .../test_doppiobot_settings.py | 9 ++++ 7 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 doppio_bot/frappe_chatgpt_integration/doctype/__init__.py create mode 100644 doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/__init__.py create mode 100644 doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/doppiobot_settings.js create mode 100644 doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/doppiobot_settings.json create mode 100644 doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/doppiobot_settings.py create mode 100644 doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/test_doppiobot_settings.py diff --git a/doppio_bot/api.py b/doppio_bot/api.py index a5cb589..314fba8 100644 --- a/doppio_bot/api.py +++ b/doppio_bot/api.py @@ -5,7 +5,6 @@ from langchain.chains import ConversationChain from langchain.prompts import PromptTemplate -OPENAI_MODEL_NAME = "gpt-3.5-turbo" # Note: Copied the default template and added extra instructions for code output prompt_template = PromptTemplate( @@ -33,11 +32,12 @@ def get_chatbot_response(session_id: str, prompt_message: str) -> str: # Throw if no key in site_config # Maybe extract and cache this (site cache) opeai_api_key = frappe.conf.get("openai_api_key") + openai_model = get_model_from_settings() if not opeai_api_key: frappe.throw("Please set `openai_api_key` in site config") - llm = OpenAI(model_name=OPENAI_MODEL_NAME, temperature=0, openai_api_key=opeai_api_key) + llm = OpenAI(model_name=openai_model, temperature=0, openai_api_key=opeai_api_key) message_history = RedisChatMessageHistory( session_id=session_id, url=frappe.conf.get("redis_cache") or "redis://localhost:6379/0", @@ -47,3 +47,9 @@ def get_chatbot_response(session_id: str, prompt_message: str) -> str: response = conversation_chain.run(prompt_message) return response + + +def get_model_from_settings(): + return ( + frappe.db.get_single_value("DoppioBot Settings", "openai_model") or "gpt-3.5-turbo" + ) diff --git a/doppio_bot/frappe_chatgpt_integration/doctype/__init__.py b/doppio_bot/frappe_chatgpt_integration/doctype/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/__init__.py b/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/doppiobot_settings.js b/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/doppiobot_settings.js new file mode 100644 index 0000000..8c61834 --- /dev/null +++ b/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/doppiobot_settings.js @@ -0,0 +1,8 @@ +// Copyright (c) 2023, Hussain Nagaria and contributors +// For license information, please see license.txt + +// frappe.ui.form.on("DoppioBot Settings", { +// refresh(frm) { + +// }, +// }); diff --git a/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/doppiobot_settings.json b/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/doppiobot_settings.json new file mode 100644 index 0000000..f20b9ad --- /dev/null +++ b/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/doppiobot_settings.json @@ -0,0 +1,42 @@ +{ + "actions": [], + "allow_rename": 1, + "creation": "2023-06-21 13:00:34.366049", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "openai_model" + ], + "fields": [ + { + "default": "gpt-3.5-turbo", + "fieldname": "openai_model", + "fieldtype": "Select", + "label": "OpenAI Model", + "options": "gpt-3.5-turbo\ngpt-3.5-turbo-16k\ntext-davinci-003\ngpt-4\ngpt-4-32k" + } + ], + "index_web_pages_for_search": 1, + "issingle": 1, + "links": [], + "modified": "2023-06-21 13:06:09.895694", + "modified_by": "Administrator", + "module": "Frappe ChatGPT Integration", + "name": "DoppioBot Settings", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "print": 1, + "read": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/doppiobot_settings.py b/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/doppiobot_settings.py new file mode 100644 index 0000000..34482aa --- /dev/null +++ b/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/doppiobot_settings.py @@ -0,0 +1,9 @@ +# Copyright (c) 2023, Hussain Nagaria and contributors +# For license information, please see license.txt + +# import frappe +from frappe.model.document import Document + + +class DoppioBotSettings(Document): + pass diff --git a/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/test_doppiobot_settings.py b/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/test_doppiobot_settings.py new file mode 100644 index 0000000..e268451 --- /dev/null +++ b/doppio_bot/frappe_chatgpt_integration/doctype/doppiobot_settings/test_doppiobot_settings.py @@ -0,0 +1,9 @@ +# Copyright (c) 2023, Hussain Nagaria and Contributors +# See license.txt + +# import frappe +from frappe.tests.utils import FrappeTestCase + + +class TestDoppioBotSettings(FrappeTestCase): + pass