mirror of
https://github.com/NovaOSS/nova-python.git
synced 2024-11-25 20:33:59 +01:00
Compare commits
4 commits
3cbaaf7d54
...
59b7c6073a
Author | SHA1 | Date | |
---|---|---|---|
59b7c6073a | |||
4ca3608cd9 | |||
b232fa533c | |||
fe5709275e |
168
.gitignore
vendored
168
.gitignore
vendored
|
@ -1,160 +1,98 @@
|
||||||
|
/target
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
.pytest_cache/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
*$py.class
|
|
||||||
|
|
||||||
# C extensions
|
# C extensions
|
||||||
*.so
|
*.so
|
||||||
|
|
||||||
# Distribution / packaging
|
# Distribution / packaging
|
||||||
.Python
|
.Python
|
||||||
|
.venv/
|
||||||
|
env/
|
||||||
|
bin/
|
||||||
build/
|
build/
|
||||||
develop-eggs/
|
develop-eggs/
|
||||||
dist/
|
dist/
|
||||||
downloads/
|
|
||||||
eggs/
|
eggs/
|
||||||
.eggs/
|
|
||||||
lib/
|
lib/
|
||||||
lib64/
|
lib64/
|
||||||
parts/
|
parts/
|
||||||
sdist/
|
sdist/
|
||||||
var/
|
var/
|
||||||
wheels/
|
include/
|
||||||
share/python-wheels/
|
man/
|
||||||
|
venv/
|
||||||
*.egg-info/
|
*.egg-info/
|
||||||
.installed.cfg
|
.installed.cfg
|
||||||
*.egg
|
*.egg
|
||||||
MANIFEST
|
|
||||||
|
|
||||||
# PyInstaller
|
|
||||||
# Usually these files are written by a python script from a template
|
|
||||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
||||||
*.manifest
|
|
||||||
*.spec
|
|
||||||
|
|
||||||
# Installer logs
|
# Installer logs
|
||||||
pip-log.txt
|
pip-log.txt
|
||||||
pip-delete-this-directory.txt
|
pip-delete-this-directory.txt
|
||||||
|
pip-selfcheck.json
|
||||||
|
|
||||||
# Unit test / coverage reports
|
# Unit test / coverage reports
|
||||||
htmlcov/
|
htmlcov/
|
||||||
.tox/
|
.tox/
|
||||||
.nox/
|
|
||||||
.coverage
|
.coverage
|
||||||
.coverage.*
|
|
||||||
.cache
|
.cache
|
||||||
nosetests.xml
|
nosetests.xml
|
||||||
coverage.xml
|
coverage.xml
|
||||||
*.cover
|
|
||||||
*.py,cover
|
|
||||||
.hypothesis/
|
|
||||||
.pytest_cache/
|
|
||||||
cover/
|
|
||||||
|
|
||||||
# Translations
|
# Translations
|
||||||
*.mo
|
*.mo
|
||||||
*.pot
|
|
||||||
|
# Mr Developer
|
||||||
|
.mr.developer.cfg
|
||||||
|
.project
|
||||||
|
.pydevproject
|
||||||
|
|
||||||
|
# Rope
|
||||||
|
.ropeproject
|
||||||
|
|
||||||
# Django stuff:
|
# Django stuff:
|
||||||
*.log
|
*.log
|
||||||
local_settings.py
|
*.pot
|
||||||
db.sqlite3
|
|
||||||
db.sqlite3-journal
|
|
||||||
|
|
||||||
# Flask stuff:
|
.DS_Store
|
||||||
instance/
|
|
||||||
.webassets-cache
|
|
||||||
|
|
||||||
# Scrapy stuff:
|
|
||||||
.scrapy
|
|
||||||
|
|
||||||
# Sphinx documentation
|
# Sphinx documentation
|
||||||
docs/_build/
|
docs/_build/
|
||||||
|
|
||||||
# PyBuilder
|
|
||||||
.pybuilder/
|
|
||||||
target/
|
|
||||||
|
|
||||||
# Jupyter Notebook
|
|
||||||
.ipynb_checkpoints
|
|
||||||
|
|
||||||
# IPython
|
|
||||||
profile_default/
|
|
||||||
ipython_config.py
|
|
||||||
|
|
||||||
# pyenv
|
|
||||||
# For a library or package, you might want to ignore these files since the code is
|
|
||||||
# intended to run in multiple environments; otherwise, check them in:
|
|
||||||
# .python-version
|
|
||||||
|
|
||||||
# pipenv
|
|
||||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
||||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
||||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
||||||
# install all needed dependencies.
|
|
||||||
#Pipfile.lock
|
|
||||||
|
|
||||||
# poetry
|
|
||||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
||||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
||||||
# commonly ignored for libraries.
|
|
||||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
||||||
#poetry.lock
|
|
||||||
|
|
||||||
# pdm
|
|
||||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
||||||
#pdm.lock
|
|
||||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
||||||
# in version control.
|
|
||||||
# https://pdm.fming.dev/#use-with-ide
|
|
||||||
.pdm.toml
|
|
||||||
|
|
||||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
||||||
__pypackages__/
|
|
||||||
|
|
||||||
# Celery stuff
|
|
||||||
celerybeat-schedule
|
|
||||||
celerybeat.pid
|
|
||||||
|
|
||||||
# SageMath parsed files
|
|
||||||
*.sage.py
|
|
||||||
|
|
||||||
# Environments
|
|
||||||
.env
|
|
||||||
.venv
|
|
||||||
env/
|
|
||||||
venv/
|
|
||||||
ENV/
|
|
||||||
env.bak/
|
|
||||||
venv.bak/
|
|
||||||
|
|
||||||
# Spyder project settings
|
|
||||||
.spyderproject
|
|
||||||
.spyproject
|
|
||||||
|
|
||||||
# Rope project settings
|
|
||||||
.ropeproject
|
|
||||||
|
|
||||||
# mkdocs documentation
|
|
||||||
/site
|
|
||||||
|
|
||||||
# mypy
|
|
||||||
.mypy_cache/
|
|
||||||
.dmypy.json
|
|
||||||
dmypy.json
|
|
||||||
|
|
||||||
# Pyre type checker
|
|
||||||
.pyre/
|
|
||||||
|
|
||||||
# pytype static type analyzer
|
|
||||||
.pytype/
|
|
||||||
|
|
||||||
# Cython debug symbols
|
|
||||||
cython_debug/
|
|
||||||
|
|
||||||
# PyCharm
|
# PyCharm
|
||||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
.idea/
|
||||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
||||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
# VSCode
|
||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
.vscode/
|
||||||
#.idea/
|
|
||||||
|
# Pyenv
|
||||||
|
.python-version
|
||||||
|
|
||||||
|
target
|
||||||
|
Cargo.lock
|
||||||
|
/doc
|
||||||
|
/gh-pages
|
||||||
|
build/
|
||||||
|
*.py[co]
|
||||||
|
__pycache__/
|
||||||
|
.cache
|
||||||
|
.pytest_cache/
|
||||||
|
dist/
|
||||||
|
.tox/
|
||||||
|
.mypy_cache/
|
||||||
|
.hypothesis/
|
||||||
|
.eggs/
|
||||||
|
venv*
|
||||||
|
guide/book/
|
||||||
|
*.so
|
||||||
|
*.out
|
||||||
|
*.egg-info
|
||||||
|
extensions/stamps/
|
||||||
|
pip-wheel-metadata
|
||||||
|
valgrind-python.supp
|
||||||
|
*.pyd
|
||||||
|
lcov.info
|
||||||
|
netlify_build/
|
14
Cargo.toml
Normal file
14
Cargo.toml
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
[package]
|
||||||
|
name = "nova-python"
|
||||||
|
version = "0.1.2"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
[lib]
|
||||||
|
name = "nova_python"
|
||||||
|
crate-type = ["cdylib"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
pyo3 = "0.19.0"
|
||||||
|
reqwest = "0.11.18"
|
||||||
|
tokio = { version = "1.29.1", features = ["rt-multi-thread", "time"] }
|
71
README.md
71
README.md
|
@ -1,2 +1,73 @@
|
||||||
# nova-python
|
# nova-python
|
||||||
🐍 Python library for accessing the Nova API
|
🐍 Python library for accessing the Nova API
|
||||||
|
|
||||||
|
## Usage ##
|
||||||
|
Install the module (This requires <a href="https://rustup.rs/">Cargo</a>)
|
||||||
|
```sh
|
||||||
|
$ pip install nova-python
|
||||||
|
```
|
||||||
|
|
||||||
|
Import the module
|
||||||
|
```python
|
||||||
|
from nova_python import Endpoints, Models, NovaClient
|
||||||
|
```
|
||||||
|
|
||||||
|
Create an instance of NovaClient, using your API key
|
||||||
|
|
||||||
|
```python
|
||||||
|
client = NovaClient("YOUR_API_KEY")
|
||||||
|
```
|
||||||
|
|
||||||
|
nova_python currently implements two enums: Endpoints and Models. Those contain:
|
||||||
|
|
||||||
|
**Endpoints**
|
||||||
|
* `Endpoints.CHAT_COMPLETION`
|
||||||
|
* `Endpoints.MODERATION`
|
||||||
|
|
||||||
|
**Models**
|
||||||
|
* `Models.GPT3`
|
||||||
|
* `Models.GPT4`
|
||||||
|
* `Models.MODERATION_LATEST`
|
||||||
|
* `Models.MODERATION_STABLE`
|
||||||
|
|
||||||
|
Now, to make a request, use the `make_request` function. For example:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from nova_python import Endpoints, Models, NovaClient
|
||||||
|
client = NovaClient("YOUR_API_KEY")
|
||||||
|
|
||||||
|
client.make_request(
|
||||||
|
endpoint=Endpoints.CHAT_COMPLETION,
|
||||||
|
model=Models.GPT3,
|
||||||
|
data=[
|
||||||
|
{"role": "system", "content": "You are a helpful assistant."},
|
||||||
|
{"role": "user", "content": "Hello!"}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```python
|
||||||
|
from nova_python import Endpoints, Models, NovaClient
|
||||||
|
client = NovaClient("YOUR_API_KEY")
|
||||||
|
|
||||||
|
client.make_request(
|
||||||
|
endpoint=Endpoints.MODERATION,
|
||||||
|
model=Models.MODERATION_STABLE,
|
||||||
|
data=[{"input": "I'm going to kill them."}]
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
If everything goes to plan, you'll receive a string containing JSON-Data, which you can then use in your project.
|
||||||
|
*Happy prompting!*
|
||||||
|
<br><br>
|
||||||
|
## FAQ ##
|
||||||
|
**Q:** I get an error when installing the package
|
||||||
|
**A:** Make you sure, that you have <a href="https://rustup.rs/">Cargo</a> installed
|
||||||
|
|
||||||
|
**Q**: I keep getting `RuntimeError: error sending request for url (https://api.nova-oss.com/v1/moderations): operation timed out`
|
||||||
|
**A**: Try passing a higher value than 30 as `seconds_until_timeout` to `make_request`
|
||||||
|
|
||||||
|
Made with 🩸 by Leander
|
23
pyproject.toml
Normal file
23
pyproject.toml
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
[build-system]
|
||||||
|
requires = ["maturin>=1.1,<2.0"]
|
||||||
|
build-backend = "maturin"
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name = "nova-python"
|
||||||
|
authors = [
|
||||||
|
{name = "Leander <@henceiusegentoo>"}
|
||||||
|
]
|
||||||
|
|
||||||
|
description = "🐍 Python library for accessing the Nova API"
|
||||||
|
requires-python = ">=3.7"
|
||||||
|
classifiers = [
|
||||||
|
"Programming Language :: Rust",
|
||||||
|
"Programming Language :: Python :: Implementation :: CPython",
|
||||||
|
"Programming Language :: Python :: Implementation :: PyPy",
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.urls]
|
||||||
|
"Repo" = "https://github.com/henceiusegentoo/nova-python"
|
||||||
|
|
||||||
|
[tool.maturin]
|
||||||
|
features = ["pyo3/extension-module"]
|
227
src/lib.rs
Normal file
227
src/lib.rs
Normal file
|
@ -0,0 +1,227 @@
|
||||||
|
use pyo3::prelude::*;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use pyo3::types::PyDict;
|
||||||
|
use std::time;
|
||||||
|
|
||||||
|
// An enum to represent the different models
|
||||||
|
#[pyclass(module = "nova_python", frozen)]
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
#[derive(Clone)]
|
||||||
|
enum Models {
|
||||||
|
#[pyo3(name = "GPT3")]
|
||||||
|
Gpt3,
|
||||||
|
#[pyo3(name = "GPT4")]
|
||||||
|
Gpt4,
|
||||||
|
#[pyo3(name = "MODERATION_LATEST")]
|
||||||
|
ModerationLatest,
|
||||||
|
#[pyo3(name = "MODERATION_STABLE")]
|
||||||
|
ModerationStable,
|
||||||
|
}
|
||||||
|
|
||||||
|
// An enum to represent the different endpoints
|
||||||
|
#[pyclass(module = "nova_python", frozen)]
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
#[derive(Clone)]
|
||||||
|
enum Endpoints {
|
||||||
|
#[pyo3(name = "CHAT_COMPLETION")]
|
||||||
|
ChatCompletion,
|
||||||
|
#[pyo3(name = "MODERATION")]
|
||||||
|
Moderation,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Interface to the Nova API
|
||||||
|
#[pyclass(module = "nova_python", frozen)]
|
||||||
|
struct NovaClient {
|
||||||
|
#[pyo3(get)]
|
||||||
|
api_key: String,
|
||||||
|
url: String
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pymethods]
|
||||||
|
impl NovaClient {
|
||||||
|
#[new]
|
||||||
|
fn new(api_key: String) -> PyResult<Self> {
|
||||||
|
let api_key = api_key.trim().to_string();
|
||||||
|
|
||||||
|
if !key_is_valid(&api_key) {
|
||||||
|
return Err(NovaClient::get_invalid_key_error());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(NovaClient {
|
||||||
|
api_key,
|
||||||
|
url: String::from("https://api.nova-oss.com/v1/")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Used to make a request to the Nova API
|
||||||
|
fn make_request(&self, endpoint: Endpoints, model: Models, data: Vec<Py<PyDict>>, seconds_until_timeout: Option<String>) -> PyResult<String> {
|
||||||
|
if !model_is_compatible(&endpoint, &model) {
|
||||||
|
return Err(NovaClient::get_endpoint_not_compatible_error());
|
||||||
|
}
|
||||||
|
|
||||||
|
let request_url = self.get_request_url(&endpoint).unwrap();
|
||||||
|
let request_body = self.get_request_body(&endpoint, &model, data).unwrap();
|
||||||
|
|
||||||
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
|
let seconds_until_timeout = match seconds_until_timeout {
|
||||||
|
Some(seconds_until_timeout) => seconds_until_timeout.parse::<u64>().unwrap(),
|
||||||
|
None => 30
|
||||||
|
};
|
||||||
|
|
||||||
|
let response: Result<String, reqwest::Error> = rt.block_on(async {
|
||||||
|
let client = reqwest::Client::builder()
|
||||||
|
.timeout(time::Duration::from_secs(seconds_until_timeout))
|
||||||
|
.user_agent("Mozilla/5.0")
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let ai_response = client.post(&request_url)
|
||||||
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.body(request_body)
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let text = ai_response.text().await?;
|
||||||
|
Ok(text)
|
||||||
|
});
|
||||||
|
|
||||||
|
match response {
|
||||||
|
Ok(response) => Ok(response),
|
||||||
|
Err(response) => Err(pyo3::exceptions::PyRuntimeError::new_err(response.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NovaClient {
|
||||||
|
fn get_request_url(&self, endpoint: &Endpoints) -> PyResult<String> {
|
||||||
|
match endpoint {
|
||||||
|
Endpoints::ChatCompletion => Ok(format!("{}chat/completions", self.url)),
|
||||||
|
Endpoints::Moderation => Ok(format!("{}moderations", self.url)),
|
||||||
|
_ => Err(NovaClient::get_invalid_endpoint_error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_request_body(&self, endpoint: &Endpoints, model: &Models, data: Vec<Py<PyDict>>) -> PyResult<String> {
|
||||||
|
let request_data = Python::with_gil(|py| {
|
||||||
|
let mut request_data = Vec::new();
|
||||||
|
|
||||||
|
for dict in data {
|
||||||
|
let dict = dict.as_ref(py).extract::<HashMap<String, String>>().unwrap_or(HashMap::new());
|
||||||
|
request_data.push(dict);
|
||||||
|
}
|
||||||
|
|
||||||
|
request_data
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut request_body = String::from("{");
|
||||||
|
|
||||||
|
let model = match model {
|
||||||
|
Models::Gpt3 => "gpt-3.5-turbo",
|
||||||
|
Models::Gpt4 => "gpt-4",
|
||||||
|
Models::ModerationLatest => "text-moderation-latest",
|
||||||
|
Models::ModerationStable => "text-moderation-stable",
|
||||||
|
_ => return Err(NovaClient::get_invalid_model_error())
|
||||||
|
};
|
||||||
|
request_body.push_str(&format!("\"model\":\"{}\"", model));
|
||||||
|
|
||||||
|
if endpoint == &Endpoints::ChatCompletion {
|
||||||
|
request_body.push_str(",\"messages\":[");
|
||||||
|
|
||||||
|
for map in request_data {
|
||||||
|
|
||||||
|
request_body.push_str("{");
|
||||||
|
for (key, value) in map {
|
||||||
|
request_body.push_str(&format!("\"{}\":\"{}\",", key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
if request_body.ends_with(",") {
|
||||||
|
request_body.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
request_body.push_str("},");
|
||||||
|
}
|
||||||
|
|
||||||
|
if request_body.ends_with(",") {
|
||||||
|
request_body.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
request_body.push_str("]");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else if endpoint == &Endpoints::Moderation {
|
||||||
|
request_body.push_str(",\"input\":");
|
||||||
|
|
||||||
|
let input = format!("\"{}\"", request_data.get(0).unwrap().get("input").unwrap());
|
||||||
|
request_body.push_str(&input);
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
return Err(NovaClient::get_invalid_endpoint_error());
|
||||||
|
}
|
||||||
|
|
||||||
|
request_body.push_str("}");
|
||||||
|
Ok(request_body)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_invalid_key_error() -> PyErr {
|
||||||
|
pyo3::exceptions::PyValueError::new_err("Invalid API key")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_endpoint_not_compatible_error() -> PyErr {
|
||||||
|
pyo3::exceptions::PyValueError::new_err("Endpoint is not compatible with model")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_invalid_endpoint_error() -> PyErr {
|
||||||
|
pyo3::exceptions::PyValueError::new_err("Invalid endpoint")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_invalid_model_error() -> PyErr {
|
||||||
|
pyo3::exceptions::PyValueError::new_err("Invalid model")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_request_failed_error() -> PyErr {
|
||||||
|
pyo3::exceptions::PyRuntimeError::new_err("Request failed for unknown reasons.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn model_is_compatible(endpoint: &Endpoints, model: &Models) -> bool {
|
||||||
|
if endpoint == &Endpoints::ChatCompletion {
|
||||||
|
if [Models::Gpt3, Models::Gpt4].contains(model) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if endpoint == &Endpoints::Moderation {
|
||||||
|
if [Models::ModerationStable, Models::ModerationLatest].contains(model) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
fn key_is_valid(api_key: &str) -> bool {
|
||||||
|
if !api_key.starts_with("nv-") {
|
||||||
|
return false;
|
||||||
|
} else if !api_key.len() == 51 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[pymodule]
|
||||||
|
fn nova_python(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||||
|
m.add_class::<Models>()?;
|
||||||
|
m.add_class::<Endpoints>()?;
|
||||||
|
m.add_class::<NovaClient>()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in a new issue