├── .gitignore ├── LICENSE ├── README.md ├── app ├── __init__.py ├── agent.py └── api.py ├── examples └── simple_chat.html ├── requirements.txt └── run.py /.gitignore: -------------------------------------------------------------------------------- 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 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 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 | .DS_Store 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Adam Cohen Hillel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LLMitlessAPI 2 | API with one endpoint that does everything. 3 | 4 | ### Use Cases: 5 | 1. Being able to create a -working- PoC of your product in a friction of tha time 6 | 2. No more 404? be ambious and send them to this endpoint! 7 | 3. 8 | -------------------------------------------------------------------------------- /app/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adamcohenhillel/LLMitlessAPI/defadc15b7b9ba280c95ae84b0ad0f8877d127d9/app/__init__.py -------------------------------------------------------------------------------- /app/agent.py: -------------------------------------------------------------------------------- 1 | import os 2 | import json 3 | from typing import Dict 4 | 5 | import openai 6 | from openai.embeddings_utils import distances_from_embeddings 7 | import pandas as pd 8 | import tiktoken 9 | 10 | 11 | openai.api_key = os.environ.get('OPENAI_APIKEY') 12 | memory = pd.DataFrame([], columns = ['text', 'embeddings', 'n_tokens', 'distances']) 13 | 14 | ACTIONS: dict = { 15 | "SAVE": "Save information to database, so you can retrieve it later", 16 | "FETCH": "Fetch information from database", 17 | "EVAL": "Execute a python expression that is present inside a string", 18 | "FINISHED": "Finish serving the request", 19 | } 20 | 21 | 22 | def fetch(memory: pd.DataFrame, to_retrieve: str, depth: int = 50) -> str: 23 | """Trying to retrieve information from memory 24 | 25 | :param memory: A dataframe of the texts and their embeddings 26 | :param retrieve: The information to retrieve from memory 27 | :param depth: How many results to return 28 | 29 | :return: The information retrieved from memory 30 | """ 31 | to_retrieve = str(to_retrieve).replace("{", " ").replace("}", " ") 32 | info_embedded = openai.Embedding.create( 33 | input=to_retrieve, 34 | engine='text-embedding-ada-002' 35 | )['data'][0]['embedding'] 36 | 37 | # Get the distances from the embeddings 38 | memory['distances'] = distances_from_embeddings( 39 | info_embedded, 40 | memory['embeddings'].values, 41 | distance_metric='cosine' 42 | ) 43 | 44 | relevant_memories = [] 45 | for _, row in memory.sort_values('distances', ascending=True).head(depth).iterrows(): 46 | relevant_memories.append(row["text"]) 47 | 48 | return "\n###\n".join(relevant_memories) 49 | 50 | 51 | def save(memory: pd.DataFrame, information: str) -> pd.DataFrame: 52 | """Saving information to memory (embeddings) 53 | 54 | :param memory: A dataframe of the texts and their embeddings 55 | :param information: The information to save to memory 56 | 57 | :return: Updated dataframe of the memory with the new information 58 | """ 59 | information = str(information).replace("{", " ").replace("}", " ") 60 | tokenizer: tiktoken.Encoding = tiktoken.get_encoding("cl100k_base") 61 | n_tokens = len(tokenizer.encode(information)) 62 | embeddings = openai.Embedding.create( 63 | input=str(information), 64 | engine='text-embedding-ada-002' 65 | )['data'][0]['embedding'] 66 | new_memory = pd.concat([ 67 | memory, 68 | pd.DataFrame([{'text': information, 'n_tokens': n_tokens, 'embeddings': embeddings}]) 69 | ], ignore_index=True) 70 | return new_memory 71 | 72 | 73 | def thought_process(conversation_context: list, service: str, data: dict): 74 | """Uses to decide what to do next, based on the previous actions and the constraints of the agent's world. 75 | """ 76 | 77 | meta_prompt = '''You are autonomus agent called "assistant" which act as an API service. You are given a service to act as, and a body data to act on. 78 | To serve the request, you must choose one of following actions, one at a time, until you finish serving the request. 79 | Actions: 80 | 81 | 82 | Rules: 83 | 1. As "assistant", you MUST response only with the following JSON format:\n{"action": "", "info": ""} 84 | 2. Action must be one of the provided actions above. 85 | 3. The responses from "user" are the results of the action you performed. Use them to choose your next action. 86 | 4. When finished serving a request, include the end response to the user with the FINISHED action. 87 | ''' 88 | 89 | meta_prompt = meta_prompt.replace("", "\n".join([f"{i+1}. {k} - {v}" for i, (k, v) in enumerate(ACTIONS.items())])) 90 | 91 | conversation_context = [ 92 | {"role": "system", "content": meta_prompt}, 93 | {"role": "user", "content": f"Service: {service}\nBody Data: {data}"}, 94 | *conversation_context 95 | ] 96 | 97 | try: 98 | response = openai.ChatCompletion.create( 99 | model="gpt-4", 100 | messages=conversation_context, 101 | ) 102 | message = response["choices"][0]["message"] 103 | return message 104 | except Exception as e: 105 | print(e) 106 | return "" 107 | 108 | 109 | def agent_loop(task_id: str, tasks_status: Dict, service: str, data: str): 110 | """The main loop of the agent 111 | 112 | """ 113 | global memory 114 | print(f'\33[36m\nBooting agent for task: {task_id}...\33[0m') 115 | 116 | conversation_context = [] 117 | running = True 118 | 119 | while running: 120 | 121 | # Choose the next action: 122 | action = thought_process( 123 | conversation_context, 124 | service, 125 | data 126 | ) 127 | conversation_context.append(action) 128 | 129 | try: 130 | action_content = json.loads(action["content"]) 131 | 132 | # Execute the action: 133 | if action_content['action'] == 'SAVE': 134 | print(f"\33[0;37m\nSAVE: {action_content['info']}\33[0m") 135 | memory = save(memory, action_content['info']) 136 | conversation_context.append({"role": "user", "content": "SAVED."}) 137 | 138 | elif action_content['action'] == 'FETCH': 139 | print(f"\33[0;37m\nFetch query: {action_content['info']}\33[0m") 140 | remembered = fetch(memory, action_content['info']) or "Nothing found." 141 | print(f"\nFecthed: {remembered}") 142 | conversation_context.append({"role": "user", "content": f"Remembered: {remembered}"}) 143 | 144 | elif action_content['action'] == 'EXECUTE': 145 | print(f"\33[0;37m\nExecute: {action_content['info']}\33[0m") 146 | try: 147 | result = eval(action_content['info']) 148 | print(f"\nCode Result: {result}") 149 | conversation_context.append({"role": "user", "content": result}) 150 | except Exception as e: 151 | print(e) 152 | 153 | elif action_content['action'] == 'FINISHED': 154 | tasks_status[task_id] = str(action_content['info']) 155 | print(f"\33[32m\nFinished: {action_content['info']}\33[0m") 156 | running = False 157 | break 158 | print('\n-----------------------------------') 159 | 160 | except Exception as e: 161 | tasks_status[task_id] = "ERRORED" 162 | print(f"\33[31m\nError: {e}\33[0m") 163 | raise e 164 | 165 | 166 | if __name__ == "__main__": 167 | ## only for testing 168 | agent_loop( 169 | service = "Get all Adam's messages", 170 | data = "", 171 | task_id="1", 172 | tasks_status={} 173 | ) 174 | -------------------------------------------------------------------------------- /app/api.py: -------------------------------------------------------------------------------- 1 | """LLMitlessAPI 2 | """ 3 | from uuid import uuid4 4 | from typing import Dict 5 | 6 | from fastapi import FastAPI, BackgroundTasks 7 | from fastapi.middleware.cors import CORSMiddleware 8 | from pydantic import BaseModel 9 | 10 | from app.agent import agent_loop 11 | 12 | 13 | app = FastAPI() 14 | 15 | origins = ["*"] 16 | 17 | app.add_middleware( 18 | CORSMiddleware, 19 | allow_origins=origins, 20 | allow_credentials=True, 21 | allow_methods=["*"], 22 | allow_headers=["*"], 23 | ) 24 | 25 | class RequestBody(BaseModel): 26 | """ 27 | """ 28 | service: str 29 | data: str 30 | 31 | 32 | tasks_status: Dict[str, str] = {} 33 | 34 | 35 | @app.post('/') 36 | async def trigger_new_task( 37 | body: RequestBody, 38 | background_tasks: BackgroundTasks 39 | ) -> Dict[str, str]: 40 | new_task_id = str(uuid4()) 41 | background_tasks.add_task( 42 | agent_loop, 43 | task_id=new_task_id, 44 | tasks_status=tasks_status, 45 | service=body.service, 46 | data=body.data 47 | ) 48 | tasks_status[new_task_id] = "running" 49 | return {"task_id": new_task_id} 50 | 51 | 52 | @app.get("/{task_id}") 53 | async def check_task(task_id: str) -> Dict[str, str]: 54 | if task_id in tasks_status: 55 | status = tasks_status[task_id] 56 | else: 57 | status = "not found" 58 | return {"result": status} -------------------------------------------------------------------------------- /examples/simple_chat.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Simple HTML Chat 5 | 47 | 48 | 49 |
50 | 51 | 52 |
53 |
    54 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | #API: 2 | fastapi==0.94.1 3 | pydantic==1.9.1 4 | uvicorn[standard]==0.21.0 5 | # firebase-admin==6.0.1 6 | 7 | # CI/CD; 8 | pytest==7.1.2 9 | pytest-asyncio==0.18.3 10 | flake8==4.0.1 11 | 12 | # openai tutorial: 13 | aiohttp==3.8.3 14 | aiosignal==1.3.1 15 | appnope==0.1.3 16 | asttokens==2.2.1 17 | async-timeout==4.0.2 18 | attrs==22.2.0 19 | backcall==0.2.0 20 | beautifulsoup4==4.11.1 21 | blobfile==2.0.1 22 | bs4==0.0.1 23 | certifi==2022.12.7 24 | charset-normalizer==2.1.1 25 | comm==0.1.2 26 | contourpy==1.0.7 27 | cycler==0.11.0 28 | debugpy==1.6.5 29 | decorator==5.1.1 30 | docopt==0.6.2 31 | entrypoints==0.4 32 | executing==1.2.0 33 | filelock==3.9.0 34 | fonttools==4.38.0 35 | frozenlist==1.3.3 36 | huggingface-hub==0.11.1 37 | idna==3.4 38 | ipykernel==6.20.1 39 | ipython==8.8.0 40 | jedi==0.18.2 41 | joblib==1.2.0 42 | jupyter_client==7.4.8 43 | jupyter_core==5.1.3 44 | kiwisolver==1.4.4 45 | # lxml==4.9.2 46 | matplotlib==3.6.3 47 | matplotlib-inline==0.1.6 48 | multidict==6.0.4 49 | nest-asyncio==1.5.6 50 | numpy==1.24.1 51 | openai==0.27.0 52 | packaging==23.0 53 | pandas==1.5.2 54 | parso==0.8.3 55 | pexpect==4.8.0 56 | pickleshare==0.7.5 57 | Pillow==9.4.0 58 | pipreqs==0.4.11 59 | platformdirs==2.6.2 60 | plotly==5.12.0 61 | prompt-toolkit==3.0.36 62 | psutil==5.9.4 63 | ptyprocess==0.7.0 64 | pure-eval==0.2.2 65 | pycryptodomex==3.17 66 | Pygments==2.14.0 67 | pyparsing==3.0.9 68 | python-dateutil==2.8.2 69 | pytz==2022.7.1 70 | PyYAML==6.0 71 | pyzmq==24.0.1 72 | regex==2022.10.31 73 | requests==2.28.1 74 | scikit-learn==1.2.0 75 | scipy==1.10.0 76 | six==1.16.0 77 | soupsieve==2.3.2.post1 78 | stack-data==0.6.2 79 | tenacity==8.1.0 80 | threadpoolctl==3.1.0 81 | tiktoken==0.1.2 82 | tokenizers==0.13.2 83 | tornado==6.2 84 | tqdm==4.64.1 85 | traitlets==5.8.1 86 | transformers==4.25.1 87 | typing_extensions==4.4.0 88 | urllib3==1.26.13 89 | wcwidth==0.2.5 90 | yarg==0.1.9 91 | yarl==1.8.2 -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | """LLMitlessAPI 2 | """ 3 | import uvicorn 4 | 5 | from app.api import app 6 | 7 | if __name__ == '__main__': 8 | print('\33[36mAPI Running.\33[0m') 9 | uvicorn.run(app, host='0.0.0.0', port=8000, log_level="critical") 10 | --------------------------------------------------------------------------------