Skip to content

Commit

Permalink
test(talkwave): increasing unit test coverage #4 and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Apr 12, 2023
1 parent 359759b commit fd3137d
Show file tree
Hide file tree
Showing 20 changed files with 336 additions and 69 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ WORKDIR /app
COPY requirements.txt .

# Install the dependencies with a specific version tag
RUN pip install --no-cache-dir -r requirements.txt==0.0.4
RUN pip install --no-cache-dir -r requirements.txt==0.0.5

# Copy the rest of the application code into the container
COPY . .
Expand Down
15 changes: 15 additions & 0 deletions README.fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ OPENAI_API_KEY="your_api_key_here"

## Utilisation 🚀

### Interface de ligne de commande

Pour utiliser TalkWave, accédez au répertoire du projet dans votre terminal et exécutez la commande suivante:

```bash
Expand All @@ -68,6 +70,19 @@ Pour plus d'informations sur les options disponibles, exécutez:
python talkwave --help
```

### Interface Web

Pour utiliser l'interface web, accédez au répertoire du projet dans
votre terminal et exécutez la commande suivante :

```bash
python talkwave/frontend.py
```

Ensuite, ouvrez votre navigateur et rendez-vous sur
<http://127.0.0.1:5000>. Cela ouvrira l'interface web de TalkWave, que
vous pourrez utiliser pour générer des réponses.

## Structure des fichiers 📁

```bash
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pip install openai tabulate python-dotenv
1. Clone the TalkWave repository:

```bash
git clone https://github.com/yourusername/talkwave.git
git clone https://github.com/sebastienrousseau/talkwave.git
```

1. Add your OpenAI API key to a `.env` file in the project directory:
Expand All @@ -53,6 +53,8 @@ OPENAI_API_KEY="your_api_key_here"

## Usage 🚀

### Command Line Interface

To use TalkWave, navigate to the project directory in your terminal and
run the following command:

Expand All @@ -74,6 +76,19 @@ For more information on the available options, run:
python talkwave --help
```

### Web Frontend

To use the web frontend, navigate to the project directory in your
terminal and run the following command:

```bash
python talkwave/frontend.py
```

Then, open your browser and navigate to `http://127.0.0.1:5000`. This
will open the TalkWave web frontend, which you can use to generate
responses.

## File Structure 📁

