Giter VIP home page Giter VIP logo

Comments (2)

justinh-rahb avatar justinh-rahb commented on July 18, 2024 1

sweep: retry this on the up-to-date codebase

from chat2gpt.

sweep-ai avatar sweep-ai commented on July 18, 2024

Here's the PR! #46.

⚡ Sweep Free Trial: I used GPT-3.5 to create this ticket. You have 3 GPT-4 tickets left for the month and 2 for the day. For more GPT-4 tickets, visit our payment portal.To get Sweep to recreate this ticket, leave a comment prefixed with "sweep:" or edit the issue.


Step 1: 🔍 Code Search

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I looked at (click to expand). If some file is missing from here, you can mention the path in the ticket description.

chat2gpt/main.py

Lines 126 to 280 in 32ee2b4

return response.json().get("audio_url"), None
else:
return None, response.text
def process_event(request):
try:
event = request.get_json()
event_type = event['type']
user_id = event['user']['name']
if event_type == 'ADDED_TO_SPACE':
return jsonify({'text': 'Hello! I am your Chat bot. How can I assist you today?'})
elif event_type == 'MESSAGE':
message = event['message']
user_message = message['text']
# Check if the bot was mentioned in the room, if so, remove the mention
if 'annotations' in message:
for annotation in message['annotations']:
if annotation['type'] == 'USER_MENTION':
if annotation['userMention']['user']['name'] == event['space']['name']:
user_message = user_message.replace(annotation['userMention']['text'], '').strip()
return handle_message(user_id, user_message)
else:
return jsonify({'text': 'Sorry, I can only process messages and being added to a space.'})
except Exception as e:
print(f"Error processing event: {str(e)}")
return jsonify({'text': 'Sorry, I encountered an error while processing your message.'})
def handle_message(user_id, user_message):
try:
# Check the user input for any policy violations
moderation_result = moderate_content(user_message)
if moderation_result["flagged"]:
return jsonify({'text': 'Sorry, your message does not comply with our content policy. Please refrain from inappropriate content.'})
current_time = datetime.datetime.now()
# Get the AIChat instance for the user, or create a new one
ai_chat = user_sessions.get(user_id)
turn_count = turn_counts.get(user_id, 0)
last_received_time = last_received_times.get(user_id)
# Count the tokens in the user message
num_tokens = num_tokens_from_string(user_message + SYSTEM_PROMPT)
# If the user types '/reset', reset the session
if user_message.strip().lower() == '/reset':
if user_id in user_sessions:
del user_sessions[user_id] # Delete the user's chat session if it exists
turn_count = 0
bot_message = "Your session has been reset. How can I assist you now?"
# Check if the user input starts with /image
elif user_message.strip().lower().startswith('/image'):
prompt = user_message.split('/image', 1)[1].strip()
if not prompt:
return jsonify({'text': 'Please provide a prompt for the image generation. Example: `/image sunset over a beach`.'})
try:
image_resp = openai.Image.create(prompt=prompt, n=1, size=IMAGE_SIZE)
image_url = image_resp["data"][0]["url"]
return jsonify({
'cardsV2': [
{
'cardId': generate_unique_card_id(),
'card': {
'header': {
'title': 'Generated Image',
'subtitle': prompt,
},
'sections': [
{
'widgets': [
{
'image': {
'imageUrl': image_url
}
}
]
}
]
}
}
]
})
except Exception as e:
return jsonify({'text': f"Sorry, I encountered an error generating the image: {str(e)}"})
# Check if the user input starts with /voices
elif user_message.strip().lower() == '/voices':
# Join voice names with commas and spaces for readability
voices_string = ', '.join(voice_names)
return jsonify({'text': f"Available voices: {voices_string}"})
# Check if the user input starts with /tts
elif user_message.strip().lower().startswith('/tts'):
parts = user_message.split(' ')
if len(parts) < 3: # Checking for /tts, voice, and message
return jsonify({'text': 'Please use the format `/tts <voice> <message>`.'})
voice = parts[1].lower()
if voice not in voices_data: # Checking against voices_data now
return jsonify({'text': f"Sorry, I couldn't recognize the voice `{voice}`. Please choose a valid voice."})
prompt = ' '.join(parts[2:])
audio_url, error = text_to_speech(prompt, voice)
if audio_url:
# Return the audio link with alt-text
audio_link = f"<{audio_url}|Click to play audio>"
return jsonify({'text': audio_link})
else:
return jsonify({'text': f"Sorry, I encountered an error generating the audio: {error}"})
# If the message is too large, return an error message
elif num_tokens > MAX_TOKENS_INPUT:
return jsonify({'text': 'Sorry, your message is too large. Please try a shorter message.'})
# If it's not a slash command, handle it normally
else:
if ai_chat is None or turn_count >= MAX_TURNS or (last_received_time is not None and (current_time - last_received_time).total_seconds() > TTL):
if API_URL:
ai_chat = AIChat(api_key=None, api_url=API_URL, system=SYSTEM_PROMPT, params=params)
else:
ai_chat = AIChat(api_key=openai_api_key, system=SYSTEM_PROMPT, model=MODEL_NAME, params=params)
user_sessions[user_id] = ai_chat
turn_count = 0
# Generate the response
response = ai_chat(user_message)
# Ensure the response is less than 4096 characters
if len(response) > 4096:
response = response[:4070] + "<MESSAGE TRUNCATED>" # truncated to leave space for the appended message
# Check the API output for any policy violations
moderation_result = moderate_content(response)
if moderation_result["flagged"]:
return jsonify({'text': 'Sorry, your message does not comply with our content policy. Please refrain from inappropriate content.'})
bot_message = response
# Update the turn count and the last received time
turn_count += 1
turn_counts[user_id] = turn_count
last_received_times[user_id] = current_time
except Exception as e:
print(f"Error calling OpenAI API: {str(e)}")
bot_message = "Sorry, I'm currently unable to generate a response."
return jsonify({'text': bot_message})

