Skip to content

Commit 54e2704

Browse files
authored
Merge pull request #1365 from imapersonman/fix-host-port-from-env
Fix: Unused HOST and PORT environment variables
2 parents e19b890 + 93346ba commit 54e2704

File tree

3 files changed

+112
-11
lines changed

3 files changed

+112
-11
lines changed

interpreter/core/async_core.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -700,17 +700,11 @@ async def chat_completion(request: ChatCompletionRequest):
700700
return router
701701

702702

703-
host = os.getenv(
704-
"HOST", "127.0.0.1"
705-
) # IP address for localhost, used for local testing. To expose to local network, use 0.0.0.0
706-
port = int(os.getenv("PORT", 8000)) # Default port is 8000
707-
708-
# FOR TESTING ONLY
709-
# host = "0.0.0.0"
710-
711-
712703
class Server:
713-
def __init__(self, async_interpreter, host="127.0.0.1", port=8000):
704+
DEFAULT_HOST = "127.0.0.1"
705+
DEFAULT_PORT = 8000
706+
707+
def __init__(self, async_interpreter, host=None, port=None):
714708
self.app = FastAPI()
715709
router = create_router(async_interpreter)
716710
self.authenticate = authenticate_function
@@ -729,7 +723,9 @@ async def validate_api_key(request: Request, call_next):
729723
)
730724

731725
self.app.include_router(router)
732-
self.config = uvicorn.Config(app=self.app, host=host, port=port)
726+
h = host or os.getenv("HOST", Server.DEFAULT_HOST)
727+
p = port or int(os.getenv("PORT", Server.DEFAULT_PORT))
728+
self.config = uvicorn.Config(app=self.app, host=h, port=p)
733729
self.uvicorn_server = uvicorn.Server(self.config)
734730

735731
@property
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
This is an Open Interpreter profile to chat with a database.
3+
"""
4+
5+
from interpreter import interpreter
6+
from datetime import date
7+
import os
8+
9+
# Use environment variables for database connection or update defaults with your credentials
10+
db_user = os.environ.get("DB_USER", "user")
11+
db_host = os.environ.get("DB_HOST", "localhost")
12+
db_port = os.environ.get("DB_PORT", "5432")
13+
db_name = os.environ.get("DB_NAME", "demo_database")
14+
db_password = os.environ.get("DB_PASSWORD", "")
15+
16+
# Construct connection string with optional password
17+
if db_password and db_password.strip():
18+
connection_string = (
19+
f"postgresql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
20+
)
21+
else:
22+
connection_string = f"postgresql://{db_user}@{db_host}:{db_port}/{db_name}"
23+
24+
25+
# LLM settings
26+
interpreter.llm.model = "ollama/llama3.1"
27+
interpreter.llm.supports_functions = False
28+
interpreter.llm.execution_instructions = False
29+
interpreter.llm.max_tokens = 1000
30+
interpreter.llm.context_window = 7000 # Can be larger but impacts performance
31+
interpreter.llm.load() # Loads Ollama models
32+
33+
# Computer settings
34+
interpreter.computer.import_computer_api = False
35+
36+
# Misc settings
37+
interpreter.auto_run = False
38+
interpreter.offline = True
39+
40+
# Custom Instructions
41+
interpreter.custom_instructions = f"""
42+
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.
43+
Recap the plan before answering the user's query.
44+
You will connect to a PostgreSQL database, with the connection string {connection_string}.
45+
Remember to only query the {db_name} database.
46+
Execute valid SQL commands to satisfy the user's query.
47+
Write all code in a full Python script. When you have to re-write code, redo the entire script.
48+
Execute the script to get the answer for the user's query.
49+
**YOU CAN EXECUTE SQL COMMANDS IN A PYTHON SCRIPT.***
50+
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.
51+
Only use real column names.
52+
***You ARE fully capable of executing SQL commands.***
53+
Be VERY clear about the answer to the user's query. They don't understand technical jargon so make it very clear and direct.
54+
Today's date is {date.today()}.
55+
You should respond in a very concise way.
56+
You can do it, I believe in you.
57+
"""

tests/core/test_async_core.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
from unittest import TestCase, mock
3+
4+
from interpreter.core.async_core import Server, AsyncInterpreter
5+
6+
7+
class TestServerConstruction(TestCase):
8+
"""
9+
Tests to make sure that the underlying server is configured correctly when constructing
10+
the Server object.
11+
"""
12+
13+
def test_host_and_port_defaults(self):
14+
"""
15+
Tests that a Server object takes on the default host and port when
16+
a) no host and port are passed in, and
17+
b) no HOST and PORT are set.
18+
"""
19+
with mock.patch.dict(os.environ, {}):
20+
s = Server(AsyncInterpreter())
21+
self.assertEqual(s.host, Server.DEFAULT_HOST)
22+
self.assertEqual(s.port, Server.DEFAULT_PORT)
23+
24+
def test_host_and_port_passed_in(self):
25+
"""
26+
Tests that a Server object takes on the passed-in host and port when they are passed-in,
27+
ignoring the surrounding HOST and PORT env vars.
28+
"""
29+
host = "the-really-real-host"
30+
port = 2222
31+
32+
with mock.patch.dict(os.environ, {"HOST": "this-is-supes-fake", "PORT": "9876"}):
33+
sboth = Server(AsyncInterpreter(), host, port)
34+
self.assertEqual(sboth.host, host)
35+
self.assertEqual(sboth.port, port)
36+
37+
def test_host_and_port_from_env_1(self):
38+
"""
39+
Tests that the Server object takes on the HOST and PORT env vars as host and port when
40+
nothing has been passed in.
41+
"""
42+
fake_host = "fake_host"
43+
fake_port = 1234
44+
45+
with mock.patch.dict(os.environ, {"HOST": fake_host, "PORT": str(fake_port)}):
46+
s = Server(AsyncInterpreter())
47+
self.assertEqual(s.host, fake_host)
48+
self.assertEqual(s.port, fake_port)

0 commit comments

Comments
 (0)