Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate LangChain with FastAPI #49

Merged
merged 16 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added server/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions server/intelligence-service/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# For Non-Azure OpenAI
OPENAI_API_KEY=your-openai-secret-key

# For Azure OpenAI
AZURE_OPENAI_API_KEY=your-azure-api-key
AZURE_OPENAI_ENDPOINT=your-azure-base-url
AZURE_OPENAI_API_VERSION=your-api-version
FelixTJDietrich marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions server/intelligence-service/.gitignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be omittable since we are already ignoring .env-files with the .gitignore of the base folder.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
12 changes: 9 additions & 3 deletions server/intelligence-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ To set up the project locally, follow these steps:

1. **Install dependencies:**
The project uses `poetry` for dependency management. Install the dependencies by running:

```bash
poetry install
```

2. **Run the application:**
You can start the FastAPI application with Uvicorn:

```bash
poetry run uvicorn src.main:app --reload
```
Expand All @@ -32,6 +34,7 @@ After running the application, you can access the FastAPI API documentation at `
## Project Structure
FelixTJDietrich marked this conversation as resolved.
Show resolved Hide resolved

The project is organized as follows:

```
intelligence-service/
├── pyproject.toml
Expand All @@ -41,13 +44,16 @@ intelligence-service/
├── tests/
│ ├── __init__.py
│ └── test_hello.py
├── src/
│ ├── config.py
├── src/
│ ├── __init__.py
│ ├── config.py
│ ├── langchain_client.py
│ ├── main.py
│ └── auth/
│ └── router.py
└── ...
```

## Testing

The project includes a set of unit tests to ensure that the core functionalities work as expected. These tests are located in the `tests/` directory.
Expand All @@ -57,4 +63,4 @@ The project includes a set of unit tests to ensure that the core functionalities
To run all tests, use the following command:

```bash
poetry run pytest
poetry run pytest
Empty file.
1,657 changes: 1,525 additions & 132 deletions server/intelligence-service/poetry.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions server/intelligence-service/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ package-mode = false
python = "^3.12"
fastapi = "^0.112.0"
uvicorn = "^0.30.5"
python-dotenv = "^1.0.1"
langchain-community = "^0.2.11"
openai = "^1.40.3"
langchain = "^0.2.12"
langchain-openai = "^0.1.21"
FelixTJDietrich marked this conversation as resolved.
Show resolved Hide resolved


[tool.poetry.group.dev.dependencies]
Expand Down
Empty file.
12 changes: 12 additions & 0 deletions server/intelligence-service/src/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
from dotenv import load_dotenv
import os

load_dotenv()

class Settings:
APP_NAME: str = "Intelligence Service"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you explicit typing it as str?

# Non-Azure OpenAI Configuration
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
FelixTJDietrich marked this conversation as resolved.
Show resolved Hide resolved

# Azure OpenAI Configuration
AZURE_OPENAI_API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
AZURE_OPENAI_API_VERSION = os.getenv("AZURE_OPENAI_API_VERSION")
FelixTJDietrich marked this conversation as resolved.
Show resolved Hide resolved


settings = Settings()
19 changes: 17 additions & 2 deletions server/intelligence-service/src/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
from fastapi import FastAPI

from fastapi import FastAPI, HTTPException
from .auth.router import router
from .config import settings
from .services.langchain_client import get_openai_client
from pydantic import BaseModel

app = FastAPI(title=settings.APP_NAME)

app.include_router(router)


class ChatRequest(BaseModel):
message: str


@app.post("/chat", response_model=dict, summary="Chat with LLM")
async def chat(request: ChatRequest):
try:
client = get_openai_client()
response = client.invoke(request.message)
return {"response": response}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
Empty file.
15 changes: 15 additions & 0 deletions server/intelligence-service/src/services/langchain_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from langchain_openai import OpenAI
from langchain_community.chat_models import AzureChatOpenAI
from ..config import settings

def get_openai_client():
FelixTJDietrich marked this conversation as resolved.
Show resolved Hide resolved
if settings.OPENAI_API_KEY:
return OpenAI(api_key=settings.OPENAI_API_KEY)
elif settings.AZURE_OPENAI_API_KEY:
return AzureChatOpenAI(
api_key=settings.AZURE_OPENAI_API_KEY,
endpoint=settings.AZURE_OPENAI_ENDPOINT,
api_version=settings.AZURE_OPENAI_API_VERSION
)
else:
raise ValueError("No valid OpenAI configuration found.")
9 changes: 9 additions & 0 deletions server/intelligence-service/tests/test_langchain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from fastapi.testclient import TestClient
from src.main import app

client = TestClient(app)

def test_chat_endpoint():
response = client.post("/chat", json={"message": "Hello, how are you?"})
assert response.status_code == 200
assert "response" in response.json()