Skip to content
18 changes: 7 additions & 11 deletions interpreter/core/async_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,17 +700,11 @@ async def chat_completion(request: ChatCompletionRequest):
return router


host = os.getenv(
"HOST", "127.0.0.1"
) # IP address for localhost, used for local testing. To expose to local network, use 0.0.0.0
port = int(os.getenv("PORT", 8000)) # Default port is 8000

# FOR TESTING ONLY
# host = "0.0.0.0"


class Server:
def __init__(self, async_interpreter, host="127.0.0.1", port=8000):
DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 8000

def __init__(self, async_interpreter, host=None, port=None):
self.app = FastAPI()
router = create_router(async_interpreter)
self.authenticate = authenticate_function
Expand All @@ -729,7 +723,9 @@ async def validate_api_key(request: Request, call_next):
)

self.app.include_router(router)
self.config = uvicorn.Config(app=self.app, host=host, port=port)
h = host or os.getenv("HOST", Server.DEFAULT_HOST)
p = port or int(os.getenv("PORT", Server.DEFAULT_PORT))
self.config = uvicorn.Config(app=self.app, host=h, port=p)
self.uvicorn_server = uvicorn.Server(self.config)

@property
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
This is an Open Interpreter profile to chat with a database.
"""

from interpreter import interpreter
from datetime import date
import os

# Use environment variables for database connection or update defaults with your credentials
db_user = os.environ.get("DB_USER", "user")
db_host = os.environ.get("DB_HOST", "localhost")
db_port = os.environ.get("DB_PORT", "5432")
db_name = os.environ.get("DB_NAME", "demo_database")
db_password = os.environ.get("DB_PASSWORD", "")

# Construct connection string with optional password
if db_password and db_password.strip():
connection_string = (
f"postgresql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
)
else:
connection_string = f"postgresql://{db_user}@{db_host}:{db_port}/{db_name}"


# LLM settings
interpreter.llm.model = "ollama/llama3.1"
interpreter.llm.supports_functions = False
interpreter.llm.execution_instructions = False
interpreter.llm.max_tokens = 1000
interpreter.llm.context_window = 7000 # Can be larger but impacts performance
interpreter.llm.load() # Loads Ollama models

# Computer settings
interpreter.computer.import_computer_api = False

# Misc settings
interpreter.auto_run = False
interpreter.offline = True

# Custom Instructions
interpreter.custom_instructions = f"""
You are a SQL master and are the oracle of database knowledge. You are obsessed with SQL. You only want to discuss SQL. SQL is life.
Recap the plan before answering the user's query.
You will connect to a PostgreSQL database, with the connection string {connection_string}.
Remember to only query the {db_name} database.
Execute valid SQL commands to satisfy the user's query.
Write all code in a full Python script. When you have to re-write code, redo the entire script.
Execute the script to get the answer for the user's query.
**YOU CAN EXECUTE SQL COMMANDS IN A PYTHON SCRIPT.***
Get the schema of '{db_name}' before writing any other SQL commands. It is important to know the tables. This will let you know what commands are correct.
Only use real column names.
***You ARE fully capable of executing SQL commands.***
Be VERY clear about the answer to the user's query. They don't understand technical jargon so make it very clear and direct.
Today's date is {date.today()}.
You should respond in a very concise way.
You can do it, I believe in you.
"""
48 changes: 48 additions & 0 deletions tests/core/test_async_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
from unittest import TestCase, mock

from interpreter.core.async_core import Server, AsyncInterpreter


class TestServerConstruction(TestCase):
"""
Tests to make sure that the underlying server is configured correctly when constructing
the Server object.
"""

def test_host_and_port_defaults(self):
"""
Tests that a Server object takes on the default host and port when
a) no host and port are passed in, and
b) no HOST and PORT are set.
"""
with mock.patch.dict(os.environ, {}):
s = Server(AsyncInterpreter())
self.assertEqual(s.host, Server.DEFAULT_HOST)
self.assertEqual(s.port, Server.DEFAULT_PORT)

def test_host_and_port_passed_in(self):
"""
Tests that a Server object takes on the passed-in host and port when they are passed-in,
ignoring the surrounding HOST and PORT env vars.
"""
host = "the-really-real-host"
port = 2222

with mock.patch.dict(os.environ, {"HOST": "this-is-supes-fake", "PORT": "9876"}):
sboth = Server(AsyncInterpreter(), host, port)
self.assertEqual(sboth.host, host)
self.assertEqual(sboth.port, port)

def test_host_and_port_from_env_1(self):
"""
Tests that the Server object takes on the HOST and PORT env vars as host and port when
nothing has been passed in.
"""
fake_host = "fake_host"
fake_port = 1234

with mock.patch.dict(os.environ, {"HOST": fake_host, "PORT": str(fake_port)}):
s = Server(AsyncInterpreter())
self.assertEqual(s.host, fake_host)
self.assertEqual(s.port, fake_port)