Skip to content

Commit

Permalink
Merge branch 'main' into file_search
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Apr 20, 2024
2 parents 09e3dd4 + 512628b commit b5fe4b6
Show file tree
Hide file tree
Showing 22 changed files with 1,082 additions and 241 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,31 @@ jobs:
- run: python -m pip install -e .[lint]
- run: ${{ matrix.lint-command }}


dist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- run: python -m pip install --upgrade pip build twine
- run: python -m build --sdist --wheel
- run: python -m twine check dist/*
- uses: actions/upload-artifact@v4
with:
path: dist/*

pytest:
runs-on: ubuntu-latest
services:
redis:
image: redis
ports:
- 6379:6379
options: --entrypoint redis-server
env:
REDIS_URL: redis:///
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Release

on:
release:
types: [published]

jobs:

release-build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: "3.x"
- run: python -m pip install --upgrade pip build
- run: python -m build --sdist --wheel
- uses: actions/upload-artifact@v4
with:
name: release-dists
path: dist/

pypi-publish:
runs-on: ubuntu-latest
needs:
- release-build
permissions:
id-token: write

steps:
- uses: actions/download-artifact@v4
with:
name: release-dists
path: dist/
- uses: pypa/gh-action-pypi-publish@release/v1
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[![PyPi Version](https://img.shields.io/pypi/v/opensam.svg)](https://pypi.python.org/pypi/opensam/)
[![Test Coverage](https://codecov.io/gh/voiio/sam/branch/main/graph/badge.svg)](https://codecov.io/gh/voiio/sam)
[![GitHub License](https://img.shields.io/github/license/voiio/sam)](https://raw.githubusercontent.com/voiio/sam/master/LICENSE)

# Sam – cuz your company is nothing without Sam

![meme](https://repository-images.githubusercontent.com/726003479/24d020ac-3ac5-401c-beae-7a6103c4e7ae)
Expand Down
47 changes: 39 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,54 @@
[build-system]
requires = ["flit_core>=3.2", "flit_scm", "wheel"]
build-backend = "flit_scm:buildapi"

[project]
name = "Sam"
name = "OpenSam"
authors = [
{ name = "Johannes Maron", email = "johannes@maron.family" },
]
readme = "README.md"
license = { file = "LICENSE" }
keywords = ["GPT", "AI", "Slack", "OpenAI", "bot"]
dynamic = ["version", "description"]

classifiers = [
"Development Status :: 2 - Pre-Alpha",
"Programming Language :: Python",
"Environment :: Web Environment",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Topic :: Software Development",
"Topic :: Communications :: Chat",
"Topic :: Internet :: WWW/HTTP",
"Topic :: Communications :: Email",
"Topic :: Games/Entertainment :: Role-Playing",
"Topic :: Multimedia :: Sound/Audio :: Conversion",
"Topic :: Multimedia :: Sound/Audio :: Speech",
"Topic :: Office/Business",
"Topic :: Office/Business :: Groupware",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Topic :: Scientific/Engineering :: Human Machine Interfaces",
"Topic :: Text Processing :: Markup :: Markdown",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
requires-python = ">=3.11"

dependencies = [
"aiohttp",
"click",
"certifi",
"markdownify",
"redis",
"requests",
"slack-bolt",
"openai>=1.21.0",
"pyyaml",
"algoliasearch",
"sentry-sdk",
]

Expand All @@ -27,8 +58,9 @@ sam = "sam.__main__:cli"
[project.optional-dependencies]
test = [
"pytest",
"pytest-cov",
"pytest-asyncio",
"pytest-cov",
"pytest-env",
]
lint = [
"bandit==1.7.8",
Expand All @@ -41,10 +73,6 @@ lint = [
Project-URL = "https://github.com/voiio/Sam"
Changelog = "https://github.com/voiio/Sam/releases"

[build-system]
requires = ["flit-scm", "wheel"]
build-backend = "flit_scm:buildapi"

[tool.flit.module]
name = "sam"

Expand All @@ -54,9 +82,12 @@ fallback_version = "1.0.0"

[tool.pytest.ini_options]
minversion = "6.0"
addopts = "--cov --tb=short -rxs"
addopts = "--tb=short -rxs"
testpaths = ["tests"]

[tool.pytest_env]
GITHUB_REPOS = 'voiio/sam'

[tool.coverage.run]
source = ["sam"]

Expand Down
78 changes: 66 additions & 12 deletions sam/__main__.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,98 @@
import asyncio
import logging
import os
import sys

import click
import openai
import sentry_sdk
from sentry_sdk.integrations.asyncio import AsyncioIntegration

from . import config

sentry_sdk.init(config.SENTRY_DSN)
sentry_sdk.init(
dsn=config.SENTRY_DSN,
enable_tracing=True,
integrations=[
AsyncioIntegration(),
],
)


@click.group()
@click.option("-v", "--verbose", is_flag=True, help="Enables verbose mode.")
def cli(verbose):
def cli():
"""Sam – cuz your company is nothing with Sam."""
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(message)s"))
logger = logging.getLogger("sam")
logger.addHandler(handler)
logger.setLevel(logging.DEBUG if verbose else logging.INFO)


@cli.group(chain=True)
def run():
"""Run an assistent bot, currently only Slack is supported."""
@click.option("-v", "--verbose", is_flag=True, help="Enables verbose mode.")
def run(verbose):
"""Run an assistent bot."""
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(
logging.Formatter("%(asctime)s %(levelname)7s %(name)s - %(message)s")
)
logging.basicConfig(
handlers=[handler], level=logging.DEBUG if verbose else logging.INFO
)


@run.command()
def slack():
"""Run the Slack bot demon."""
from slack_bolt.adapter.socket_mode.async_handler import AsyncSocketModeHandler

from .slack import app
from .slack import get_app

loop = asyncio.get_event_loop()

loop.run_until_complete(
AsyncSocketModeHandler(app, config.SLACK_APP_TOKEN).start_async()
AsyncSocketModeHandler(get_app(), config.SLACK_APP_TOKEN).start_async()
)


@cli.group(chain=True)
@click.option("-v", "--verbose", is_flag=True, help="Enables verbose mode.")
def assistants(verbose):
"""Manage OpenAI assistants."""
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter("%(message)s"))
logging.basicConfig(
handlers=[handler], level=logging.DEBUG if verbose else logging.INFO
)


@assistants.command(name="list")
def _list():
"""List all assistants configured in your project."""
assistant_list = list(config.load_assistants())
for assistant_config in assistant_list:
click.echo(
f"{assistant_config.name} ({assistant_config.project}): {assistant_config.assistant_id}"
)
if not assistant_list:
click.echo("No assistants configured.")


@assistants.command()
def upload():
"""Compile and upload all assistants system prompts to OpenAI."""
assistant_list = list(config.load_assistants())
for assistant_config in assistant_list:
click.echo(f"Uploading {assistant_config.name}...", nl=False)
project_api_key_name = (
f"OPENAI_{assistant_config.project.replace('-', '_').upper()}_API_KEY"
)
project_api_key = os.getenv(project_api_key_name)
with openai.OpenAI(api_key=project_api_key) as client:
client.beta.assistants.update(
assistant_id=assistant_config.assistant_id,
instructions=assistant_config.system_prompt,
)
click.echo(" Done!")
if not assistant_list:
click.echo("No assistants configured.")


if __name__ == "__main__":
cli()
Loading

0 comments on commit b5fe4b6

Please sign in to comment.