chat2gpt/README.md

Lines 1 to 146 in 32ee2b4

[![Python 3.11](https://github.com/RAHB-REALTORS-Association/chat2gpt/actions/workflows/python-3.11.yml/badge.svg)](https://github.com/RAHB-REALTORS-Association/chat2gpt/actions/workflows/python-3.11.yml)
[![GCP Deployment](https://github.com/RAHB-REALTORS-Association/chat2gpt/actions/workflows/gcp-deploy.yml/badge.svg)](https://github.com/RAHB-REALTORS-Association/chat2gpt/actions/workflows/gcp-deploy.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
<br/>
<p align="center">
<img src="https://raw.githubusercontent.com/RAHB-REALTORS-Association/chat2gpt/master/docs/chat2gpt.png" alt="Logo" width="300"/>
</p>
<hr/>
**Chat²GPT** is a [ChatGPT](https://openai.com/chatgpt) chat bot for Google Chat 🤖💬. It's designed to amplify the experience in your Google Chat rooms by offering personalized user sessions for coherent dialogues, a manual reset capability, the power to generate images via OpenAI's [DALL·E 2 API](https://openai.com/dall-e-2), and dynamic interactions through mentions or direct messaging. Moreover, with the integration of Eleven Labs TTS API, Chat²GPT now brings voice interactions, letting users convert textual prompts into audio. User input and text output is moderated with OpenAI's [Moderation API](https://platform.openai.com/docs/guides/moderation).
## 📖 Table of Contents
- [🛠️ Setup](#%EF%B8%8F-setup)
- [🧑‍💻 Usage](#-usage)
- [🛡️ Privacy](#%EF%B8%8F-privacy)
- [Data Practices 📝](#data-practices-)
- [OpenAI and User Awareness ℹ️](#openai-and-user-awareness-%E2%84%B9%EF%B8%8F)
- [🌐 Community](#-community)
- [Contributing 👥🤝](#contributing-)
- [Reporting Bugs 🐛📝](#reporting-bugs-)
- [📄 License](#-license)
## 🛠️ Setup
[![Run on Google Cloud](https://deploy.cloud.run/button.svg)](https://deploy.cloud.run?git_repo=https://github.com/RAHB-REALTORS-Association/chat2gpt)
This bot is intended to be deployed on Google Cloud Functions and its continuous deployment pipeline is managed using GitHub Actions.
Follow these steps to setup your Chat²GPT bot:
**1. Clone the Repository 📁**
Clone this repository to your local machine using the command:
```bash
git clone https://github.com/RAHB-REALTORS-Association/chat2gpt.git
```
**2. Create Google Cloud Project ☁️**
Create a new project in your Google Cloud Console or select an existing one.
**3. Create a Service Account and JSON Key 📑**
In the Google Cloud Console:
- Navigate to "IAM & Admin" > "Service Accounts".
- Click on "Create Service Account".
- Give your service account a name and description, then click "Create and Continue".
- Grant this service account the "Cloud Functions Developer" role (or any role that has the "cloudfunctions.functions.create" permission) to allow it to create and update Cloud
Functions. If you plan to use other Google Cloud services, you might need to grant additional roles.
- Click "Continue" and then "Done".
- Click on the newly created service account from the list.
- Go to the "Keys" tab, click "Add Key" and select "Create new key".
- Choose "JSON" as the key type and click "Create". The JSON key file will be automatically downloaded to your local machine. This file contains the service account credentials that
will be used by GitHub Actions for deploying your function. Be careful with this file as it provides admin access to your Google Cloud project.
**4. Set GitHub Secrets 🔒**
In your GitHub repository:
- Navigate to "Settings" > "Secrets" > "New repository secret".
- Add the following secrets:
- `GCP_PROJECT_ID`: Your Google Cloud Project identifier.
- `GCP_FUNCTION`: Your desired Google Cloud Function name.
- `GCP_REGION`: Your chosen Google Cloud region.
- `GCP_SA_KEY`: The entire JSON key file content that was downloaded in the previous step, encoded as base64.
- `OPENAI_API_KEY`: Your OpenAI API key.
- `MODEL_NAME`: The name of the OpenAI model you're using. Default: "gpt-3.5-turbo".
- `SYSTEM_PROMPT`: The system prompt to use for the OpenAI API.
- `MAX_TURNS`: This sets the maximum number of exchanges the bot remembers in a user session before resetting. Default: 10 exchanges.
- `TTL`: This sets the duration (in seconds) a user session stays active from the last received message before it resets. Default: 600 seconds (10 minutes).
- `MAX_TOKENS_INPUT`: This sets the maximum number of tokens that can be sent. Default: 1000 tokens.
- `MAX_TOKENS_OUTPUT`: This sets the maximum number of tokens that can be received. Default: 1000 tokens.
- `TEMPERATURE`: This sets the temperature for the OpenAI API. Default: 0.8.
- `IMAGE_SIZE`: This sets the image size for the DALL-E API. Default: "512x512".
- `API_URL`: This sets the API endpoint for the chat completions API. Default: "https://api.openai.com/v1/chat/completions".
- `ELEVENLABS_API_KEY`: Your Eleven Labs API key. Can be disabled by omitting this secret.
- `ELEVENLABS_MODEL_NAME`: Eleven Labs model you're using. Default: "eleven_monolingual_v1".
**5. GitHub Actions 🚀**
The GitHub Actions workflow is configured to automatically deploy the bot to Google Cloud Functions whenever changes are pushed to the main branch of the repository.
**6. Configure Bot Access 🤝**
- Navigate to https://chat.google.com/u/0/botmanagement.
- Click on the bot you created.
- Under "Functionality", select "Bot works in...".
- Select "Spaces any user can create".
- Click "Save".
Now, your bot can be added to any room within your Google Workspace.
## 🧑‍💻 Usage
- **Dynamic Interactions:** Chat²GPT is attentive to its surroundings. You can invoke it in chat rooms by directly mentioning it using `@botname`. Alternatively, for more private interactions or queries, you can send a direct message to the bot.
- **Interactive Sessions:** This bot remembers multiple rounds of a conversation per user, creating an illusion of continuous dialogue. It can even reference past questions or answers, mimicking a natural conversation flow.
- **Session Management:** To maintain efficient performance, each conversation is limited by a configurable setting, recommended at 5-10 turns. Moreover, the bot keeps an eye on the time since the last message, auto-resetting the session if a set time limit is surpassed. And if needed, users can manually reset their own session anytime with the `/reset` command.
- **Image Generation:** Want to visualize an idea? Use the `/image <prompt>` command. Based on the given prompt, which can range from a word to a paragraph, the bot leverages OpenAI's DALL·E 2 API to generate a relevant image.
- **Text-to-Speech (TTS):** Utilize the power of Eleven Labs TTS API with the `/tts <voice> <prompt>` command. This command will return a voice response based on the given prompt in the specified voice. To see a list of available voices, use the `/voices` command.
- **Optimized Performance:** We prioritize a smooth experience. Before processing any message, the bot checks its size by counting its tokens. If found too lengthy, an error message suggests the user to condense their message. This ensures uninterrupted bot interactions without straining the system.
Remember, Chat²GPT is flexible, suitable for deployment on Google Cloud, FaaS (Function as a Service), or PaaS (Platform as a Service) environments, ensuring it's a perfect fit for all your Google Chat endeavors.
## 🛡️ Privacy
### Data Practices 📝
- **Ephemeral Conversations:** Chat²GPT doesn't store or retain conversation history. Every session is temporary, ending when a conversation concludes or times out.
- **Reactive Responses:** The bot only reacts to direct prompts, such as @mentions or direct messages, and doesn't "read the room".
- **Anonymous Sessions:** Users are tracked using anonymous ID numbers solely for session consistency. These IDs are cleared with each app redeployment.
### AI APIs and User Awareness ℹ️
- **OpenAI's Commitment:** We use OpenAI's APIs, which, as per OpenAI's policy, don't use user inputs for model training. More details are on [OpenAI's official site](https://openai.com/policies/api-data-usage-policies).
- **Eleven Labs' Commitment:** We use Eleven Labs' APIs, which, as per Eleven Labs' policy, don't use user inputs for model training. More details are on [Eleven Labs' official site](https://www.eleven-labs.com/en/legal/).
- **User Awareness:** Discussing sensitive topics? Exercise caution, especially in group settings. Chat²GPT doesn't log conversations, but your organization or platform might.
## 🌐 Community
### Contributing 👥🤝
Contributions of any kind are very welcome, and would be much appreciated. 🙏
For Code of Conduct, see [Contributor Covenant](https://www.contributor-covenant.org/version/2/1/code_of_conduct/).
To get started, fork the repo, make your changes, add, commit and push the code, then come back here to open a pull request. If you're new to GitHub or open source, [this guide](https://www.freecodecamp.org/news/how-to-make-your-first-pull-request-on-github-3#let-s-make-our-first-pull-request-) or the [git docs](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request) may help you get started, but feel free to reach out if you need any support.
[![Submit a PR](https://img.shields.io/badge/Submit_a_PR-GitHub-%23060606?style=for-the-badge&logo=github&logoColor=fff)](https://github.com/RAHB-REALTORS-Association/chat2gpt/compare)
### Reporting Bugs 🐛📝
If you've found something that doesn't work as it should, or would like to suggest a new feature, then go ahead and raise an issue on GitHub.
For bugs, please outline the steps needed to reproduce, and include relevant info like system info and resulting logs.
[![Raise an Issue](https://img.shields.io/badge/Raise_an_Issue-GitHub-%23060606?style=for-the-badge&logo=github&logoColor=fff)](https://github.com/RAHB-REALTORS-Association/chat2gpt/issues/new/choose)
## 📄 License
This project is open sourced under the MIT license. See the [LICENSE](LICENSE) file for more info.

I also found the following external resources that might be helpful:

Summaries of links found in the content:

#42 (comment)_:

The page is a pull request on GitHub titled "Integrate Eleven Labs TTS API" by the user justinh-rahb. The pull request aims to seamlessly integrate the Eleven Labs Text-to-Speech API into the chat2gpt application. The integration allows users to convert specific text into audio using various voices provided by the API. The pull request introduces two new slash commands: /tts and /voices. The /tts command allows users to specify a voice name and a message, and the bot will return an audio link synthesized based on the chosen voice. The /voices command lists out all available voice names to aid users in selecting a voice for the /tts command. The pull request also mentions the addition of variables for chat TEMPERATURE and IMAGE_SIZE for DALL-E generation. However, there is a comment by the user justinh-rahb stating that the feature needs more work due to the way the audio data is returned by the API. Storing and serving files is beyond the scope of the project, so unless a simple method is found, the feature branch may be abandoned.


Step 2: 🧐 Snippet Analysis

From looking through the relevant snippets, I decided to make the following modifications:

File Path Proposed Changes
main.py In the function text_to_speech(prompt, voice), modify the code to handle the binary data response from the TTS API. Convert the binary data into an audio file. Then, upload the audio file to a cloud storage service and retrieve a public URL for the file. Return this URL instead of the binary data.

Step 3: 📝 Planning

I have created a plan for writing the pull request. I am now working my plan and coding the required changes to address this issue. Here is the planned pull request:

Fix TTS audio binary data issue
sweep/fix-tts-audio-binary-data

Description

This pull request addresses the issue of the Text-to-Speech (TTS) audio data being returned as binary data instead of a URL. The current implementation of the chat bot does not have a mechanism to handle binary data, store it as an audio file, and serve it as a URL. This PR modifies the code to handle the binary data response from the TTS API, convert it into an audio file, store the file in a cloud storage service, and return a public URL for the file.

Summary of Changes

  • Modified the text_to_speech(prompt, voice) function in main.py to handle the binary data response from the TTS API.
  • Implemented code to convert the binary data into an audio file.
  • Integrated a cloud storage service (e.g., Google Cloud Storage or AWS S3) to store the audio file.
  • Retrieved a public URL for the stored audio file.
  • Updated the text_to_speech(prompt, voice) function to return the URL of the audio file instead of the binary data.

Please review and merge this pull request to resolve the issue of TTS audio data being returned as binary data.


Step 4: ⌨️ Coding

File Instructions Progress
main.py In the function text_to_speech(prompt, voice), modify the code to handle the binary data response from the TTS API. Convert the binary data into an audio file. Then, upload the audio file to a cloud storage service and retrieve a public URL for the file. Return this URL instead of the binary data. ✅ Commit ffbf0ee

Step 5: 🔁 Code Review

Here are the my self-reviews of my changes at sweep/fix-tts-audio-binary-data.

Here is the 1st review

Thanks for your contribution. Here are a few changes that need to be made:

  • In main.py on lines 1-8, you've added imports for 'wave' and 'boto3'. Please add these dependencies to the 'requirements.txt' file to ensure they are installed when setting up the project.

  • In main.py on lines 123-133, you've modified the 'text_to_speech' function to convert the API response into an audio file and upload it to an S3 bucket. However, there are a few potential issues:

    • The 's3' client is created without any arguments. This may not work if the AWS credentials are not configured correctly in the environment. Please ensure that the AWS credentials are being handled correctly.

    • The bucket name and the file name are hardcoded. This could lead to conflicts if multiple requests are made at the same time. Consider generating a unique file name for each request to avoid this issue.

Please make these changes and update the pull request. If you need any help, feel free to ask.

I finished incorporating these changes.


To recreate the pull request, leave a comment prefixed with "sweep:" or edit the issue.
Join Our Discord

from chat2gpt.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.