From f820ba81f7bdb8f5a0f37eb1bcfdb5e89d219408 Mon Sep 17 00:00:00 2001 From: Josh Bocarsly <32345545+jdbocarsly@users.noreply.github.com> Date: Fri, 30 Jun 2023 22:39:37 +0100 Subject: [PATCH] Chat improvements (#414) * prompt updates add ability to change temperature in the frontend * fix bug in field_utils.js where block and item fields couldn't be set to 0 (introduced in #309 and #312) * Fix typos in prompt Co-authored-by: Matthew Evans <7916000+ml-evs@users.noreply.github.com> tweak prompt to work well after typos * pin openai model to current model --- pydatalab/pydatalab/apps/chat/blocks.py | 19 +++--- .../src/components/datablocks/ChatBlock.vue | 60 +++++++++++++++---- webapp/src/field_utils.js | 4 +- 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/pydatalab/pydatalab/apps/chat/blocks.py b/pydatalab/pydatalab/apps/chat/blocks.py index 0733ff227..af4b89bc6 100644 --- a/pydatalab/pydatalab/apps/chat/blocks.py +++ b/pydatalab/pydatalab/apps/chat/blocks.py @@ -11,7 +11,7 @@ from pydatalab.utils import CustomJSONEncoder __all__ = "ChatBlock" -MODEL = "gpt-3.5-turbo" +MODEL = "gpt-3.5-turbo-0613" MAX_CONTEXT_SIZE = 4097 @@ -35,14 +35,14 @@ def num_tokens_from_messages(messages: Sequence[dict]): class ChatBlock(DataBlock): blocktype = "chat" - description = "LLM Chat Block with contextual data (powered by GPT-3.5-turbo)" + description = "Virtual assistant" accepted_file_extensions: Sequence[str] = [] __supports_collections = True defaults = { - "system_prompt": """You are a virtual assistant that helps materials chemists manage their experimental data and plan experiments. You are deployed in the group of Professor Clare Grey in the Department of Chemistry at the University of Cambridge. + "system_prompt": """You are whinchat (lowercase w), a virtual data managment assistant that helps materials chemists manage their experimental data and plan experiments. You are deployed in the group of Professor Clare Grey in the Department of Chemistry at the University of Cambridge. You are embedded within the program datalab, where you have access to JSON describing an ‘item’, or a collection of items, with connections to other items. These items may include experimental samples, starting materials, and devices (e.g. battery cells made out of experimental samples and starting materials). -Answer questions in markdown. Specify the language for all markdown code blocks. You can make diagrams by writing a mermaid code block or an svg code block. -Be as concise as possible. Start the conversion with a friendly greeting introducing yourself. +Answer questions in markdown. Specify the language for all markdown code blocks. You can make diagrams by writing a mermaid code block or an svg code block. When writing mermaid code, you must use quotations around each of the labels (e.g. A["label1"] --> B["label2"]) +Be as concise as possible. When saying your name, type a bird emoji right after whinchat 🐦. """, "temperature": 0.2, "error_message": None, @@ -61,7 +61,6 @@ def plot_functions(self): def render(self): if not self.data.get("messages"): - if (item_id := self.data.get("item_id")) is not None: info_json = self._prepare_item_json_for_chat(item_id) elif (collection_id := self.data.get("collection_id")) is not None: @@ -76,7 +75,8 @@ def render(self): }, { "role": "user", - "content": f"""Here is the JSON data for the current item(s): {info_json}""", + "content": f"""Here is the JSON data for the current item(s): {info_json}. +Start with a friendly introduction and give me a one sentence summary of what this is (not detailed, no information about specific masses). """, }, ] @@ -107,7 +107,7 @@ def render(self): try: LOGGER.debug( - f"submitting request to OpenAI API for completion with last message role \"{self.data['messages'][-1]['role']}\" (message = {self.data['messages'][-1:]})" + f"submitting request to OpenAI API for completion with last message role \"{self.data['messages'][-1]['role']}\" (message = {self.data['messages'][-1:]}). Temperature = {self.data['temperature']} (type {type(self.data['temperature'])})" ) responses = openai.ChatCompletion.create( model=MODEL, @@ -128,9 +128,10 @@ def render(self): except AttributeError: self.data["messages"].append(responses["choices"][0]["message"]) + self.data["model_name"] = MODEL + token_count = num_tokens_from_messages(self.data["messages"]) self.data["token_count"] = token_count - return def _prepare_item_json_for_chat(self, item_id: str): diff --git a/webapp/src/components/datablocks/ChatBlock.vue b/webapp/src/components/datablocks/ChatBlock.vue index 36e4c5693..45720e7aa 100644 --- a/webapp/src/components/datablocks/ChatBlock.vue +++ b/webapp/src/components/datablocks/ChatBlock.vue @@ -2,20 +2,37 @@ -
- [show prompts] +
+ [show advanced]
-
- [hide prompts] +
+ [hide advanced]
-
- Conversation token count: {{ tokenCount }} (max: 4097) -
+
+ : {{ modelName }}
+ : {{ tokenCount }}/4097 - +
+ + + + Temperature must must be a number between 0 and 1 + +
+
+