Spaces:
Runtime error
Runtime error
first commit
Browse files- .gitignore +160 -0
- app.py +47 -0
- calling_functions.py +54 -0
- chat_settings.py +68 -0
- requirements.txt +5 -0
.gitignore
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# pdm
|
105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
106 |
+
#pdm.lock
|
107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
108 |
+
# in version control.
|
109 |
+
# https://pdm.fming.dev/#use-with-ide
|
110 |
+
.pdm.toml
|
111 |
+
|
112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
113 |
+
__pypackages__/
|
114 |
+
|
115 |
+
# Celery stuff
|
116 |
+
celerybeat-schedule
|
117 |
+
celerybeat.pid
|
118 |
+
|
119 |
+
# SageMath parsed files
|
120 |
+
*.sage.py
|
121 |
+
|
122 |
+
# Environments
|
123 |
+
.env
|
124 |
+
.venv
|
125 |
+
env/
|
126 |
+
venv/
|
127 |
+
ENV/
|
128 |
+
env.bak/
|
129 |
+
venv.bak/
|
130 |
+
|
131 |
+
# Spyder project settings
|
132 |
+
.spyderproject
|
133 |
+
.spyproject
|
134 |
+
|
135 |
+
# Rope project settings
|
136 |
+
.ropeproject
|
137 |
+
|
138 |
+
# mkdocs documentation
|
139 |
+
/site
|
140 |
+
|
141 |
+
# mypy
|
142 |
+
.mypy_cache/
|
143 |
+
.dmypy.json
|
144 |
+
dmypy.json
|
145 |
+
|
146 |
+
# Pyre type checker
|
147 |
+
.pyre/
|
148 |
+
|
149 |
+
# pytype static type analyzer
|
150 |
+
.pytype/
|
151 |
+
|
152 |
+
# Cython debug symbols
|
153 |
+
cython_debug/
|
154 |
+
|
155 |
+
# PyCharm
|
156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
+
#.idea/
|
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import streamlit as st
|
3 |
+
from streamlit_chat import message
|
4 |
+
import os
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
from chat_settings import (
|
8 |
+
get_initial_message,
|
9 |
+
get_chatgpt_response,
|
10 |
+
update_chat,
|
11 |
+
)
|
12 |
+
|
13 |
+
|
14 |
+
# Carga las claves
|
15 |
+
load_dotenv()
|
16 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
17 |
+
LLM = "gpt-3.5-turbo-0613"
|
18 |
+
|
19 |
+
|
20 |
+
# Streamlit Application
|
21 |
+
def main():
|
22 |
+
st.title("ProPilot - Implementando Functions Calling")
|
23 |
+
st.markdown(
|
24 |
+
"""
|
25 |
+
Esta es una demo en la cual se busca implementar de manera sencilla las functions calling de la API de OpenAI.
|
26 |
+
"""
|
27 |
+
)
|
28 |
+
|
29 |
+
if 'messages' not in st.session_state:
|
30 |
+
st.session_state['messages'] = get_initial_message()
|
31 |
+
|
32 |
+
query = st.text_input("Ingresa tu texto")
|
33 |
+
|
34 |
+
if st.button("Enviar") and query:
|
35 |
+
st.session_state['messages'] = update_chat(st.session_state['messages'], "user", query)
|
36 |
+
chatgpt_response = get_chatgpt_response(st.session_state['messages'], LLM)
|
37 |
+
st.session_state['messages'] = update_chat(st.session_state['messages'], "assistant", chatgpt_response)
|
38 |
+
|
39 |
+
if st.session_state['messages']:
|
40 |
+
for i, msg in enumerate(st.session_state['messages']):
|
41 |
+
if msg['role'] == 'user':
|
42 |
+
message(msg['content'], is_user=True, key=str(i))
|
43 |
+
else:
|
44 |
+
message(msg['content'], key=str(i))
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
main()
|
calling_functions.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
|
3 |
+
|
4 |
+
# Definici贸n de esquema para Calling Function
|
5 |
+
ADD_DECIMAL_AND_HEXADECIMAL_FUNCTION_SCHEMA = [
|
6 |
+
{
|
7 |
+
"name": "add_decimal_values",
|
8 |
+
"description": "Add two decimal values",
|
9 |
+
"parameters": {
|
10 |
+
"type": "object",
|
11 |
+
"properties": {
|
12 |
+
"value1": {
|
13 |
+
"type": "integer",
|
14 |
+
"description": "The first decimal value to add. For example, 5",
|
15 |
+
},
|
16 |
+
"value2": {
|
17 |
+
"type": "integer",
|
18 |
+
"description": "The second decimal value to add. For example, 10",
|
19 |
+
},
|
20 |
+
},
|
21 |
+
"required": ["value1", "value2"],
|
22 |
+
},
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"name": "add_hexadecimal_values",
|
26 |
+
"description": "Add two hexadecimal values",
|
27 |
+
"parameters": {
|
28 |
+
"type": "object",
|
29 |
+
"properties": {
|
30 |
+
"value1": {
|
31 |
+
"type": "string",
|
32 |
+
"description": "The first hexadecimal value to add. For example, 5",
|
33 |
+
},
|
34 |
+
"value2": {
|
35 |
+
"type": "string",
|
36 |
+
"description": "The second hexadecimal value to add. For example, A",
|
37 |
+
},
|
38 |
+
},
|
39 |
+
"required": ["value1", "value2"],
|
40 |
+
},
|
41 |
+
},
|
42 |
+
]
|
43 |
+
|
44 |
+
|
45 |
+
# Definici贸n de las funciones
|
46 |
+
def add_decimal_values(arguments):
|
47 |
+
v1 = int(re.search(r'"value1": (\d+)', str(arguments)).group(1))
|
48 |
+
v2 = int(re.search(r'"value2": (\d+)', str(arguments)).group(1))
|
49 |
+
return v1 + v2
|
50 |
+
|
51 |
+
def add_hexadecimal_values(arguments):
|
52 |
+
v1 = re.search(r'"value1": "(\w+)"', str(arguments)).group(1)
|
53 |
+
v2 = re.search(r'"value2": "(\w+)"', str(arguments)).group(1)
|
54 |
+
return hex(int(v1, 16) + int(v2, 16))[2:]
|
chat_settings.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import os
|
3 |
+
|
4 |
+
from calling_functions import (
|
5 |
+
ADD_DECIMAL_AND_HEXADECIMAL_FUNCTION_SCHEMA,
|
6 |
+
add_decimal_values, # noqa
|
7 |
+
add_hexadecimal_values, # noqa
|
8 |
+
)
|
9 |
+
|
10 |
+
# Definici贸n de las funciones de Chat
|
11 |
+
def get_initial_message():
|
12 |
+
messages = [
|
13 |
+
{
|
14 |
+
"role": "system",
|
15 |
+
"content": "Hola, soy ProPilot. Si deseas probar el function calling que tengo configurado solo pregunta: Cual es la suma de 24 y el valor hexadecimal F?",
|
16 |
+
},
|
17 |
+
]
|
18 |
+
return messages
|
19 |
+
|
20 |
+
|
21 |
+
def get_chatgpt_response(messages, model):
|
22 |
+
intermediate_results = []
|
23 |
+
|
24 |
+
while True:
|
25 |
+
response = openai.ChatCompletion.create(
|
26 |
+
model=model,
|
27 |
+
messages=messages,
|
28 |
+
functions=ADD_DECIMAL_AND_HEXADECIMAL_FUNCTION_SCHEMA,
|
29 |
+
temperature=0,
|
30 |
+
headers={
|
31 |
+
"Helicone-Auth": os.getenv('HELICONE_API_KEY'),
|
32 |
+
"Helicone-Cache-Enabled": "true",
|
33 |
+
}
|
34 |
+
)
|
35 |
+
|
36 |
+
if response.choices[0]["finish_reason"] == "stop":
|
37 |
+
final_answer = response.choices[0]["message"]["content"]
|
38 |
+
return final_answer
|
39 |
+
|
40 |
+
elif response.choices[0]["finish_reason"] == "function_call":
|
41 |
+
fn_name = response.choices[0]["message"]["function_call"]["name"]
|
42 |
+
arguments = response.choices[0]["message"]["function_call"]["arguments"]
|
43 |
+
|
44 |
+
function = globals()[fn_name]
|
45 |
+
result = function(arguments)
|
46 |
+
|
47 |
+
if isinstance(result, dict) and "result" in result:
|
48 |
+
result = result["result"]
|
49 |
+
|
50 |
+
intermediate_results.append(str(result))
|
51 |
+
|
52 |
+
# Remove intermediate results from the messages
|
53 |
+
messages = messages[:-len(intermediate_results)]
|
54 |
+
|
55 |
+
# Append the final answer as a system message
|
56 |
+
messages.append(
|
57 |
+
{
|
58 |
+
"role": "system",
|
59 |
+
"content": intermediate_results[-1]
|
60 |
+
}
|
61 |
+
)
|
62 |
+
|
63 |
+
|
64 |
+
def update_chat(messages, role, content):
|
65 |
+
messages.append(
|
66 |
+
{"role": role, "content": content},
|
67 |
+
)
|
68 |
+
return messages
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
streamlit_chat
|
3 |
+
openai
|
4 |
+
python-dotenv
|
5 |
+
helicone
|