From edcb46d26ce69d664a938dc1dbd69dc032c23fd8 Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Sun, 3 Sep 2023 22:11:12 +0530 Subject: [PATCH 01/18] cohere lib added in project dependency --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index fd2dd53a..4db93259 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,7 @@ tabulate = "^0.9.0" functions-framework = "^3.4.0" yaspin = "^3.0.0" pydantic = "^2.3.0" +cohere = "^4.21" [build-system] requires = ["poetry-core"] From 68a7e1b133e2d64d2ed81e5b9ccdf2b14af34b8e Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Sun, 3 Sep 2023 22:12:33 +0530 Subject: [PATCH 02/18] cohere class added and generate and chat method created. --- textbase/models.py | 81 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 3 deletions(-) diff --git a/textbase/models.py b/textbase/models.py index 814ed533..5028c1d2 100644 --- a/textbase/models.py +++ b/textbase/models.py @@ -4,18 +4,27 @@ import time import typing import traceback +import cohere from textbase import Message # Return list of values of content. -def get_contents(message: Message, data_type: str): +def get_contents(message: Message, data_type: str, client: str = None): return [ + { + "user_name": message["role"], + "message": content["value"] + } + if content["data_type"] == data_type and client == "cohere" + else { "role": message["role"], "content": content["value"] } - for content in message["content"] if content["data_type"] == data_type + else + f"Content data type is not {data_type}" + for content in message["content"] ] # Returns content if it's non empty. @@ -143,4 +152,70 @@ def generate( data = json.loads(response.text) # parse the JSON data into a dictionary message = data['message'] - return message \ No newline at end of file + return message + + +class Cohere: + api_key = None + + @classmethod + def generate( + cls, + system_prompt: str, + message_history: list[Message], + model: str = "command", + max_tokens=3500, + temperature=0.5, + ): + assert cls.api_key is not None, "Cohere API key is not set." + filtered_messages = [] + + for message in message_history: + #list of all the contents inside a single message + contents = get_contents(message, "STRING") + if contents: + filtered_messages.extend(contents) + cohere_client = cohere.Client(cls.api_key) + response = cohere_client.generate( + prompt=system_prompt, + model=model, + temperature=temperature, + max_tokens=max_tokens, + ) + return response[0] + + @classmethod + def chat( + cls, + system_prompt: str, + user_name:str, + message_history: list[Message], + model: str = "command", + max_tokens=1500, + temperature=0.5, + ): + assert cls.api_key is not None, "Cohere API key is not set." + filtered_messages = [] + + for message in message_history: + #list of all the contents inside a single message + contents = get_contents(message, "STRING","cohere") + if contents: + filtered_messages.extend(contents) + + # initializing cohere client + cohere_client = cohere.Client(cls.api_key) + + response = cohere_client.chat( + message=system_prompt, + user_name=user_name, + model=model, + chat_history=[ + *map(dict, filtered_messages), + ], + temperature=temperature, + max_tokens=max_tokens, + ) + + return response.text + \ No newline at end of file From 544eb7aa314a1e0eb04449f68493a08e337434cf Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Sun, 3 Sep 2023 22:14:06 +0530 Subject: [PATCH 03/18] cohere bot for response generation of given prompt added in examples --- examples/cohere-prompt-bot/main.py | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/cohere-prompt-bot/main.py diff --git a/examples/cohere-prompt-bot/main.py b/examples/cohere-prompt-bot/main.py new file mode 100644 index 00000000..d6c8f7f3 --- /dev/null +++ b/examples/cohere-prompt-bot/main.py @@ -0,0 +1,43 @@ +from textbase import bot, Message +from textbase.models import Cohere +from typing import List + +# Load your Cohere API key +Cohere.api_key = "JRqiMRt2MtCqje9DDEYQd3ZIUEDxLdOfwkG9cem2" + +# Prompt for `command` (cohere's model) +# you add your sample prompts here +SYSTEM_PROMPT = """how python interpreter works! +""" + +@bot() +def on_message(message_history: List[Message], state: dict = None): + + # Generate `command` (cohere's model) response + bot_response = Cohere.generate( + system_prompt=SYSTEM_PROMPT, + message_history=message_history, # Assuming history is the list of user messages + model="command", + ) + + response = { + "data": { + "messages": [ + { + "data_type": "STRING", + "value": bot_response + } + ], + "state": state + }, + "errors": [ + { + "message": "" + } + ] + } + + return { + "status_code": 200, + "response": response + } \ No newline at end of file From a407b39a59d685ae14422d5363bb07e0f2d360c7 Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Sun, 3 Sep 2023 22:17:27 +0530 Subject: [PATCH 04/18] Revert "cohere bot for response generation of given prompt added in examples" This reverts commit 544eb7aa314a1e0eb04449f68493a08e337434cf. --- examples/cohere-prompt-bot/main.py | 43 ------------------------------ 1 file changed, 43 deletions(-) delete mode 100644 examples/cohere-prompt-bot/main.py diff --git a/examples/cohere-prompt-bot/main.py b/examples/cohere-prompt-bot/main.py deleted file mode 100644 index d6c8f7f3..00000000 --- a/examples/cohere-prompt-bot/main.py +++ /dev/null @@ -1,43 +0,0 @@ -from textbase import bot, Message -from textbase.models import Cohere -from typing import List - -# Load your Cohere API key -Cohere.api_key = "JRqiMRt2MtCqje9DDEYQd3ZIUEDxLdOfwkG9cem2" - -# Prompt for `command` (cohere's model) -# you add your sample prompts here -SYSTEM_PROMPT = """how python interpreter works! -""" - -@bot() -def on_message(message_history: List[Message], state: dict = None): - - # Generate `command` (cohere's model) response - bot_response = Cohere.generate( - system_prompt=SYSTEM_PROMPT, - message_history=message_history, # Assuming history is the list of user messages - model="command", - ) - - response = { - "data": { - "messages": [ - { - "data_type": "STRING", - "value": bot_response - } - ], - "state": state - }, - "errors": [ - { - "message": "" - } - ] - } - - return { - "status_code": 200, - "response": response - } \ No newline at end of file From 7b79192487f416e3baf4696afe3f93804f81f73f Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Sun, 3 Sep 2023 22:19:06 +0530 Subject: [PATCH 05/18] cohere bot example added for response generation for given prompt --- examples/cohere-prompt-bot/main.py | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/cohere-prompt-bot/main.py diff --git a/examples/cohere-prompt-bot/main.py b/examples/cohere-prompt-bot/main.py new file mode 100644 index 00000000..538d7764 --- /dev/null +++ b/examples/cohere-prompt-bot/main.py @@ -0,0 +1,43 @@ +from textbase import bot, Message +from textbase.models import Cohere +from typing import List + +# Load your Cohere API key +Cohere.api_key = "" + +# Prompt for `command` (cohere's model) +# you add your sample prompts here +SYSTEM_PROMPT = """how python interpreter works! +""" + +@bot() +def on_message(message_history: List[Message], state: dict = None): + + # Generate `command` (cohere's model) response + bot_response = Cohere.generate( + system_prompt=SYSTEM_PROMPT, + message_history=message_history, # Assuming history is the list of user messages + model="command", + ) + + response = { + "data": { + "messages": [ + { + "data_type": "STRING", + "value": bot_response + } + ], + "state": state + }, + "errors": [ + { + "message": "" + } + ] + } + + return { + "status_code": 200, + "response": response + } \ No newline at end of file From 3c0294c01c7e3c249fd27c4de84ffdf0b3b6e435 Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Sun, 3 Sep 2023 22:19:56 +0530 Subject: [PATCH 06/18] cohere chat bot created --- examples/cohere-chat-bot/chat.py | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 examples/cohere-chat-bot/chat.py diff --git a/examples/cohere-chat-bot/chat.py b/examples/cohere-chat-bot/chat.py new file mode 100644 index 00000000..10fc8fce --- /dev/null +++ b/examples/cohere-chat-bot/chat.py @@ -0,0 +1,42 @@ +from textbase import bot, Message +from textbase.models import Cohere +from typing import List + +# Load your Cohere API key +Cohere.api_key = "" + +# Prompt for `command` (cohere's model) +MESSAGE = "Hey there, How are you!" + +@bot() +def on_message(message_history: List[Message], state: dict = None): + + # Generate `command` (cohere's model) response + bot_response = Cohere.chat( + system_prompt=MESSAGE, + user_name="user", + message_history=message_history, # Assuming history is the list of user messages + model="command", + ) + + response = { + "data": { + "messages": [ + { + "data_type": "STRING", + "value": bot_response + } + ], + "state": state + }, + "errors": [ + { + "message": "" + } + ] + } + + return { + "status_code": 200, + "response": response + } \ No newline at end of file From d14fccf433a8ee4ca787dbf7d06c242af4120a2d Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Sun, 3 Sep 2023 22:20:22 +0530 Subject: [PATCH 07/18] file renamed --- examples/cohere-chat-bot/{chat.py => main.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/cohere-chat-bot/{chat.py => main.py} (100%) diff --git a/examples/cohere-chat-bot/chat.py b/examples/cohere-chat-bot/main.py similarity index 100% rename from examples/cohere-chat-bot/chat.py rename to examples/cohere-chat-bot/main.py From d4ed88ba53d95ee0177f93a0992c82c3b76fae54 Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Sun, 3 Sep 2023 23:22:16 +0530 Subject: [PATCH 08/18] summarize method added to cohere class which is used to get summary of long text --- textbase/models.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/textbase/models.py b/textbase/models.py index 5028c1d2..8d0bb847 100644 --- a/textbase/models.py +++ b/textbase/models.py @@ -218,4 +218,36 @@ def chat( ) return response.text + + @classmethod + def summarize( + cls, + text_input: str, + message_history: list[Message], + model: str = "command", + temperature=0.3, + length:str = "medium", + format:str = "paragraph", + ): + assert cls.api_key is not None, "Cohere API key is not set." + filtered_messages = [] + + for message in message_history: + #list of all the contents inside a single message + contents = get_contents(message, "STRING") + if contents: + filtered_messages.extend(contents) + + # initializing cohere client + cohere_client = cohere.Client(cls.api_key) + + response =cohere_client.summarize( + text=text_input, + length=length, + format=format, + model=model, + temperature=temperature, + ) + + return response.summary \ No newline at end of file From c82ae2ed702e196fbd8af09d7c9e2a3e6e0075e5 Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Sun, 3 Sep 2023 23:23:36 +0530 Subject: [PATCH 09/18] summarize bot added in example with a prompt text for which we want the summary --- examples/cohere-summarize-bot/main.py | 62 +++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 examples/cohere-summarize-bot/main.py diff --git a/examples/cohere-summarize-bot/main.py b/examples/cohere-summarize-bot/main.py new file mode 100644 index 00000000..b1ef0163 --- /dev/null +++ b/examples/cohere-summarize-bot/main.py @@ -0,0 +1,62 @@ +from textbase import bot, Message +from textbase.models import Cohere +from typing import List + +# Load your Cohere API key +Cohere.api_key = "" + +# Prompt for `command` (cohere's model) +# you add your sample prompts here +SYSTEM_PROMPT = """"Ice cream is a sweetened frozen food typically eaten as a snack or dessert. + It may be made from milk or cream and is flavoured with a sweetener, + either sugar or an alternative, and a spice, such as cocoa or vanilla, + or with fruit such as strawberries or peaches. + It can also be made by whisking a flavored cream base and liquid nitrogen together. + Food coloring is sometimes added, in addition to stabilizers. + The mixture is cooled below the freezing point of water and stirred to incorporate air spaces + and to prevent detectable ice crystals from forming. The result is a smooth, + semi-solid foam that is solid at very low temperatures (below 2 °C or 35 °F). + It becomes more malleable as its temperature increases. + The meaning of the name ice cream varies from one country to another. + In some countries, such as the United States, ice cream applies only to a specific variety, + and most governments regulate the commercial use of the various terms according to the + relative quantities of the main ingredients, notably the amount of cream. + Products that do not meet the criteria to be called ice cream are sometimes labelled + frozen dairy dessert instead. In other countries, such as Italy and Argentina, + one word is used for all variants. Analogues made from dairy alternatives, + such as goat's or sheep's milk, or milk substitutes + (e.g., soy, cashew, coconut, almond milk or tofu), are available for those who are + lactose intolerant, allergic to dairy protein or vegan. +""" + +@bot() +def on_message(message_history: List[Message], state: dict = None): + + # Generate `command` (cohere's model) response + bot_response = Cohere.summarize( + text_input=SYSTEM_PROMPT, + message_history=message_history, # Assuming history is the list of user messages + model="command", + ) + + response = { + "data": { + "messages": [ + { + "data_type": "STRING", + "value": bot_response + } + ], + "state": state + }, + "errors": [ + { + "message": "" + } + ] + } + + return { + "status_code": 200, + "response": response + } \ No newline at end of file From 69a0063844f767c82070cb4ee14dedc4d087fe73 Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Sun, 3 Sep 2023 23:48:11 +0530 Subject: [PATCH 10/18] chat history fixes done in chat method of cohere --- textbase/models.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/textbase/models.py b/textbase/models.py index 8d0bb847..547b010c 100644 --- a/textbase/models.py +++ b/textbase/models.py @@ -211,6 +211,10 @@ def chat( user_name=user_name, model=model, chat_history=[ + { + "user_name": "system", + "message": system_prompt + }, *map(dict, filtered_messages), ], temperature=temperature, From 7d03397c91b9c44ca30028d925fabe7e5d6add95 Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Sun, 3 Sep 2023 23:53:14 +0530 Subject: [PATCH 11/18] docstring added for cohere summarize method for better understanding of what method is doing --- textbase/models.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/textbase/models.py b/textbase/models.py index 547b010c..33ee840c 100644 --- a/textbase/models.py +++ b/textbase/models.py @@ -233,6 +233,34 @@ def summarize( length:str = "medium", format:str = "paragraph", ): + """ + Summarize a text_input using the Cohere API based on a text input. + + Args: + cls (object): The class object containing the Cohere API key. + text_input (str): The input text to be summarized. + message_history (list[Message]): A list of messages to consider in the summarization process. + model (str, optional): The model to use for summarization. Defaults to "command". + temperature (float, optional): The temperature parameter for generating the summary. + Higher values (e.g., 1.0) make the output more random, while lower values (e.g., 0.1) make it more deterministic. Defaults to 0.3. + length (str, optional): The desired length of the summary. + Options include "short," "medium," and "long." Defaults to "medium". + format (str, optional): The desired format of the summary, e.g., "paragraph" or other formats supported by Cohere. Defaults to "paragraph". + + Returns: + str: The summarized text generated by the Cohere API. + + Raises: + AssertionError: Raised when the Cohere API key is not set. + + Example: + To summarize a text with a text input using the default parameters: + ``` + api_key = "your_api_key_here" + result = summarize(api_key, "This is the input text.", message_history) + print(result) + ``` + """ assert cls.api_key is not None, "Cohere API key is not set." filtered_messages = [] From f43b5b0dc5c40116af76043493f4ae142d997e98 Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Sun, 3 Sep 2023 23:57:46 +0530 Subject: [PATCH 12/18] docstring added for better understanding the chat method of cohere class --- textbase/models.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/textbase/models.py b/textbase/models.py index 33ee840c..4103959d 100644 --- a/textbase/models.py +++ b/textbase/models.py @@ -194,6 +194,37 @@ def chat( max_tokens=1500, temperature=0.5, ): + + """ + Engage in a chat conversation using the Cohere API. + + Args: + cls (object): The class object containing the Cohere API key. + system_prompt (str): The initial system prompt or message to start the conversation. + user_name (str): The user's name or identifier in the conversation. + message_history (list[Message]): A list of messages representing the chat history. + model (str, optional): The model to use for the chat conversation. Defaults to "command". + max_tokens (int, optional): The maximum number of tokens in the generated response. Defaults to 1500. + temperature (float, optional): The temperature parameter for generating responses. + Higher values (e.g., 1.0) make the output more random, while lower values (e.g., 0.1) make it more deterministic. Defaults to 0.5. + + Returns: + str: The text of the response generated by the Cohere API as part of the chat conversation. + + Raises: + AssertionError: Raised when the Cohere API key is not set. + + Example: + To have a chat conversation using the Cohere API: + ``` + api_key = "your_api_key_here" + system_prompt = "System: Welcome to the chatbot. How can I assist you today?" + user_name = "User123" + message_history = [] # You can add previous messages to the history + result = chat(api_key, system_prompt, user_name, message_history) + print(result) + ``` + """ assert cls.api_key is not None, "Cohere API key is not set." filtered_messages = [] @@ -260,7 +291,7 @@ def summarize( result = summarize(api_key, "This is the input text.", message_history) print(result) ``` - """ + """ assert cls.api_key is not None, "Cohere API key is not set." filtered_messages = [] From a52072de057175ddedbdfc263a3efda8fd336310 Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Mon, 4 Sep 2023 00:00:13 +0530 Subject: [PATCH 13/18] doc string added in cohere class for generate method to easy to interact with the code and more redable and understanding --- textbase/models.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/textbase/models.py b/textbase/models.py index 4103959d..0601d85d 100644 --- a/textbase/models.py +++ b/textbase/models.py @@ -167,6 +167,34 @@ def generate( max_tokens=3500, temperature=0.5, ): + """ + Generate text based on a system prompt and optional message history using the Cohere API. + + Args: + cls (object): The class object containing the Cohere API key. + system_prompt (str): The system prompt or message to generate text from. + message_history (list[Message]): A list of messages to consider in the generation process. + model (str, optional): The model to use for text generation. Defaults to "command". + max_tokens (int, optional): The maximum number of tokens in the generated text. Defaults to 3500. + temperature (float, optional): The temperature parameter for generating the text. + Higher values (e.g., 1.0) make the output more random, while lower values (e.g., 0.1) make it more deterministic. Defaults to 0.5. + + Returns: + str: The generated text produced by the Cohere API based on the input prompt and history. + + Raises: + AssertionError: Raised when the Cohere API key is not set. + + Example: + To generate text based on a system prompt and message history using the Cohere API: + ``` + api_key = "your_api_key_here" + system_prompt = "Generate a creative story about space exploration." + message_history = [] # You can add previous messages to the history + result = generate(api_key, system_prompt, message_history) + print(result) + ``` + """ assert cls.api_key is not None, "Cohere API key is not set." filtered_messages = [] From 80da8975d08ada04d2c6ebfbb7848bea86bea1fb Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Mon, 4 Sep 2023 00:12:30 +0530 Subject: [PATCH 14/18] cohere chat bot document added --- docs/docs/examples/cohere-chat-bot.md | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 docs/docs/examples/cohere-chat-bot.md diff --git a/docs/docs/examples/cohere-chat-bot.md b/docs/docs/examples/cohere-chat-bot.md new file mode 100644 index 00000000..4a1b51ad --- /dev/null +++ b/docs/docs/examples/cohere-chat-bot.md @@ -0,0 +1,52 @@ +--- +sidebar_position: 1 +--- + +# Cohere Chat bot + +This bot makes an API call to Cohere and processes the user input. It uses command model by default. More information added in Cohere Class itself. + +```py +from textbase import bot, Message +from textbase.models import Cohere +from typing import List + +# Load your Cohere API key +Cohere.api_key = "" + +# Prompt for `command` (cohere's model) +MESSAGE = "Is messi better than ronaldo or ronaldo better than messi?" + +@bot() +def on_message(message_history: List[Message], state: dict = None): + + # Generate `command` (cohere's model) response + bot_response = Cohere.chat( + system_prompt=MESSAGE, + user_name="user", + message_history=message_history, # Assuming history is the list of user messages + model="command", + ) + + response = { + "data": { + "messages": [ + { + "data_type": "STRING", + "value": bot_response + } + ], + "state": state + }, + "errors": [ + { + "message": "" + } + ] + } + + return { + "status_code": 200, + "response": response + } +``` \ No newline at end of file From 81619f54dccfb2d5efa3b4e6581c4f9fe270cc8b Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Mon, 4 Sep 2023 00:14:13 +0530 Subject: [PATCH 15/18] cohere chat bot doc fixes --- docs/docs/examples/cohere-chat-bot.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/examples/cohere-chat-bot.md b/docs/docs/examples/cohere-chat-bot.md index 4a1b51ad..954ae39c 100644 --- a/docs/docs/examples/cohere-chat-bot.md +++ b/docs/docs/examples/cohere-chat-bot.md @@ -4,7 +4,7 @@ sidebar_position: 1 # Cohere Chat bot -This bot makes an API call to Cohere and processes the user input. It uses command model by default. More information added in Cohere Class itself. +This chat bot makes an API call to Cohere and processes the user input and also maintain the chat history of user and system. It uses command model by default. More information added in Cohere Class itself. ```py from textbase import bot, Message From 635e95d4935aab68e409d50b85e7b1061c0fbf74 Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Mon, 4 Sep 2023 00:16:03 +0530 Subject: [PATCH 16/18] cohere prompt bot document added --- docs/docs/examples/cohere-prompt-bot.md | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 docs/docs/examples/cohere-prompt-bot.md diff --git a/docs/docs/examples/cohere-prompt-bot.md b/docs/docs/examples/cohere-prompt-bot.md new file mode 100644 index 00000000..af7c49dc --- /dev/null +++ b/docs/docs/examples/cohere-prompt-bot.md @@ -0,0 +1,53 @@ +--- +sidebar_position: 2 +--- + +# Cohere Prompt bot + +This Prompt bot makes an API call to Cohere and processes the user input and resopond accordingly. It uses command model by default. More information added in Cohere Class itself. + +```py +from textbase import bot, Message +from textbase.models import Cohere +from typing import List + +# Load your Cohere API key +Cohere.api_key = "" + +# Prompt for `command` (cohere's model) +# you add your sample prompts here +SYSTEM_PROMPT = """How much developer can love python language? +""" + +@bot() +def on_message(message_history: List[Message], state: dict = None): + + # Generate `command` (cohere's model) response + bot_response = Cohere.generate( + system_prompt=SYSTEM_PROMPT, + message_history=message_history, # Assuming history is the list of user messages + model="command", + ) + + response = { + "data": { + "messages": [ + { + "data_type": "STRING", + "value": bot_response + } + ], + "state": state + }, + "errors": [ + { + "message": "" + } + ] + } + + return { + "status_code": 200, + "response": response + } +``` \ No newline at end of file From 08fd7b96c65edee455f9899083fc486e7dc0755b Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Mon, 4 Sep 2023 00:20:47 +0530 Subject: [PATCH 17/18] cohere summary bot added in textbase docs --- docs/docs/examples/cohere-summarize-bot.md | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 docs/docs/examples/cohere-summarize-bot.md diff --git a/docs/docs/examples/cohere-summarize-bot.md b/docs/docs/examples/cohere-summarize-bot.md new file mode 100644 index 00000000..c9840e7a --- /dev/null +++ b/docs/docs/examples/cohere-summarize-bot.md @@ -0,0 +1,73 @@ +--- +sidebar_position: 3 +--- + +# Cohere Summary bot + +This bot makes an API call to Cohere and processes the long user input and retruns short and crisp summary of the given para in different forms. It uses command model by default. More information added in Cohere Class itself. + +```py +from textbase import bot, Message +from textbase.models import Cohere +from typing import List + +# Load your Cohere API key +Cohere.api_key = "" + +# Prompt for `command` (cohere's model) +# you add your sample prompts here +SYSTEM_PROMPT = """"Ice cream is a sweetened frozen food typically eaten as a snack or dessert. + It may be made from milk or cream and is flavoured with a sweetener, + either sugar or an alternative, and a spice, such as cocoa or vanilla, + or with fruit such as strawberries or peaches. + It can also be made by whisking a flavored cream base and liquid nitrogen together. + Food coloring is sometimes added, in addition to stabilizers. + The mixture is cooled below the freezing point of water and stirred to incorporate air spaces + and to prevent detectable ice crystals from forming. The result is a smooth, + semi-solid foam that is solid at very low temperatures (below 2 °C or 35 °F). + It becomes more malleable as its temperature increases. + The meaning of the name ice cream varies from one country to another. + In some countries, such as the United States, ice cream applies only to a specific variety, + and most governments regulate the commercial use of the various terms according to the + relative quantities of the main ingredients, notably the amount of cream. + Products that do not meet the criteria to be called ice cream are sometimes labelled + frozen dairy dessert instead. In other countries, such as Italy and Argentina, + one word is used for all variants. Analogues made from dairy alternatives, + such as goat's or sheep's milk, or milk substitutes + (e.g., soy, cashew, coconut, almond milk or tofu), are available for those who are + lactose intolerant, allergic to dairy protein or vegan. +""" + +@bot() +def on_message(message_history: List[Message], state: dict = None): + + # Generate `command` (cohere's model) response + bot_response = Cohere.summarize( + text_input=SYSTEM_PROMPT, + message_history=message_history, # Assuming history is the list of user messages + model="command", + length="long" + ) + + response = { + "data": { + "messages": [ + { + "data_type": "STRING", + "value": bot_response + } + ], + "state": state + }, + "errors": [ + { + "message": "" + } + ] + } + + return { + "status_code": 200, + "response": response + } +``` \ No newline at end of file From 741c8735c29f2d2d6fff2db2263abd4612daf5d8 Mon Sep 17 00:00:00 2001 From: Pravee Singh Date: Mon, 4 Sep 2023 00:58:57 +0530 Subject: [PATCH 18/18] side bar positions updated --- docs/docs/examples/huggingface-bot.md | 2 +- docs/docs/examples/mimicking-bot.md | 2 +- docs/docs/examples/openai-bot.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/docs/examples/huggingface-bot.md b/docs/docs/examples/huggingface-bot.md index e0342afe..8fd205a0 100644 --- a/docs/docs/examples/huggingface-bot.md +++ b/docs/docs/examples/huggingface-bot.md @@ -1,5 +1,5 @@ --- -sidebar_position: 3 +sidebar_position: 4 --- # HuggingFace bot diff --git a/docs/docs/examples/mimicking-bot.md b/docs/docs/examples/mimicking-bot.md index 3a49adda..48a439b5 100644 --- a/docs/docs/examples/mimicking-bot.md +++ b/docs/docs/examples/mimicking-bot.md @@ -1,5 +1,5 @@ --- -sidebar_position: 1 +sidebar_position: 5 --- # Mimicking bot diff --git a/docs/docs/examples/openai-bot.md b/docs/docs/examples/openai-bot.md index a7df7b7c..144d229a 100644 --- a/docs/docs/examples/openai-bot.md +++ b/docs/docs/examples/openai-bot.md @@ -1,5 +1,5 @@ --- -sidebar_position: 2 +sidebar_position: 6 --- # Open AI bot