Skip to content

Commit

Permalink
Merge pull request #70 from jahanvir/main
Browse files Browse the repository at this point in the history
Added Support for Google PaLM API
  • Loading branch information
sammyCofactory authored Sep 7, 2023
2 parents 52506a9 + 891bdb6 commit c6c7d73
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
OPENAI_API_KEY=
HUGGINGFACEHUB_API_TOKEN=
PALM_API_KEY=
49 changes: 49 additions & 0 deletions docs/docs/examples/palmai-bot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
sidebar_position: 2
---

# Google PaLM AI bot

This bot makes an API call to PaLMAI and processes the user input. It uses PaLM Chat.

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

# Load your PALM API key
# PALMAI.api_key = ""
# or from environment variable:
PalmAI.api_key = os.getenv("PALM_API_KEY")

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

bot_response = PalmAI.generate(
message_history=message_history, # Assuming history is the list of user messages
)

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

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

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

# Load your PALM API key
# PALMAI.api_key = ""
# or from environment variable:
PalmAI.api_key = os.getenv("PALM_API_KEY")


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

bot_response = PalmAI.generate(
message_history=message_history, # Assuming history is the list of user messages
)

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
29 changes: 28 additions & 1 deletion textbase/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import openai
import google.generativeai as palm
import requests
import time
import typing
Expand Down Expand Up @@ -143,4 +144,30 @@ 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

@classmethod
def generate(
cls,
message_history: list[Message],
):

assert cls.api_key is not None, "Palm API key is not set."
palm.configure(api_key=cls.api_key)

filtered_messages = []

for message in message_history:
#list of all the contents inside a single message
contents = extract_content_values(message)
if contents:
filtered_messages.extend(contents)

#send request to Google Palm chat API
response = palm.chat(messages=filtered_messages)

print(response)
return response.last

0 comments on commit c6c7d73

Please sign in to comment.