Skip to content

Commit 37bd764

Browse files
committed
Add example for connecting to ADL Server using langchain and OpenAI
1 parent 2850439 commit 37bd764

3 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# ADL Server LangChain Python Example
2+
3+
This example demonstrates how to use `langchain` and `langchain-openai` in Python to connect to the `adl-server`.
4+
It sends a simple "Hello" message to the ADL Server's OpenAI-compatible chat completions endpoint.
5+
6+
## Prerequisites
7+
8+
- [Docker](https://www.docker.com/) and [Docker Compose](https://docs.docker.com/compose/) installed.
9+
- Python 3.8 or higher.
10+
- `pip` package manager.
11+
12+
## Setup ADL Server
13+
14+
First, start the ADL Server using Docker Compose in the parent directory:
15+
16+
```bash
17+
docker-compose up -d
18+
```
19+
20+
This will start the ADL Server on port `8080`.
21+
22+
## Setup Python Environment
23+
24+
1. Ideally, create a virtual environment:
25+
26+
```bash
27+
python3 -m venv .venv
28+
source .venv/bin/activate # On Windows, use .venv\Scripts\activate
29+
```
30+
31+
2. Install dependencies:
32+
33+
```bash
34+
pip install -r requirements.txt
35+
```
36+
37+
## Running the Example
38+
39+
Execute the main script:
40+
41+
```bash
42+
python main.py
43+
```
44+
45+
The script will:
46+
1. Load environment variables.
47+
2. Initialize a `ChatOpenAI` client pointing to `http://localhost:8080/v1` (the ADL Server address).
48+
3. Send a "Hello" message.
49+
4. Print the response from the server.
50+
51+
You should see output similar to:
52+
53+
```
54+
Connecting to ADL Server at http://localhost:8080/v1...
55+
Sending message: 'Hello'
56+
--------------------
57+
Response from ADL Server:
58+
Hello there! How can I help you today?
59+
--------------------
60+
```
61+

‎examples/langchain-python/main.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import os
2+
from langchain_openai import ChatOpenAI
3+
from langchain_core.messages import HumanMessage
4+
from dotenv import load_dotenv
5+
6+
# Load environment variables if .env file exists
7+
load_dotenv()
8+
9+
# Configuration for ADL Server
10+
ADL_SERVER_URL = os.getenv("ADL_SERVER_URL", "http://localhost:8080/v1")
11+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "dummy-key") # API key is required by the client but will be ignored by the server
12+
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o")
13+
14+
def main():
15+
print(f"Connecting to ADL Server at {ADL_SERVER_URL}...")
16+
17+
# Initialize the ChatOpenAI client pointing to the ADL Server, for example: http://localhost:8080/v1
18+
chat = ChatOpenAI(base_url=ADL_SERVER_URL, api_key=OPENAI_API_KEY, model=MODEL_NAME,temperature=0.7)
19+
20+
# Create a simple message
21+
messages = [HumanMessage(content="Hello ADL")]
22+
23+
try:
24+
# Send the message to the server
25+
print("Sending message: 'Hello'")
26+
response = chat.invoke(messages)
27+
28+
# Print the response
29+
print("-" * 20)
30+
print("Response from ADL Server:")
31+
print(response.content)
32+
print("-" * 20)
33+
34+
except Exception as e:
35+
print(f"Error communicating with ADL Server: {e}")
36+
37+
if __name__ == "__main__":
38+
main()
39+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
langchain
2+
langchain-openai
3+
python-dotenv
4+

0 commit comments

Comments
 (0)