Skip to content

Commit cc43bde

Browse files
committed
Merge branch 'main' into giphy-apps-pkg
2 parents 21cbe02 + 7f26b6c commit cc43bde

File tree

5 files changed

+72
-69
lines changed

5 files changed

+72
-69
lines changed

quickstart/ai_giphy/.env.example

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ SMTP_PASSWORD=your_smtp_password_or_api_key
99
SMTP_SENDER=your_sender_email (e.g., [email protected])
1010

1111
# Azure OpenAI Configuration (used by the AI agent)
12-
APP_AZURE_OPENAI_API_KEY=your_azure_openai_api_key
13-
APP_AZURE_OPENAI_API_VERSION=your_azure_openai_api_version
14-
APP_AZURE_OPENAI_ENDPOINT=your_azure_openai_endpoint
15-
APP_AZURE_OPENAI_DEPLOYMENT_NAME=your_azure_openai_deployment_name
12+
OPENAI_API_KEY=your_openai_api_key
13+
OPENAI_MODEL_NAME=your_model_name
14+
OPENAI_BASE_URL=your_custom_base_url

quickstart/ai_giphy/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ https://github.com/user-attachments/assets/fd23ce3b-d63d-480d-a4fe-4258fc5de5c7
1212
- [Temporal CLI](https://docs.temporal.io/cli)
1313
- Giphy API key
1414
- SMTP credentials (e.g., SendGrid)
15-
- Azure OpenAI API credentials
15+
- OpenAI API key
1616

1717
### Installation Guides
1818
- [macOS Setup Guide](https://github.com/atlanhq/application-sdk/blob/main/docs/docs/setup/MAC.md)
@@ -70,11 +70,10 @@ SMTP_USERNAME=your_smtp_username (e.g., apikey for SendGrid)
7070
SMTP_PASSWORD=your_smtp_password_or_api_key
7171
SMTP_SENDER=your_sender_email (e.g., [email protected])
7272
73-
# Azure OpenAI Configuration (used by the AI agent)
74-
APP_AZURE_OPENAI_API_KEY=your_azure_openai_api_key
75-
APP_AZURE_OPENAI_API_VERSION=your_azure_openai_api_version
76-
APP_AZURE_OPENAI_ENDPOINT=your_azure_openai_endpoint
77-
APP_AZURE_OPENAI_DEPLOYMENT_NAME=your_azure_openai_deployment_name
73+
# OpenAI Configuration (used by the AI agent)
74+
OPENAI_API_KEY=your_openai_api_key
75+
OPENAI_MODEL_NAME=your_model_name (optional, defaults to gpt-4.1-mini)
76+
OPENAI_BASE_URL=your_custom_base_url (optional, for custom gateways)
7877
7978
```
8079

quickstart/ai_giphy/app/ai_agent.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from langchain import hub
1010
from langchain.agents import AgentExecutor, create_tool_calling_agent
1111
from langchain_core.tools import StructuredTool
12-
from langchain_openai import AzureChatOpenAI
12+
from langchain_openai import ChatOpenAI
1313

1414
load_dotenv()
1515
logger = get_logger(__name__)
@@ -18,9 +18,14 @@
1818
SMTP_PORT = int(os.getenv("SMTP_PORT", "587"))
1919
SMTP_USERNAME = os.getenv("SMTP_USERNAME", "apikey")
2020
SMTP_PASSWORD = os.getenv("SMTP_PASSWORD")
21+
SMTP_SENDER = os.getenv("SMTP_SENDER", "[email protected]")
2122
GIPHY_API_KEY = os.getenv("GIPHY_API_KEY")
2223
SMTP_SENDER = os.getenv("SMTP_SENDER", "[email protected]")
2324

25+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
26+
OPENAI_MODEL_NAME = os.getenv("OPENAI_MODEL_NAME", "gpt-4.1-mini")
27+
OPENAI_BASE_URL = os.getenv("OPENAI_BASE_URL")
28+
2429

2530
def fetch_gif(search_term: str) -> str:
2631
"""
@@ -34,7 +39,8 @@ def fetch_gif(search_term: str) -> str:
3439
"""
3540
if not GIPHY_API_KEY:
3641
raise ValueError(
37-
"GIPHY_API_KEY is not set, please set it in the environment variables for the application. For reference, please refer to the README.md file and .env.example file"
42+
"GIPHY_API_KEY is not set, please set it in the environment variables for the application. "
43+
"For reference, please refer to the README.md file and .env.example file."
3844
)
3945

4046
url = f"https://api.giphy.com/v1/gifs/random?api_key={GIPHY_API_KEY}&tag={search_term}&rating=pg"
@@ -46,16 +52,18 @@ def fetch_gif(search_term: str) -> str:
4652
return gif_url
4753
except Exception as e:
4854
logger.error(f"Failed to fetch GIF: {e}")
55+
# Fallback GIF
4956
return "https://media.giphy.com/media/3o7abAHdYvZdBNnGZq/giphy.gif"
5057

5158

5259
def send_email_with_gify(to: str, gify_url: str):
5360
"""
54-
Send an email to the given recipient with the specified subject and body
61+
Send an email to the given recipient with the specified subject and body.
5562
"""
5663
if not SMTP_PASSWORD:
5764
raise ValueError(
58-
"SMTP_PASSWORD is not set, please set it in the environment variables for the application. For reference, please refer to the README.md file and .env.example file"
65+
"SMTP_PASSWORD is not set, please set it in the environment variables for the application. "
66+
"For reference, please refer to the README.md file and .env.example file."
5967
)
6068

6169
sender = SMTP_SENDER
@@ -68,53 +76,45 @@ def send_email_with_gify(to: str, gify_url: str):
6876
<p>Enjoy!</p>
6977
</body>
7078
</html>
71-
"""
79+
"""
80+
7281
msg = MIMEText(body, "html")
7382
msg["Subject"] = subject
7483
msg["From"] = sender
7584
msg["To"] = to
7685

7786
try:
78-
host = SMTP_HOST
79-
port = SMTP_PORT
80-
username = SMTP_USERNAME
81-
password = SMTP_PASSWORD
82-
with smtplib.SMTP(host, port) as server:
87+
with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server:
8388
server.starttls()
84-
server.login(username, password) # pyright: ignore[reportArgumentType]
89+
server.login(SMTP_USERNAME, SMTP_PASSWORD) # pyright: ignore[reportArgumentType]
8590
server.send_message(msg)
86-
return "Email sent successfully"
91+
return "Email sent successfully"
8792
except Exception as e:
8893
logger.error(f"Failed to send email: {e}")
8994
raise
9095

9196

9297
def get_chain():
93-
# Check for required Azure OpenAI environment variables
94-
required_azure_vars = [
95-
"APP_AZURE_OPENAI_API_KEY",
96-
"APP_AZURE_OPENAI_API_VERSION",
97-
"APP_AZURE_OPENAI_ENDPOINT",
98-
"APP_AZURE_OPENAI_DEPLOYMENT_NAME",
99-
]
98+
# Only API key is required. Base URL is optional.
99+
if not os.getenv("OPENAI_API_KEY"):
100+
raise ValueError(
101+
"OPENAI_API_KEY is not set, please set it in the environment variables for the application."
102+
)
100103

101-
for var in required_azure_vars:
102-
if not os.getenv(var):
103-
raise ValueError(
104-
f"{var} is not set, please set it in the environment variables for the application. For reference, please refer to the README.md file and .env.example file"
105-
)
106-
107-
local_llm = AzureChatOpenAI(
108-
api_key=os.getenv("APP_AZURE_OPENAI_API_KEY"),
109-
api_version=os.getenv("APP_AZURE_OPENAI_API_VERSION"),
110-
azure_endpoint=os.getenv("APP_AZURE_OPENAI_ENDPOINT"),
111-
azure_deployment=os.getenv("APP_AZURE_OPENAI_DEPLOYMENT_NAME"),
104+
# Optional: Only used if someone is using a proxy / Azure / custom gateway
105+
openai_base_url = os.getenv("OPENAI_BASE_URL", None)
106+
107+
local_llm = ChatOpenAI(
108+
api_key=OPENAI_API_KEY,
109+
model=OPENAI_MODEL_NAME,
110+
base_url=openai_base_url, # Passing None is allowed and simply ignored
112111
)
113112

114113
local_tools = [
115114
StructuredTool.from_function(fetch_gif),
116115
StructuredTool.from_function(send_email_with_gify),
117116
]
117+
118118
try:
119119
prompt = hub.pull("hwchase17/openai-tools-agent")
120120
agent = create_tool_calling_agent(local_llm, local_tools, prompt)

quickstart/ai_giphy/tests/test_ai_agent.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@ def mock_env_vars(self):
1414
patch("app.ai_agent.SMTP_PORT", "587"),
1515
patch("app.ai_agent.SMTP_USERNAME", "test_user"),
1616
patch("app.ai_agent.SMTP_PASSWORD", "test_password"),
17-
patch("os.environ.get") as mock_env_get,
17+
patch("app.ai_agent.OPENAI_API_KEY", "test_openai_key"),
18+
patch("app.ai_agent.OPENAI_MODEL_NAME", "gpt-4.1-mini"),
19+
patch("app.ai_agent.OPENAI_BASE_URL", None),
20+
patch("app.ai_agent.os.getenv") as mock_getenv,
1821
):
19-
# Mock Azure OpenAI environment variables
20-
env_vars = {
21-
"APP_AZURE_OPENAI_API_KEY": "test_azure_key",
22-
"APP_AZURE_OPENAI_API_VERSION": "2023-05-15",
23-
"APP_AZURE_OPENAI_ENDPOINT": "https://test.openai.azure.com/",
24-
"APP_AZURE_OPENAI_DEPLOYMENT_NAME": "test-deployment",
25-
}
26-
mock_env_get.side_effect = lambda key, default=None: env_vars.get(
27-
key, default
28-
)
22+
# Mock os.getenv for get_chain() function
23+
def getenv_side_effect(key, default=None):
24+
env_vars = {
25+
"OPENAI_API_KEY": "test_openai_key",
26+
"OPENAI_MODEL_NAME": "gpt-4.1-mini",
27+
"OPENAI_BASE_URL": None,
28+
}
29+
return env_vars.get(key, default)
30+
31+
mock_getenv.side_effect = getenv_side_effect
2932

3033
yield
3134

@@ -85,7 +88,7 @@ def test_send_email_with_gify_failure() -> None:
8588
def test_get_chain_success() -> None:
8689
"""Test successful creation of AI agent chain."""
8790
with (
88-
patch("app.ai_agent.AzureChatOpenAI") as mock_llm,
91+
patch("app.ai_agent.ChatOpenAI") as mock_llm,
8992
patch("app.ai_agent.hub.pull") as mock_hub_pull,
9093
patch("app.ai_agent.create_tool_calling_agent") as mock_create_agent,
9194
patch("app.ai_agent.AgentExecutor") as mock_agent_executor,
@@ -106,10 +109,9 @@ def test_get_chain_success() -> None:
106109

107110
assert result == mock_executor
108111
mock_llm.assert_called_once_with(
109-
api_key="test_azure_key",
110-
api_version="2023-05-15",
111-
azure_endpoint="https://test.openai.azure.com/",
112-
azure_deployment="test-deployment",
112+
api_key="test_openai_key",
113+
model="gpt-4.1-mini",
114+
base_url=None,
113115
)
114116
mock_hub_pull.assert_called_once_with("hwchase17/openai-tools-agent")
115117
mock_create_agent.assert_called_once()
@@ -119,7 +121,7 @@ def test_get_chain_success() -> None:
119121
def test_get_chain_failure() -> None:
120122
"""Test get_chain raises exception when agent creation fails."""
121123
with (
122-
patch("app.ai_agent.AzureChatOpenAI"),
124+
patch("app.ai_agent.ChatOpenAI"),
123125
patch("app.ai_agent.hub.pull", side_effect=Exception("Hub Error")),
124126
):
125127
with pytest.raises(Exception, match="Hub Error"):

quickstart/ai_giphy/tests/test_ai_giphy.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@ def mock_env_vars(self):
1818
patch("app.ai_agent.SMTP_PORT", "587"),
1919
patch("app.ai_agent.SMTP_USERNAME", "test_user"),
2020
patch("app.ai_agent.SMTP_PASSWORD", "test_password"),
21-
patch("os.environ.get") as mock_env_get,
21+
patch("app.ai_agent.OPENAI_API_KEY", "test_openai_key"),
22+
patch("app.ai_agent.OPENAI_MODEL_NAME", "gpt-4.1-mini"),
23+
patch("app.ai_agent.OPENAI_BASE_URL", None),
24+
patch("app.ai_agent.os.getenv") as mock_getenv,
2225
):
23-
# Mock Azure OpenAI environment variables
24-
env_vars = {
25-
"APP_AZURE_OPENAI_API_KEY": "test_azure_key",
26-
"APP_AZURE_OPENAI_API_VERSION": "2023-05-15",
27-
"APP_AZURE_OPENAI_ENDPOINT": "https://test.openai.azure.com/",
28-
"APP_AZURE_OPENAI_DEPLOYMENT_NAME": "test-deployment",
29-
}
30-
mock_env_get.side_effect = lambda key, default=None: env_vars.get(
31-
key, default
32-
)
26+
# Mock os.getenv for get_chain() function
27+
def getenv_side_effect(key, default=None):
28+
env_vars = {
29+
"OPENAI_API_KEY": "test_openai_key",
30+
"OPENAI_MODEL_NAME": "gpt-4.1-mini",
31+
"OPENAI_BASE_URL": None,
32+
}
33+
return env_vars.get(key, default)
34+
35+
mock_getenv.side_effect = getenv_side_effect
3336

3437
yield
3538

@@ -94,7 +97,7 @@ async def test_ai_agent_tools_integration() -> None:
9497
with (
9598
patch("requests.get") as mock_requests,
9699
patch("smtplib.SMTP") as mock_smtp,
97-
patch("app.ai_agent.AzureChatOpenAI"),
100+
patch("app.ai_agent.ChatOpenAI"),
98101
patch("app.ai_agent.hub.pull"),
99102
patch("app.ai_agent.create_tool_calling_agent"),
100103
patch("app.ai_agent.AgentExecutor"),

0 commit comments

Comments
 (0)