Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add Support for Google Palm AI API #123

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions docs/docs/examples/palmai-bot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
sidebar_position: 2
---

# Palm AI bot

This bot makes an API call to PalmAI API and processes the user input. It uses by default chat-bison, which is optimised for chat purposes.

```py
import os
from textbase import bot, Message
from textbase.models import PalmAI
from typing import List

# Load your PalmAI API key
PalmAI.api_key = ""
# or from environment variable
PalmAI.api_key = os.getenv("PALMAI_API_KEY")

# Prompt for GPT-3.5 Turbo
SYSTEM_PROMPT = """You are chatting with an AI. There are no specific prefixes for responses, so you can ask or talk about anything you like.
You will respond in a natural, conversational manner. Feel free to start the conversation with any question or topic, and let's have a pleasant chat!
"""

@bot()
def on_message(message_history: List[Message], state: dict = None):

bot_response = PalmAI.generate(
system_prompt=SYSTEM_PROMPT,
message_history=message_history
)

response = {
"data": {
"messages": [
{
"data_type": "STRING",
"value": bot_response
}
],
"state": state
},
"errors": [
{
"message": ""
}
]
}

return {
"status_code": 200,
"response": response
}

```
44 changes: 44 additions & 0 deletions examples/palmai-bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
from textbase import bot, Message
from textbase.models import PalmAI
from typing import List

# Load your PalmAI API key
PalmAI.api_key = ""
# or from environment variable
PalmAI.api_key = os.getenv("PALMAI_API_KEY")

# Prompt for GPT-3.5 Turbo
SYSTEM_PROMPT = """You are chatting with an AI. There are no specific prefixes for responses, so you can ask or talk about anything you like.
You will respond in a natural, conversational manner. Feel free to start the conversation with any question or topic, and let's have a pleasant chat!
"""

@bot()
def on_message(message_history: List[Message], state: dict = None):

bot_response = PalmAI.generate(
system_prompt=SYSTEM_PROMPT,
message_history=message_history
)

response = {
"data": {
"messages": [
{
"data_type": "STRING",
"value": bot_response
}
],
"state": state
},
"errors": [
{
"message": ""
}
]
}

return {
"status_code": 200,
"response": response
}
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tabulate = "^0.9.0"
functions-framework = "^3.4.0"
yaspin = "^3.0.0"
pydantic = "^2.3.0"
google-generativeai = "^0.1.0"

[build-system]
requires = ["poetry-core"]
Expand Down
39 changes: 38 additions & 1 deletion textbase/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import traceback

from textbase import Message
import google.generativeai as palm

# Return list of values of content.
def get_contents(message: Message, data_type: str):
Expand Down Expand Up @@ -143,4 +144,40 @@ def generate(
data = json.loads(response.text) # parse the JSON data into a dictionary
message = data['message']

return message
return message

class PalmAI:
api_key = None
response = None
client = None

@classmethod
def generate(
cls,
system_prompt: str,
message_history: list[Message],
model="models/chat-bison-001",
examples: typing.List[typing.Tuple] = None,
temperature = 0.7,
):
assert cls.api_key is not None, "PalmAI API key is not set."
if cls.client == None:
palm.configure(api_key=cls.api_key)
cls.client = palm

most_recent_message = get_contents(message_history[-1], "STRING")[0]["content"]

if cls.response == None:
cls.response = cls.client.chat(
context = system_prompt,
model = model,
temperature=temperature,
messages=most_recent_message
)

else:
cls.response = cls.response.reply(
message=most_recent_message
)

return cls.response.last