Skip to content

Commit

Permalink
Merge pull request #146 from plpycoin/main
Browse files Browse the repository at this point in the history
chore: Use environment variable configuration files to set host, port
  • Loading branch information
shreyashankar authored Nov 6, 2024
2 parents 169b601 + 0ef33fa commit 4e61ede
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 12 deletions.
9 changes: 9 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
OPENAI_API_KEY=

BACKEND_ALLOW_ORIGINS=
BACKEND_HOST=localhost
BACKEND_PORT=8000
BACKEND_RELOAD=True

FRONTEND_HOST=0.0.0.0
FRONTEND_PORT=3000
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Load environment variables from .env file
include .env

.PHONY: tests tests-basic lint install mypy update ui-install ui-run

# Existing commands
Expand Down Expand Up @@ -32,13 +35,13 @@ run-ui-dev:
@echo "Starting server..."
@python server/app/main.py & \
echo "Starting UI development server..." && \
cd $(UI_DIR) && npm run dev
cd $(UI_DIR) && HOST=${FRONTEND_HOST} PORT=${FRONTEND_PORT} npm run dev

run-ui:
@echo "Starting server..."
@python server/app/main.py & \
echo "Building UI..." && \
cd $(UI_DIR) && npm run build && npm run start
cd $(UI_DIR) && npm run build && HOST=${FRONTEND_HOST} PORT=${FRONTEND_PORT} NEXT_PUBLIC_FRONTEND_ALLOWED_HOSTS=${FRONTEND_ALLOWED_HOSTS} npm run start

# Help command
help:
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,23 @@ pip install poetry
poetry install
```

4. Set up your OpenAI API key:
4. Set up your OpenAI API key and other environment variables:

Create a .env file in the project root and add your OpenAI API key:
Copy the .env.sample file under the root directory to .env and modify the environment variables inside as needed.

```bash
OPENAI_API_KEY=your_api_key_here

BACKEND_ALLOW_ORIGINS=
BACKEND_HOST=localhost
BACKEND_PORT=8000
BACKEND_RELOAD=True

FRONTEND_HOST=0.0.0.0
FRONTEND_PORT=3000
```

Alternatively, you can set the OPENAI_API_KEY environment variable in your shell.
Alternatively, you can set the OPENAI_API_KEY environment variable and others in your shell.

5. Run the basic test suite to ensure everything is working (this costs less than $0.01 with OpenAI):

Expand Down
12 changes: 10 additions & 2 deletions server/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@

load_dotenv()

# Read backend configuration from .env
host = os.getenv("BACKEND_HOST", "127.0.0.1")
port = int(os.getenv("BACKEND_PORT", 8000))
reload = os.getenv("BACKEND_RELOAD", "False").lower() == "true"

# Set default allow_origins if BACKEND_ALLOW_ORIGINS is not provided
allow_origins = os.getenv("BACKEND_ALLOW_ORIGINS", "http://localhost:3000").split(",")

app = FastAPI()
os.environ["USE_FRONTEND"] = "true"


# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000"], # Adjust this to your Next.js app's URL
allow_origins=allow_origins, # Adjust this to your Next.js app's URL
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
Expand All @@ -30,4 +38,4 @@ async def root():
if __name__ == "__main__":
import uvicorn

uvicorn.run("server.app.main:app", host="0.0.0.0", port=8000, reload=True)
uvicorn.run("server.app.main:app", host=host, port=port, reload=reload)
7 changes: 7 additions & 0 deletions website/.env.local.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
OPENAI_API_KEY=sk-xxx
OPENAI_API_BASE=https://api.openai.com/v1
MODEL_NAME=gpt-4o-mini

NEXT_PUBLIC_BACKEND_HOST=localhost
NEXT_PUBLIC_BACKEND_PORT=8000

14 changes: 14 additions & 0 deletions website/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-

## Getting Started

### Setting up environment variables

Copy the .env.sample file from the root directory to .env.local and modify the environment variables inside:

```bash
OPENAI_API_KEY=sk-xxx
OPENAI_API_BASE=https://api.openai.com/v1
MODEL_NAME=gpt-4o-mini

NEXT_PUBLIC_BACKEND_HOST=localhost
NEXT_PUBLIC_BACKEND_PORT=8008

```

First, run the development server:

```bash
Expand Down
9 changes: 7 additions & 2 deletions website/src/app/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { openai } from "@ai-sdk/openai";
import { createOpenAI } from "@ai-sdk/openai";
import { streamText } from "ai";

// Allow streaming responses up to 60 seconds
export const maxDuration = 60;
const openai = createOpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: process.env.OPENAI_API_BASE,
compatibility: 'strict', // strict mode, enable when using the OpenAI API
});

export async function POST(req: Request) {
const { messages } = await req.json();

const result = await streamText({
model: openai("gpt-4o-mini"),
model: openai(process.env.MODEL_NAME),
system: "You are a helpful assistant.",
messages,
});
Expand Down
3 changes: 2 additions & 1 deletion website/src/app/api/runPipeline/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ export async function POST(request: Request) {
await fs.writeFile(filePath, yamlString, "utf8");

// Submit the YAML config to the FastAPI endpoint
const response = await axios.post("http://localhost:8000/run_pipeline", {

const response = await axios.post(`http://${process.env.NEXT_PUBLIC_BACKEND_HOST}:${process.env.NEXT_PUBLIC_BACKEND_PORT}/run_pipeline`, {
yaml_config: filePath,
});

Expand Down
2 changes: 1 addition & 1 deletion website/src/app/playground/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ const WrappedCodeEditorPipelineApp: React.FC = () => {
useEffect(() => {
setIsLocalhost(
window.location.hostname === "localhost" ||
window.location.hostname === "127.0.0.1"
window.location.hostname === "127.0.0.1"
);
}, []);

Expand Down
2 changes: 1 addition & 1 deletion website/src/contexts/WebSocketContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({
return;
}

ws.current = new WebSocket("ws://localhost:8000/ws/run_pipeline");
ws.current = new WebSocket(`ws://${process.env.NEXT_PUBLIC_BACKEND_HOST}:${process.env.NEXT_PUBLIC_BACKEND_PORT}/ws/run_pipeline`);

ws.current.onopen = () => {
setReadyState(WebSocket.OPEN);
Expand Down

0 comments on commit 4e61ede

Please sign in to comment.