Skip to content

Commit e1e4387

Browse files
committed
initial commit
0 parents  commit e1e4387

File tree

10 files changed

+563
-0
lines changed

10 files changed

+563
-0
lines changed

.gitignore

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
.idea/**/workspace.xml
2+
.idea/**/tasks.xml
3+
.idea/**/usage.statistics.xml
4+
.idea/**/dictionaries
5+
.idea/**/shelf
6+
.idea/**/aws.xml
7+
.idea/**/contentModel.xml
8+
.idea/**/dataSources/
9+
.idea/**/dataSources.ids
10+
.idea/**/dataSources.local.xml
11+
.idea/**/sqlDataSources.xml
12+
.idea/**/dynamic.xml
13+
.idea/**/uiDesigner.xml
14+
.idea/**/dbnavigator.xml
15+
.idea/**/gradle.xml
16+
.idea/**/libraries
17+
cmake-build-*/
18+
.idea/**/mongoSettings.xml
19+
*.iws
20+
out/
21+
.idea_modules/
22+
atlassian-ide-plugin.xml
23+
.idea/replstate.xml
24+
.idea/sonarlint/
25+
com_crashlytics_export_strings.xml
26+
crashlytics.properties
27+
crashlytics-build.properties
28+
fabric.properties
29+
.idea/httpRequests
30+
.idea/caches/build_file_checksums.ser
31+
__pycache__/
32+
*.py[cod]
33+
*$py.class
34+
*.so
35+
.Python
36+
build/
37+
develop-eggs/
38+
dist/
39+
downloads/
40+
eggs/
41+
.eggs/
42+
lib/
43+
lib64/
44+
parts/
45+
sdist/
46+
var/
47+
wheels/
48+
share/python-wheels/
49+
*.egg-info/
50+
.installed.cfg
51+
*.egg
52+
MANIFEST
53+
*.manifest
54+
*.spec
55+
pip-log.txt
56+
pip-delete-this-directory.txt
57+
htmlcov/
58+
.tox/
59+
.nox/
60+
.coverage
61+
.coverage.*
62+
.cache
63+
nosetests.xml
64+
coverage.xml
65+
*.cover
66+
*.py,cover
67+
.hypothesis/
68+
.pytest_cache/
69+
cover/
70+
*.mo
71+
*.pot
72+
*.log
73+
local_settings.py
74+
db.sqlite3
75+
db.sqlite3-journal
76+
instance/
77+
.webassets-cache
78+
.scrapy
79+
docs/_build/
80+
.pybuilder/
81+
target/
82+
.ipynb_checkpoints
83+
profile_default/
84+
ipython_config.py
85+
.pdm.toml
86+
__pypackages__/
87+
celerybeat-schedule
88+
celerybeat.pid
89+
*.sage.py
90+
.env
91+
.venv
92+
env/
93+
venv/
94+
ENV/
95+
env.bak/
96+
venv.bak/
97+
.spyderproject
98+
.spyproject
99+
.ropeproject
100+
/site
101+
.mypy_cache/
102+
.dmypy.json
103+
dmypy.json
104+
.pyre/
105+
.pytype/
106+
cython_debug/
107+
.idea/

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
=====================
3+
4+
Copyright © 2023 Leandro Lima <leandro@lls-software.com>
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# funcgpt: Python library for creating functions with OpenAI's GPT
2+
3+
funcgpt is an easy-to-use Python library that allows you to quickly create Python functions using the power of OpenAI's GPT models. With just a few lines of code, you can create functions that generate human-like responses, answer questions, or anything else that GPT is capable of.
4+
5+
## Features
6+
7+
- Easy to use decorator for creating functions based on GPT models
8+
- Supports different GPT model versions
9+
- Customize GPT's behavior with adjustable temperature values
10+
- Generate responses in streaming or non-streaming modes
11+
12+
## Installation
13+
14+
To install funcgpt, use pip:
15+
16+
```bash
17+
pip install funcgpt
18+
```
19+
20+
## Usage
21+
22+
To create a function that answers questions like a pirate, you can use the following snippet:
23+
24+
```python
25+
from funcgpt import gpt
26+
27+
@gpt
28+
def answer_like_pirate(message: str) -> str:
29+
"""Answer questions like a pirate."""
30+
...
31+
32+
```
33+
34+
Usage:
35+
36+
```python
37+
>>> answer_like_pirate("How are you doing today?")
38+
"Arrr, I be doin' fine, matey."
39+
```
40+
41+
To do the same thing, but with a function that streams responses, you can use the following snippet:
42+
43+
```python
44+
from typing import Iterator
45+
from funcgpt import gpt
46+
47+
@gpt
48+
def stream_like_pirate(message: str) -> Iterator[str]:
49+
"""Answers questions like a pirate."""
50+
...
51+
52+
```
53+
54+
Usage:
55+
56+
```python
57+
>>> for token in stream_like_pirate("How are you doing today?"):
58+
... print(token, end="", flush=True)
59+
...
60+
Arrr, I be doin' fine, matey.
61+
```
62+
63+
For defining a function that returns a boolean value, you can use the following snippet:
64+
65+
```python
66+
from funcgpt import gpt
67+
68+
@gpt
69+
def is_pirate(message: str) -> bool:
70+
"""Returns true if the message is from a pirate."""
71+
...
72+
73+
```
74+
75+
Usage:
76+
77+
```python
78+
>>> is_pirate("Arrr, I be doin' fine, matey.")
79+
True
80+
```
81+
82+
For choosing a different model or temperature, you can use the `model` and `temperature` keyword arguments:
83+
84+
```python
85+
from funcgpt import gpt
86+
87+
@gpt(model="gpt-4", temperature=0)
88+
def answer_like_pirate(message: str) -> str:
89+
"""Answer questions like a pirate."""
90+
...
91+
92+
```
93+
94+
## Contributing
95+
96+
We welcome contributions! Please feel free to fork the repository, make changes, and submit pull requests. If you have any questions or ideas, don't hesitate to open an issue.
97+
98+
## License
99+
100+
funcgpt is released under the MIT License. See the [LICENSE](LICENSE) file for more details.

