mirror of
https://github.com/NovaOSS/nova-python.git
synced 2024-11-25 16:03:58 +01:00
Initial commit aka. it's done
This commit is contained in:
parent
3cbaaf7d54
commit
fe5709275e
168
.gitignore
vendored
168
.gitignore
vendored
|
@ -1,160 +1,98 @@
|
|||
/target
|
||||
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
.pytest_cache/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
.venv/
|
||||
env/
|
||||
bin/
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
include/
|
||||
man/
|
||||
venv/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.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
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
pip-selfcheck.json
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Mr Developer
|
||||
.mr.developer.cfg
|
||||
.project
|
||||
.pydevproject
|
||||
|
||||
# Rope
|
||||
.ropeproject
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
*.pot
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
.DS_Store
|
||||
|
||||
# Sphinx documentation
|
||||
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
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# 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
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
.idea/
|
||||
|
||||
# VSCode
|
||||
.vscode/
|
||||
|
||||
# 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.0"
|
||||
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"] }
|
42
README.md
42
README.md
|
@ -1,2 +1,44 @@
|
|||
# nova-python
|
||||
🐍 Python library for accessing the Nova API
|
||||
|
||||
## Usage ##
|
||||
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`
|
||||
|
||||
**Models**
|
||||
* `Models.GPT3`
|
||||
+ `Models.GPT4`
|
||||
|
||||
Now, to make a request, use the `make_request` function.
|
||||
|
||||
```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!"}
|
||||
]
|
||||
)
|
||||
```
|
||||
|
||||
If everything goes to plan, you'll receive a string containing JSON-Data, which you can then use in your project.
|
||||
*Happy prompting!*
|
||||
|
||||
Made with 🩸 by Leander
|
21
pyproject.toml
Normal file
21
pyproject.toml
Normal file
|
@ -0,0 +1,21 @@
|
|||
[build-system]
|
||||
requires = ["maturin>=1.1,<2.0"]
|
||||
build-backend = "maturin"
|
||||
|
||||
[project]
|
||||
name = "nova-python"
|
||||
authors = [
|
||||
{name = "Leander"}
|
||||
]
|
||||
|
||||
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",
|
||||
]
|
||||
|
||||
|
||||
[tool.maturin]
|
||||
features = ["pyo3/extension-module"]
|
193
src/lib.rs
Normal file
193
src/lib.rs
Normal file
|
@ -0,0 +1,193 @@
|
|||
use pyo3::prelude::*;
|
||||
use std::collections::HashMap;
|
||||
use pyo3::types::PyDict;
|
||||
use std::time;
|
||||
|
||||
#[pyclass(module = "nova_python", frozen)]
|
||||
#[derive(PartialEq)]
|
||||
#[derive(Clone)]
|
||||
enum Models {
|
||||
#[pyo3(name = "GPT3")]
|
||||
Gpt3,
|
||||
#[pyo3(name = "GPT4")]
|
||||
Gpt4
|
||||
}
|
||||
|
||||
#[pyclass(module = "nova_python", frozen)]
|
||||
#[derive(PartialEq)]
|
||||
#[derive(Clone)]
|
||||
enum Endpoints {
|
||||
#[pyo3(name = "CHAT_COMPLETION")]
|
||||
ChatCompletion,
|
||||
}
|
||||
|
||||
#[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/")
|
||||
})
|
||||
}
|
||||
|
||||
fn make_request(&self, endpoint: Endpoints, model: Models, data: Vec<Py<PyDict>>) -> 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 response: Result<String, reqwest::Error> = rt.block_on(async {
|
||||
let client = reqwest::Client::builder()
|
||||
.timeout(time::Duration::from_secs(10))
|
||||
.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)),
|
||||
_ => 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",
|
||||
_ => 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 {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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