autonomous-app 0.2.25__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/PKG-INFO +7 -8
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/pyproject.toml +3 -3
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/requirements.txt +4 -9
- autonomous_app-0.3.0/src/autonomous/__init__.py +7 -0
- autonomous_app-0.3.0/src/autonomous/ai/audioagent.py +32 -0
- autonomous_app-0.3.0/src/autonomous/ai/imageagent.py +31 -0
- autonomous_app-0.3.0/src/autonomous/ai/jsonagent.py +40 -0
- autonomous_app-0.3.0/src/autonomous/ai/models/openai.py +280 -0
- autonomous_app-0.3.0/src/autonomous/ai/oaiagent.py +53 -0
- autonomous_app-0.3.0/src/autonomous/ai/textagent.py +35 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous/auth/autoauth.py +2 -2
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous/auth/user.py +8 -10
- autonomous_app-0.3.0/src/autonomous/model/autoattr.py +105 -0
- autonomous_app-0.3.0/src/autonomous/model/automodel.py +159 -0
- autonomous_app-0.3.0/src/autonomous/storage/__init__.py +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous/storage/imagestorage.py +9 -54
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous/tasks/autotask.py +0 -25
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous_app.egg-info/PKG-INFO +7 -8
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous_app.egg-info/SOURCES.txt +13 -14
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous_app.egg-info/requires.txt +4 -5
- autonomous_app-0.2.25/src/autonomous/__init__.py +0 -4
- autonomous_app-0.2.25/src/autonomous/ai/oaiagent.py +0 -214
- autonomous_app-0.2.25/src/autonomous/db/autodb.py +0 -86
- autonomous_app-0.2.25/src/autonomous/db/table.py +0 -156
- autonomous_app-0.2.25/src/autonomous/errors/__init__.py +0 -1
- autonomous_app-0.2.25/src/autonomous/errors/danglingreferenceerror.py +0 -8
- autonomous_app-0.2.25/src/autonomous/model/__init__.py +0 -1
- autonomous_app-0.2.25/src/autonomous/model/autoattribute.py +0 -20
- autonomous_app-0.2.25/src/autonomous/model/automodel.py +0 -400
- autonomous_app-0.2.25/src/autonomous/model/orm.py +0 -86
- autonomous_app-0.2.25/src/autonomous/model/serializer.py +0 -110
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/LICENSE +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/README.md +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/setup.cfg +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/setup.py +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous/ai/__init__.py +0 -0
- {autonomous_app-0.2.25/src/autonomous/storage → autonomous_app-0.3.0/src/autonomous/ai/models}/__init__.py +0 -0
- {autonomous_app-0.2.25/src/autonomous/storage → autonomous_app-0.3.0/src/autonomous/apis}/version_control/GHCallbacks.py +0 -0
- {autonomous_app-0.2.25/src/autonomous/storage → autonomous_app-0.3.0/src/autonomous/apis}/version_control/GHOrganization.py +0 -0
- {autonomous_app-0.2.25/src/autonomous/storage → autonomous_app-0.3.0/src/autonomous/apis}/version_control/GHRepo.py +0 -0
- {autonomous_app-0.2.25/src/autonomous/storage → autonomous_app-0.3.0/src/autonomous/apis}/version_control/GHVersionControl.py +0 -0
- {autonomous_app-0.2.25/src/autonomous/storage → autonomous_app-0.3.0/src/autonomous/apis}/version_control/__init__.py +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous/auth/__init__.py +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous/auth/github.py +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous/auth/google.py +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous/cli.py +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous/logger.py +0 -0
- {autonomous_app-0.2.25/src/autonomous/db → autonomous_app-0.3.0/src/autonomous/model}/__init__.py +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous/storage/localstorage.py +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous/tasks/__init__.py +0 -0
- {autonomous_app-0.2.25/src/autonomous/storage → autonomous_app-0.3.0/src/autonomous/utils}/markdown.py +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous_app.egg-info/dependency_links.txt +0 -0
- {autonomous_app-0.2.25 → autonomous_app-0.3.0}/src/autonomous_app.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: autonomous-app
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Containerized application framework built on Flask with additional libraries and tools for rapid development of web applications.
|
|
5
5
|
Author-email: Steven A Moore <samoore@binghamton.edu>
|
|
6
6
|
License: MIT License
|
|
@@ -25,27 +25,26 @@ License: MIT License
|
|
|
25
25
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
26
|
SOFTWARE.
|
|
27
27
|
Project-URL: homepage, https://github.com/Sallenmoore/autonomous
|
|
28
|
-
Classifier: Programming Language :: Python :: 3.
|
|
28
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
29
29
|
Classifier: License :: OSI Approved :: MIT License
|
|
30
30
|
Classifier: Operating System :: OS Independent
|
|
31
|
-
Requires-Python: >=3.
|
|
31
|
+
Requires-Python: >=3.12
|
|
32
32
|
Description-Content-Type: text/markdown
|
|
33
33
|
License-File: LICENSE
|
|
34
34
|
Requires-Dist: Flask
|
|
35
|
-
Requires-Dist: icecream
|
|
36
35
|
Requires-Dist: setuptools
|
|
37
36
|
Requires-Dist: python-dotenv
|
|
38
37
|
Requires-Dist: PyGithub
|
|
39
38
|
Requires-Dist: pygit2
|
|
40
|
-
Requires-Dist:
|
|
39
|
+
Requires-Dist: mongoengine
|
|
41
40
|
Requires-Dist: pillow
|
|
42
|
-
Requires-Dist:
|
|
41
|
+
Requires-Dist: redis
|
|
43
42
|
Requires-Dist: jsmin
|
|
44
43
|
Requires-Dist: requests
|
|
45
44
|
Requires-Dist: gunicorn
|
|
46
45
|
Requires-Dist: Authlib
|
|
47
|
-
Requires-Dist:
|
|
48
|
-
Requires-Dist:
|
|
46
|
+
Requires-Dist: rq
|
|
47
|
+
Requires-Dist: openai>=1.42
|
|
49
48
|
Requires-Dist: dateparser
|
|
50
49
|
Requires-Dist: python-slugify
|
|
51
50
|
|
|
@@ -10,10 +10,10 @@ authors = [
|
|
|
10
10
|
description = "Containerized application framework built on Flask with additional libraries and tools for rapid development of web applications."
|
|
11
11
|
dynamic = ["version", "dependencies"]
|
|
12
12
|
readme = "README.md"
|
|
13
|
-
requires-python = ">=3.
|
|
13
|
+
requires-python = ">=3.12"
|
|
14
14
|
license={file = "LICENSE"}
|
|
15
15
|
classifiers=[
|
|
16
|
-
"Programming Language :: Python :: 3.
|
|
16
|
+
"Programming Language :: Python :: 3.12",
|
|
17
17
|
"License :: OSI Approved :: MIT License",
|
|
18
18
|
"Operating System :: OS Independent",
|
|
19
19
|
]
|
|
@@ -35,7 +35,7 @@ where = ["src"]
|
|
|
35
35
|
include = ["autonomous*"]
|
|
36
36
|
|
|
37
37
|
[tool.pytest.ini_options]
|
|
38
|
-
addopts = "--cov=autonomous -rx -l -x -s --log-level=INFO --ignore=src --no-cov-on-fail -v --pdb --cov-reset"
|
|
38
|
+
addopts = "--cov=autonomous -rx -l -x -s --log-level=INFO --ignore=src --no-cov-on-fail -v --pdb --cov-reset "
|
|
39
39
|
|
|
40
40
|
testpaths = [
|
|
41
41
|
"tests",
|
|
@@ -8,8 +8,6 @@ Flask
|
|
|
8
8
|
|
|
9
9
|
##### Debugging, Logging, and Documentation #####
|
|
10
10
|
|
|
11
|
-
icecream
|
|
12
|
-
|
|
13
11
|
##### System #####
|
|
14
12
|
|
|
15
13
|
setuptools
|
|
@@ -19,10 +17,9 @@ python-dotenv
|
|
|
19
17
|
|
|
20
18
|
PyGithub
|
|
21
19
|
pygit2
|
|
22
|
-
|
|
23
|
-
cloudinary
|
|
20
|
+
mongoengine
|
|
24
21
|
pillow
|
|
25
|
-
|
|
22
|
+
redis
|
|
26
23
|
|
|
27
24
|
##### Frontend #####
|
|
28
25
|
|
|
@@ -38,14 +35,12 @@ gunicorn
|
|
|
38
35
|
Authlib
|
|
39
36
|
|
|
40
37
|
##### Async #####
|
|
41
|
-
|
|
38
|
+
rq
|
|
42
39
|
|
|
43
40
|
##### AI #####
|
|
44
41
|
|
|
45
|
-
openai >= 1.
|
|
42
|
+
openai >= 1.42
|
|
46
43
|
|
|
47
44
|
##### Parsing #####
|
|
48
|
-
|
|
49
|
-
validators
|
|
50
45
|
dateparser
|
|
51
46
|
python-slugify
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from autonomous import log
|
|
2
|
+
from autonomous.model.autoattr import ReferenceAttr, StringAttr
|
|
3
|
+
from autonomous.model.automodel import AutoModel
|
|
4
|
+
|
|
5
|
+
from .models.openai import OpenAIModel
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class AudioAgent(AutoModel):
|
|
9
|
+
client = ReferenceAttr()
|
|
10
|
+
name = StringAttr(default="audioagent")
|
|
11
|
+
instructions = StringAttr(
|
|
12
|
+
default="You are highly skilled AI trained to assist with generating audio files."
|
|
13
|
+
)
|
|
14
|
+
description = StringAttr(
|
|
15
|
+
default="A helpful AI assistant trained to assist with generating audio files."
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
_ai_model = OpenAIModel
|
|
19
|
+
|
|
20
|
+
def get_client(self):
|
|
21
|
+
if self.client is None:
|
|
22
|
+
self.client = self._ai_model(
|
|
23
|
+
name=self.name,
|
|
24
|
+
instructions=self.instructions,
|
|
25
|
+
description=self.description,
|
|
26
|
+
)
|
|
27
|
+
self.client.save()
|
|
28
|
+
self.save()
|
|
29
|
+
return self.client
|
|
30
|
+
|
|
31
|
+
def generate(self, prompt, file_path, **kwargs):
|
|
32
|
+
return self.get_client().generate_audio(prompt, file_path, **kwargs)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from autonomous.model.autoattr import ReferenceAttr, StringAttr
|
|
2
|
+
from autonomous.model.automodel import AutoModel
|
|
3
|
+
|
|
4
|
+
from .models.openai import OpenAIModel
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class ImageAgent(AutoModel):
|
|
8
|
+
client = ReferenceAttr()
|
|
9
|
+
name = StringAttr(default="imageagent")
|
|
10
|
+
instructions = StringAttr(
|
|
11
|
+
default="You are highly skilled AI trained to assist with generating images."
|
|
12
|
+
)
|
|
13
|
+
description = StringAttr(
|
|
14
|
+
default="A helpful AI assistant trained to assist with generating images."
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
_ai_model = OpenAIModel
|
|
18
|
+
|
|
19
|
+
def get_client(self):
|
|
20
|
+
if self.client is None:
|
|
21
|
+
self.client = self._ai_model(
|
|
22
|
+
name=self.name,
|
|
23
|
+
instructions=self.instructions,
|
|
24
|
+
description=self.description,
|
|
25
|
+
)
|
|
26
|
+
self.client.save()
|
|
27
|
+
self.save()
|
|
28
|
+
return self.client
|
|
29
|
+
|
|
30
|
+
def generate(self, prompt, **kwargs):
|
|
31
|
+
return self.get_client().generate_image(prompt, **kwargs)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
from autonomous.model.autoattr import ReferenceAttr, StringAttr
|
|
4
|
+
from autonomous.model.automodel import AutoModel
|
|
5
|
+
|
|
6
|
+
from .models.openai import OpenAIModel
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class JSONAgent(AutoModel):
|
|
10
|
+
client = ReferenceAttr()
|
|
11
|
+
name = StringAttr(default="jsonagent")
|
|
12
|
+
instructions = StringAttr(
|
|
13
|
+
default="You are highly skilled AI trained to assist with generating JSON formatted data."
|
|
14
|
+
)
|
|
15
|
+
description = StringAttr(
|
|
16
|
+
default="A helpful AI assistant trained to assist with generating JSON formatted data."
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
_ai_model = OpenAIModel
|
|
20
|
+
|
|
21
|
+
def get_client(self):
|
|
22
|
+
if self.client is None:
|
|
23
|
+
self.client = self._ai_model(
|
|
24
|
+
name=self.name,
|
|
25
|
+
instructions=self.instructions,
|
|
26
|
+
description=self.description,
|
|
27
|
+
)
|
|
28
|
+
self.client.save()
|
|
29
|
+
self.save()
|
|
30
|
+
return self.client
|
|
31
|
+
|
|
32
|
+
def generate(self, messages, function, additional_instructions=""):
|
|
33
|
+
result = self.get_client().generate_json(
|
|
34
|
+
messages, function, additional_instructions
|
|
35
|
+
)
|
|
36
|
+
if isinstance(result, str):
|
|
37
|
+
result = json.loads(result)
|
|
38
|
+
elif not isinstance(result, dict):
|
|
39
|
+
raise ValueError(f"Invalid JSON response from AI model.\n\n{result}")
|
|
40
|
+
return result
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import random
|
|
4
|
+
import time
|
|
5
|
+
from base64 import b64decode
|
|
6
|
+
|
|
7
|
+
from openai import NotFoundError as openai_NotFoundError
|
|
8
|
+
from openai import OpenAI
|
|
9
|
+
|
|
10
|
+
from autonomous import log
|
|
11
|
+
from autonomous.model.autoattr import DictAttr, ListAttr, StringAttr
|
|
12
|
+
from autonomous.model.automodel import AutoModel
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class OpenAIModel(AutoModel):
|
|
16
|
+
_client = None
|
|
17
|
+
text_model = StringAttr(default="gpt-4o")
|
|
18
|
+
image_model = StringAttr(default="dall-e-3")
|
|
19
|
+
json_model = StringAttr(default="gpt-4o")
|
|
20
|
+
agent_id = StringAttr()
|
|
21
|
+
messages = ListAttr(StringAttr(default=[]))
|
|
22
|
+
tools = DictAttr()
|
|
23
|
+
vector_store = StringAttr()
|
|
24
|
+
name = StringAttr(default="agent")
|
|
25
|
+
instructions = StringAttr(
|
|
26
|
+
default="You are highly skilled AI trained to assist with various tasks."
|
|
27
|
+
)
|
|
28
|
+
description = StringAttr(
|
|
29
|
+
default="A helpful AI assistant trained to assist with various tasks."
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def client(self):
|
|
34
|
+
if not self._client:
|
|
35
|
+
self._client = OpenAI(api_key=os.environ.get("OPENAI_KEY"))
|
|
36
|
+
return self._client
|
|
37
|
+
|
|
38
|
+
def clear_agent(self):
|
|
39
|
+
if self.agent_id:
|
|
40
|
+
self.client.beta.assistants.delete(self.agent_id)
|
|
41
|
+
self.agent_id = ""
|
|
42
|
+
self.save()
|
|
43
|
+
|
|
44
|
+
def clear_agents(self):
|
|
45
|
+
assistants = self.client.beta.assistants.list().data
|
|
46
|
+
log(assistants)
|
|
47
|
+
for assistant in assistants:
|
|
48
|
+
log(f"==== Deleting Agent with ID: {assistant.id} ====")
|
|
49
|
+
try:
|
|
50
|
+
self.client.beta.assistants.delete(assistant.id)
|
|
51
|
+
except openai_NotFoundError:
|
|
52
|
+
log(f"==== Agent with ID: {assistant.id} not found ====")
|
|
53
|
+
self.agent_id = ""
|
|
54
|
+
self.save()
|
|
55
|
+
|
|
56
|
+
def _get_agent_id(self):
|
|
57
|
+
if not self.agent_id or not self.client.beta.assistants.retrieve(self.agent_id):
|
|
58
|
+
agent = self.client.beta.assistants.create(
|
|
59
|
+
instructions=self.instructions,
|
|
60
|
+
description=self.description,
|
|
61
|
+
name=self.name,
|
|
62
|
+
model=self.json_model,
|
|
63
|
+
)
|
|
64
|
+
self.agent_id = agent.id
|
|
65
|
+
log(f"==== Creating Agent with ID: {self.agent_id} ====")
|
|
66
|
+
self.save()
|
|
67
|
+
return self.agent_id
|
|
68
|
+
|
|
69
|
+
def clear_files(self, file_id=None):
|
|
70
|
+
if not file_id:
|
|
71
|
+
store_files = self.client.files.list().data
|
|
72
|
+
for sf in store_files:
|
|
73
|
+
self.client.files.delete(file_id=sf.id)
|
|
74
|
+
for vs in self.client.beta.vector_stores.list().data:
|
|
75
|
+
try:
|
|
76
|
+
self.client.beta.vector_stores.delete(vs.id)
|
|
77
|
+
except openai_NotFoundError:
|
|
78
|
+
log(f"==== Vector Store {vs.id} not found ====")
|
|
79
|
+
else:
|
|
80
|
+
self.client.files.delete(file_id=file_id)
|
|
81
|
+
self.tools.pop("file_search", None)
|
|
82
|
+
self.save()
|
|
83
|
+
return self.client.files.list()
|
|
84
|
+
|
|
85
|
+
def attach_file(self, file_contents, filename="dbdata.json"):
|
|
86
|
+
# Upload the user provided file to OpenAI
|
|
87
|
+
self.tools["file_search"] = {"type": "file_search"}
|
|
88
|
+
# Create a vector store
|
|
89
|
+
if vs := self.client.beta.vector_stores.list().data:
|
|
90
|
+
self.vector_store = vs[0].id
|
|
91
|
+
else:
|
|
92
|
+
self.vector_store = self.client.beta.vector_stores.create(
|
|
93
|
+
name="Data Reference",
|
|
94
|
+
expires_after={"anchor": "last_active_at", "days": 14},
|
|
95
|
+
).id
|
|
96
|
+
|
|
97
|
+
file_obj = self.client.files.create(
|
|
98
|
+
file=(filename, file_contents), purpose="assistants"
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
self.client.beta.vector_stores.files.create(
|
|
102
|
+
vector_store_id=self.vector_store,
|
|
103
|
+
file_id=file_obj.id,
|
|
104
|
+
)
|
|
105
|
+
self.client.beta.assistants.update(
|
|
106
|
+
self._get_agent_id(),
|
|
107
|
+
tools=list(self.tools.values()),
|
|
108
|
+
tool_resources={"file_search": {"vector_store_ids": [self.vector_store]}},
|
|
109
|
+
)
|
|
110
|
+
self.save()
|
|
111
|
+
return file_obj.id
|
|
112
|
+
|
|
113
|
+
def _add_function(self, user_function):
|
|
114
|
+
user_function["strict"] = True
|
|
115
|
+
user_function["parameters"]["required"] = list(
|
|
116
|
+
user_function["parameters"]["properties"].keys()
|
|
117
|
+
)
|
|
118
|
+
user_function["parameters"]["additionalProperties"] = False
|
|
119
|
+
|
|
120
|
+
self.tools["function"] = {"type": "function", "function": user_function}
|
|
121
|
+
self.client.beta.assistants.update(
|
|
122
|
+
self._get_agent_id(), tools=list(self.tools.values())
|
|
123
|
+
)
|
|
124
|
+
return """
|
|
125
|
+
IMPORTANT: Always use the function 'response' tool to respond to the user with the only the requested JSON schema. DO NOT add any text to the response outside of the JSON schema.
|
|
126
|
+
|
|
127
|
+
"""
|
|
128
|
+
|
|
129
|
+
def _format_messages(self, messages):
|
|
130
|
+
message_list = []
|
|
131
|
+
if isinstance(messages, str):
|
|
132
|
+
message_list.insert(0, {"role": "user", "content": messages})
|
|
133
|
+
else:
|
|
134
|
+
for message in messages:
|
|
135
|
+
if isinstance(message, str):
|
|
136
|
+
message_list.insert(0, {"role": "user", "content": message})
|
|
137
|
+
else:
|
|
138
|
+
raise Exception(
|
|
139
|
+
f"==== Invalid message: {message} ====\nMust be a string "
|
|
140
|
+
)
|
|
141
|
+
return message_list
|
|
142
|
+
|
|
143
|
+
def generate_json(self, messages, function, additional_instructions=""):
|
|
144
|
+
_instructions_addition = self._add_function(function)
|
|
145
|
+
_instructions_addition += additional_instructions
|
|
146
|
+
|
|
147
|
+
formatted_messages = self._format_messages(messages)
|
|
148
|
+
thread = self.client.beta.threads.create(messages=formatted_messages)
|
|
149
|
+
|
|
150
|
+
run = self.client.beta.threads.runs.create(
|
|
151
|
+
thread_id=thread.id,
|
|
152
|
+
assistant_id=self._get_agent_id(),
|
|
153
|
+
additional_instructions=_instructions_addition,
|
|
154
|
+
parallel_tool_calls=False,
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
while run.status in ["queued", "in_progress"]:
|
|
158
|
+
run = self.client.beta.threads.runs.retrieve(
|
|
159
|
+
thread_id=thread.id,
|
|
160
|
+
run_id=run.id,
|
|
161
|
+
)
|
|
162
|
+
time.sleep(0.5)
|
|
163
|
+
log(f"==== Job Status: {run.status} ====")
|
|
164
|
+
print(f"==== Job Status: {run.status} ====")
|
|
165
|
+
|
|
166
|
+
if run.status in ["failed", "expired", "canceled"]:
|
|
167
|
+
log(f"==== Error: {run.last_error} ====")
|
|
168
|
+
print(f"==== Error: {run.last_error} ====")
|
|
169
|
+
return None
|
|
170
|
+
print("=================== RUN COMPLETED ===================")
|
|
171
|
+
print(run.status)
|
|
172
|
+
if run.status == "completed":
|
|
173
|
+
response = self.client.beta.threads.messages.list(thread_id=thread.id)
|
|
174
|
+
results = response.data[0].content[0].text.value
|
|
175
|
+
elif run.status == "requires_action":
|
|
176
|
+
results = run.required_action.submit_tool_outputs.tool_calls[
|
|
177
|
+
0
|
|
178
|
+
].function.arguments
|
|
179
|
+
else:
|
|
180
|
+
log(f"====Status: {run.status} Error: {run.last_error} ====")
|
|
181
|
+
print(f"====Status: {run.status} Error: {run.last_error} ====")
|
|
182
|
+
return None
|
|
183
|
+
|
|
184
|
+
results = results[results.find("{") : results.rfind("}") + 1]
|
|
185
|
+
try:
|
|
186
|
+
results = json.loads(results, strict=False)
|
|
187
|
+
except Exception:
|
|
188
|
+
print(f"==== Invalid JSON:\n{results}")
|
|
189
|
+
log(f"==== Invalid JSON:\n{results}")
|
|
190
|
+
return {}
|
|
191
|
+
else:
|
|
192
|
+
log(f"==== Results: {results} ====")
|
|
193
|
+
print(results)
|
|
194
|
+
print("=================== END REPORT ===================")
|
|
195
|
+
return results
|
|
196
|
+
|
|
197
|
+
def generate_text(self, messages, additional_instructions=""):
|
|
198
|
+
formatted_messages = self._format_messages(messages)
|
|
199
|
+
thread = self.client.beta.threads.create(messages=formatted_messages)
|
|
200
|
+
|
|
201
|
+
run = self.client.beta.threads.runs.create(
|
|
202
|
+
thread_id=thread.id,
|
|
203
|
+
assistant_id=self._get_agent_id(),
|
|
204
|
+
additional_instructions=additional_instructions,
|
|
205
|
+
parallel_tool_calls=False,
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
while run.status in ["queued", "in_progress"]:
|
|
209
|
+
run = self.client.beta.threads.runs.retrieve(
|
|
210
|
+
thread_id=thread.id,
|
|
211
|
+
run_id=run.id,
|
|
212
|
+
)
|
|
213
|
+
time.sleep(0.5)
|
|
214
|
+
log(f"==== Job Status: {run.status} ====")
|
|
215
|
+
print(f"==== Job Status: {run.status} ====")
|
|
216
|
+
|
|
217
|
+
if run.status in ["failed", "expired", "canceled"]:
|
|
218
|
+
log(f"==== Error: {run.last_error} ====")
|
|
219
|
+
print(f"==== Error: {run.last_error} ====")
|
|
220
|
+
return None
|
|
221
|
+
print("=================== RUN COMPLETED ===================")
|
|
222
|
+
print(run.status)
|
|
223
|
+
if run.status == "completed":
|
|
224
|
+
response = self.client.beta.threads.messages.list(thread_id=thread.id)
|
|
225
|
+
results = response.data[0].content[0].text.value
|
|
226
|
+
else:
|
|
227
|
+
log(f"====Status: {run.status} Error: {run.last_error} ====")
|
|
228
|
+
print(f"====Status: {run.status} Error: {run.last_error} ====")
|
|
229
|
+
return None
|
|
230
|
+
|
|
231
|
+
print(results)
|
|
232
|
+
print("=================== END REPORT ===================")
|
|
233
|
+
return results
|
|
234
|
+
|
|
235
|
+
def generate_audio(self, prompt, file_path, **kwargs):
|
|
236
|
+
voice = kwargs.get("voice") or random.choice(
|
|
237
|
+
["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
|
|
238
|
+
)
|
|
239
|
+
response = self.client.audio.speech.create(
|
|
240
|
+
model="tts-1",
|
|
241
|
+
voice=voice,
|
|
242
|
+
input=prompt,
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
return response.stream_to_file(file_path)
|
|
246
|
+
|
|
247
|
+
def generate_image(self, prompt, **kwargs):
|
|
248
|
+
image = None
|
|
249
|
+
try:
|
|
250
|
+
response = self.client.images.generate(
|
|
251
|
+
model=self.image_model,
|
|
252
|
+
prompt=prompt,
|
|
253
|
+
response_format="b64_json",
|
|
254
|
+
**kwargs,
|
|
255
|
+
)
|
|
256
|
+
image_dict = response.data[0]
|
|
257
|
+
except Exception as e:
|
|
258
|
+
print(f"==== Error: Unable to create image ====\n\n{e}")
|
|
259
|
+
else:
|
|
260
|
+
image = b64decode(image_dict.b64_json)
|
|
261
|
+
return image
|
|
262
|
+
|
|
263
|
+
def summarize_text(self, text, primer=""):
|
|
264
|
+
message = [
|
|
265
|
+
{
|
|
266
|
+
"role": "system",
|
|
267
|
+
"content": f"You are a highly skilled AI trained in language comprehension and summarization.{primer}",
|
|
268
|
+
},
|
|
269
|
+
{"role": "user", "content": text},
|
|
270
|
+
]
|
|
271
|
+
response = self.client.chat.completions.create(
|
|
272
|
+
model="gpt-4o-mini", messages=message
|
|
273
|
+
)
|
|
274
|
+
try:
|
|
275
|
+
result = response.choices[0].message.content
|
|
276
|
+
except Exception as e:
|
|
277
|
+
log(f"{type(e)}:{e}\n\n Unable to generate content ====")
|
|
278
|
+
return None
|
|
279
|
+
|
|
280
|
+
return result
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
import random
|
|
5
|
+
import time
|
|
6
|
+
from base64 import b64decode
|
|
7
|
+
|
|
8
|
+
from openai import OpenAI
|
|
9
|
+
from pydantic import model_validator
|
|
10
|
+
|
|
11
|
+
from autonomous import log
|
|
12
|
+
from autonomous.model.autoattr import ReferenceAttr
|
|
13
|
+
from autonomous.model.automodel import AutoModel
|
|
14
|
+
|
|
15
|
+
from .models.openai import OpenAIModel
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class OAIAgent(AutoModel):
|
|
19
|
+
client_model = ReferenceAttr()
|
|
20
|
+
_ai_model = OpenAIModel
|
|
21
|
+
|
|
22
|
+
@property
|
|
23
|
+
def client(self):
|
|
24
|
+
if self.client_model is None:
|
|
25
|
+
self.client_model = self._ai_model()
|
|
26
|
+
self.save()
|
|
27
|
+
return self.client_model
|
|
28
|
+
|
|
29
|
+
def __init__(self, **kwargs):
|
|
30
|
+
self._client = OpenAI(api_key=os.environ.get("OPENAI_KEY"))
|
|
31
|
+
|
|
32
|
+
def clear_files(self, file_id=None):
|
|
33
|
+
return self.client.clear_files(file_id)
|
|
34
|
+
|
|
35
|
+
def attach_file(self, file_contents, filename="dbdata.json"):
|
|
36
|
+
return self.client.attach_file(file_contents, filename)
|
|
37
|
+
|
|
38
|
+
def generate(self, messages, function=None, additional_instructions=""):
|
|
39
|
+
if function is None:
|
|
40
|
+
return self.client.generate_text(messages, additional_instructions)
|
|
41
|
+
else:
|
|
42
|
+
return self.client.generate_json(
|
|
43
|
+
messages, function, additional_instructions
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
def generate_audio(self, prompt, file_path, **kwargs):
|
|
47
|
+
return self.client.generate_audio(self, file_path, **kwargs)
|
|
48
|
+
|
|
49
|
+
def generate_image(self, prompt, **kwargs):
|
|
50
|
+
return self.client.generate_image(prompt, **kwargs)
|
|
51
|
+
|
|
52
|
+
def summarize_text(self, text, primer=""):
|
|
53
|
+
return self.client.generate_image(text, primer)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from autonomous import log
|
|
2
|
+
from autonomous.model.autoattr import ReferenceAttr, StringAttr
|
|
3
|
+
from autonomous.model.automodel import AutoModel
|
|
4
|
+
|
|
5
|
+
from .models.openai import OpenAIModel
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TextAgent(AutoModel):
|
|
9
|
+
client = ReferenceAttr()
|
|
10
|
+
name = StringAttr(default="textagent")
|
|
11
|
+
instructions = StringAttr(
|
|
12
|
+
default="You are highly skilled AI trained to assist with generating text according to the given requirements."
|
|
13
|
+
)
|
|
14
|
+
description = StringAttr(
|
|
15
|
+
default="A helpful AI assistant trained to assist with generating text according to the given requirements."
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
_ai_model = OpenAIModel
|
|
19
|
+
|
|
20
|
+
def get_client(self):
|
|
21
|
+
if self.client is None:
|
|
22
|
+
self.client = self._ai_model(
|
|
23
|
+
name=self.name,
|
|
24
|
+
instructions=self.instructions,
|
|
25
|
+
description=self.description,
|
|
26
|
+
)
|
|
27
|
+
self.client.save()
|
|
28
|
+
self.save()
|
|
29
|
+
return self.client
|
|
30
|
+
|
|
31
|
+
def summarize_text(self, text, primer=""):
|
|
32
|
+
return self.get_client().summarize_text(text, primer)
|
|
33
|
+
|
|
34
|
+
def generate(self, messages, additional_instructions=""):
|
|
35
|
+
return self.get_client().generate_text(messages, additional_instructions)
|
|
@@ -4,14 +4,14 @@ from functools import wraps
|
|
|
4
4
|
|
|
5
5
|
import requests
|
|
6
6
|
from authlib.integrations.requests_client import OAuth2Auth, OAuth2Session
|
|
7
|
-
from flask import
|
|
7
|
+
from flask import redirect, session, url_for
|
|
8
8
|
|
|
9
9
|
from autonomous import log
|
|
10
10
|
from autonomous.auth.user import AutoUser
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
class AutoAuth:
|
|
14
|
-
user_class = AutoUser
|
|
14
|
+
user_class: type[AutoUser] = AutoUser
|
|
15
15
|
|
|
16
16
|
def __init__(
|
|
17
17
|
self,
|
|
@@ -5,7 +5,7 @@ This module provides a User class that uses the OpenIDAuth class for authenticat
|
|
|
5
5
|
from datetime import datetime
|
|
6
6
|
|
|
7
7
|
from autonomous import log
|
|
8
|
-
from autonomous.model.
|
|
8
|
+
from autonomous.model.autoattr import DateTimeAttr, StringAttr
|
|
9
9
|
from autonomous.model.automodel import AutoModel
|
|
10
10
|
|
|
11
11
|
|
|
@@ -14,15 +14,13 @@ class AutoUser(AutoModel):
|
|
|
14
14
|
This class represents a user who can authenticate using OpenID.
|
|
15
15
|
"""
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"token": None,
|
|
25
|
-
}
|
|
17
|
+
name = StringAttr(default="Anonymous")
|
|
18
|
+
email = StringAttr(required=True)
|
|
19
|
+
last_login = DateTimeAttr(default=datetime.now)
|
|
20
|
+
state = StringAttr(default="unauthenticated")
|
|
21
|
+
provider = StringAttr()
|
|
22
|
+
role = StringAttr(default="guest")
|
|
23
|
+
token = StringAttr()
|
|
26
24
|
|
|
27
25
|
def __eq__(self, other):
|
|
28
26
|
return self.pk == other.pk
|