funcgpt/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from __future__ import annotations
2+
3+
from .decorators import gpt
4+
5+
__all__ = ["gpt"]

funcgpt/credentials.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from __future__ import annotations
2+
3+
from os import getenv
4+
5+
if (OPENAI_API_KEY := getenv("OPENAI_API_KEY")) is None:
6+
raise RuntimeError("OPENAI_API_KEY environment variable not set")
7+
8+
OPENAI_ORG_ID = getenv("OPENAI_ORG_ID")

funcgpt/decorators.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import Callable, Literal, TypeVar, overload
2+
3+
from funcgpt.protocols import GPTAnswerProtocol, GPTStreamProtocol
4+
from funcgpt.wrapper import create_generic_wrapper
5+
6+
T = TypeVar("T", bound=GPTAnswerProtocol | GPTStreamProtocol)
7+
8+
__all__ = ["gpt"]
9+
10+
11+
@overload
12+
def gpt(f: T) -> T:
13+
...
14+
15+
16+
@overload
17+
def gpt(
18+
model: Literal["gpt-3.5-turbo", "gpt-4"] = "gpt-3.5-turbo",
19+
temperature: int = 0,
20+
) -> Callable[[T], T]:
21+
...
22+
23+
24+
def gpt(*args, model: Literal["gpt-3.5-turbo", "gpt-4"] = "gpt-3.5-turbo", temperature: int = 0):
25+
if len(args) == 1 and callable(f := args[0]):
26+
return create_generic_wrapper(f=f, model=model, temperature=temperature)
27+
else:
28+
return gpt

0 commit comments

Comments
 (0)