```bash
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "talkwave"
version = "0.0.4"
version = "0.0.5"
description = "TalkWave is an AI chatbot for developers written in Python."
authors = ["Sebastien Rousseau <sebastian.rousseau at gmail.com>"]
license = "Apache Software License"
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = talkwave
version = 0.0.4
version = 0.0.5
author = Sebastian Rousseau
author_email = sebastian.rousseau@gmail.com
description = TalkWave is an AI chatbot for developers written in Python.
Expand Down
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"pytest>=7.3.0",
]

VERSION = '0.0.4'
VERSION = '0.0.5'
URL = 'https://github.com/sebastienrousseau/talkwave'

setup(
Expand Down Expand Up @@ -68,7 +68,9 @@
'natural language processing '
'OpenAI GPT-3 GPT3 GPT python'
),
packages=['talkwave'],
packages=[

],
install_requires=DEPENDENCIES,
tests_require=TEST_DEPENDENCIES,
python_requires='>=3.9',
Expand Down
38 changes: 22 additions & 16 deletions talkwave/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,28 @@
# TalkWave
TalkWave is a Python package that provides an easy-to-use command-line
interface for interacting with OpenAI's GPT-powered language models.
TalkWave is an AI chatbot for developers written in Python. It features
a simple HTML frontend and is designed to be accessible across various
browsers and devices. TalkWave supports asynchronous operations and can
handle multiple requests simultaneously.
## Features
- Supports multiple GPT models for generating responses.
- Accepts a range of parameters to customize the response, such as max
tokens, temperature, and stopping conditions.
- Stores responses in log files, JSON, and Markdown formats for easy
- [x] Accepts a range of parameters to customize the response, such as
max tokens, temperature, and stopping conditions.
- [x] Accessible design for cross-browser and device compatibility
(Chrome, Firefox, Safari, Edge, and mobile).
- [x] Accurately limits billing with limits and ID binding to prevent
exceeding API limits and incurring charges.
- [x] Implements rate limiting functionality to prevent exceeding API
limits and incurring charges.
- [x] Plain Python implementation with a limited number of dependencies
for easy installation and use.
- [x] Stores responses in log files, JSON, and Markdown formats for easy
analysis and sharing.
- Provides rate limiting functionality to prevent exceeding API limits
and incurring charges.
- [x] Supports multiple GPT models for generating responses, including
`gpt-3.5-turbo`,`text-davinci-002`,`text-curie-001`,`text-babbage-001`,
`text-ada-001`.
## Requirements
Expand All @@ -47,10 +57,10 @@
1. Clone the TalkWave repository:
```bash
git clone https://github.com/yourusername/talkwave.git
git clone https://github.com/sebastienrousseau/talkwave.git
```
3. Add your OpenAI API key to a `.env` file in the project directory:
1. Add your OpenAI API key to a `.env` file in the project directory:
```bash
OPENAI_API_KEY="your_api_key_here"
Expand All @@ -76,7 +86,8 @@
-T 0.5\n
-u "test@test.com"\n
-r 5\n
-s "."
-s\n
-o "json"\n
```
For more information on the available options, run:
Expand All @@ -95,8 +106,3 @@
- [MIT license](https://opensource.org/licenses/MIT)
"""

"""The Python talkwave module."""
__all__ = ["__version__"]

__version__ = "0.0.4"
18 changes: 11 additions & 7 deletions talkwave/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@

"""
Enables use of Python TalkWave as a "main" function (i.e.
"python3 -m talkwave").
"python3 talkwave").
This allows using TalkWave with third-party libraries without modifying
their code.
"""
# '

from core import main
from parse import parse_args
from tabulate import tabulate
from utils.parse import parse_args
import os
import sys

from __version__ import (
__description__,
__title__,
__version__,
)

# set the directory where the data is stored, value is a string from an
# environment variable "DIR_PATH" (required).
Expand All @@ -36,11 +39,12 @@

if __name__ == "__main__":
# Set the title and description of the program
title = "TalkWave 🐍 (v0.0.4)"
description = "An AI chatbot for developers"
title = __title__ + " (v" + __version__ + ")"
description = __description__

# Print the title and description of the program
title_table = tabulate([[title], [description]], tablefmt="rounded_grid")
title_table = tabulate([[title], [description]],
tablefmt="rounded_grid")
print()
print(title_table)
print()
Expand Down
28 changes: 28 additions & 0 deletions talkwave/__version__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (C) 2023 Sebastien Rousseau.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__ = "Sebastien Rousseau"
__author_email__ = "sebastian.rousseau@gmail.com"
__copyright__ = "Copyright (C) 2023 Sebastien Rousseau."
__description__ = "An AI chatbot for developers written in Python."
__license__ = "Apache License, Version 2.0."
__logo__ = (
"https://raw.githubusercontent.com/"
"sebastienrousseau/vault/main/assets/"
"talkwave/icon/ico-talkwave.svg"
)
__title__ = "TalkWave 🐍"
__url__ = "https://talkwave.co/"
__version__ = "0.0.5"
59 changes: 43 additions & 16 deletions talkwave/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,31 @@
#
# '
from datetime import datetime
from utils.curl import send_request

try:
from talkwave.__version__ import (
__logo__,
__title__,
__version__,
)
except ModuleNotFoundError:
from __version__ import (
__logo__,
__title__,
__version__,
)


import dotenv
import json
import os
import sys
import sqlite3
import sys

# set the timeout for the API call to a string value from an environment
timeout = os.environ.get('TIMEOUT', '90')
try:
from talkwave.curl import send_request
except ModuleNotFoundError:
from curl import send_request

# set the directory where the data is stored, value is a string from an
# environment variable "DATA_DIR" (required).
Expand All @@ -40,6 +56,8 @@
Write the response to a log file.
'''

data_dir = "data" # Define the data directory or pass it as an argument


def write_response_to_log_file(response, prompt, timestamp):
"""
Expand All @@ -63,7 +81,14 @@ def write_response_to_log_file(response, prompt, timestamp):
database = ["db", "sqlite", "sqlite3", "db3", "s3db", "sl3"]
markdown = ["md", "markdown"]
html = ["html", "htm"]
VERSION = '0.0.4'
# pdf = ["pdf"]
# image = ["png", "jpg", "jpeg", "gif", "bmp", "tiff", "tif"]


# Function Name: write_response_to_file
'''
Write the response to a file.
'''


def write_response_to_file(
Expand Down Expand Up @@ -104,9 +129,9 @@ def write_response_to_file(
""")
f.write("""
<link rel=\"stylesheet\"
href=\"https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css\"
integrity=\"sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65\"
crossorigin=\"anonymous\" />\n""")
href=\"https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css\"
integrity=\"sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65\"
crossorigin=\"anonymous\" />\n""")
f.write(
"""
<style>
Expand All @@ -116,7 +141,7 @@ def write_response_to_file(
}
</style>\n
""")
f.write("<title>TalkWave 🐍 Log Analysis</title>\n")
f.write("<title>"+__title__+"</title>\n")
f.write("</head>\n")
f.write("<body>\n")
f.write(
Expand All @@ -130,12 +155,11 @@ def write_response_to_file(
"""
<h1 class=\"d-flex align-items-center fs-4 text-white mb-0\">
""")
f.write("""
<img src=\"https://raw.githubusercontent.com/sebastienrousseau/vault/
main/assets/talkwave/icon/ico-talkwave.svg\"
width=\"33\"
height=\"33\"
class=\"me-3\" alt=\"TalkWave\" />TalkWave</h1>""")
f.write(f"""
<img src="{__logo__}"
width="33"
height="33"
class="me-3" alt="{__title__}" />{__title__}</h1>""")
f.write("</div>")
f.write("""
<div class=\"container d-flex flex-wrap justify-content-end\">
Expand Down Expand Up @@ -185,7 +209,7 @@ def write_response_to_file(
os.path.join(data_dir, f"{timestamp}_log.md"),
"a"
) as f:
f.write(f"# TalkWave 🐍 (v{VERSION})\n\n")
f.write(f"# {__title__} (v{__version__})\n\n")
f.write(f"## {timestamp}\n\n")
f.write(f"```bash\n{prompt}\n```\n\n")
f.write(f"```bash\n{response}\n```\n")
Expand Down Expand Up @@ -261,6 +285,9 @@ def main(
print("Invalid model")
sys.exit()

# Create the timestamp
timestamp = datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")

json_resp = send_request(
key,
model_str,
Expand Down
9 changes: 7 additions & 2 deletions talkwave/utils/curl.py → talkwave/curl.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import os
import time
import requests

# set the timeout for the API call to 10 seconds
timeout = 10
# Set the timeout for the API call to a string value from an environment
# Keep the default value as a string
timeout_str = os.environ.get('TIMEOUT', '90')

# Convert the timeout value to an integer or float
timeout = int(timeout_str)

# Function Name: request

Expand Down
File renamed without changes.
File renamed without changes.
Empty file removed talkwave/utils/__init__.py
Empty file.
9 changes: 0 additions & 9 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
import sys
import os

# Add the path to the 'utils' directory to sys.path
utils_path = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', 'talkwave', 'utils'))
if not os.path.isdir(utils_path):
raise ValueError(f"Cannot find 'utils' directory at {utils_path}")
sys.path.insert(0, utils_path)
Loading

0 comments on commit fd3137d

Please sign in to comment.