forked from rungalileo/sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
189 lines (156 loc) · 7.2 KB
/
Copy pathapp.py
File metadata and controls
189 lines (156 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
import asyncio
import json
import os
from dotenv import load_dotenv
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
from agent import SimpleAgent
from agent_framework.llm.openai_provider import OpenAIProvider
from agent_framework.llm.models import LLMConfig
from agent_framework.utils.logging import get_galileo_logger
# Load environment variables
load_dotenv()
app = Flask(__name__)
CORS(app)
@app.route("/")
def index():
"""Serve the main page"""
return render_template("index.html")
@app.route("/api/generate", methods=["POST"])
def generate_startup():
"""API endpoint to generate startup pitch with individual Galileo trace"""
try:
data = request.json
industry = data.get("industry", "").strip()
audience = data.get("audience", "").strip()
random_word = data.get("randomWord", "").strip()
mode = data.get("mode", "silly").strip() # Default to silly mode
# Log API request as JSON
api_request = {
"endpoint": "/api/generate",
"method": "POST",
"inputs": {
"industry": industry,
"audience": audience,
"random_word": random_word,
"mode": mode,
},
}
print(f"API Request: {json.dumps(api_request, indent=2)}")
if not industry or not audience or not random_word:
return jsonify({"error": "All fields are required"}), 400
# Get the centralized Galileo logger instance
logger = get_galileo_logger()
# Start individual trace for this request if logger is available
trace = None
if logger:
trace = logger.start_trace(f"Generate startup pitch - {mode} mode - {industry} targeting {audience}")
try:
# Add LLM span for request processing if logger is available
if logger:
logger.add_llm_span(
input=f"API request received for {mode} mode startup generation",
output="Request validated and processing started",
model="flask_api",
num_input_tokens=len(str(api_request)),
num_output_tokens=0,
total_tokens=len(str(api_request)),
duration_ns=0,
)
# Run the agent asynchronously with proper event loop handling
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
agent_result = loop.run_until_complete(run_agent(industry, audience, random_word, mode))
finally:
loop.close()
# Parse the structured JSON result from agent
try:
parsed_result = json.loads(agent_result)
final_output = parsed_result.get("final_output", "No output generated")
except json.JSONDecodeError:
# Fallback if result is not JSON
final_output = str(agent_result)
# Log API response as JSON
api_response = {
"endpoint": "/api/generate",
"status": "success",
"result_length": len(final_output),
"mode": mode,
"agent_result_preview": (str(agent_result)[:200] + "..." if len(str(agent_result)) > 200 else str(agent_result)),
}
print(f"API Response: {json.dumps(api_response, indent=2)}")
# Add LLM span for successful completion if logger is available
if logger:
logger.add_llm_span(
input=f"Agent execution completed successfully",
output=final_output,
model="flask_api",
num_input_tokens=len(str(api_request)),
num_output_tokens=len(final_output),
total_tokens=len(str(api_request)) + len(final_output),
duration_ns=0,
)
# Conclude the trace successfully and flush immediately
logger.conclude(output=final_output, duration_ns=0)
logger.flush()
return jsonify({"result": final_output})
except Exception as e:
# Add LLM span for error if logger is available
if logger:
logger.add_llm_span(
input=f"Agent execution failed",
output=str(e),
model="flask_api",
num_input_tokens=len(str(api_request)),
num_output_tokens=len(str(e)),
total_tokens=len(str(api_request)) + len(str(e)),
duration_ns=0,
)
# Conclude the trace with error and flush immediately
logger.conclude(output=str(e), duration_ns=0)
logger.flush()
raise e
except Exception as e:
print(f"Error generating startup: {e}")
return jsonify({"error": str(e)}), 500
async def run_agent(industry: str, audience: str, random_word: str, mode: str = "silly") -> str:
"""Run the agent to generate startup pitch"""
# Set up LLM provider (this could be updated to use the Galileo-wrapped OpenAI client if desired)
llm_provider = OpenAIProvider(config=LLMConfig(model="gpt-4", temperature=0.7))
# Create agent instance with mode
agent = SimpleAgent(llm_provider=llm_provider, mode=mode)
# Create different task descriptions based on mode
if mode == "serious":
task = (
f"First, get recent business news context from NewsAPI to understand current market trends, then "
f"generate a professional startup business plan for a {industry} company targeting {audience} "
f"that incorporates the concept '{random_word}'. Use the news context to inform market analysis "
f"and competitive landscape. Make this extremely professional and corporate."
)
else: # silly mode
task = (
f"First, get some inspiration from recent HackerNews stories, then "
f"generate a startup pitch for a {industry} company targeting {audience} "
f"that includes the word '{random_word}'. Make the pitch creative and "
f"incorporate relevant trends from the HackerNews stories."
)
# Run the agent with individual parameters (Galileo logging handled by individual traces)
result = await agent.run(task, industry=industry, audience=audience, random_word=random_word)
return result
if __name__ == "__main__":
# Verify Galileo configuration
project_id = os.environ.get("GALILEO_PROJECT")
log_stream = os.environ.get("GALILEO_LOG_STREAM")
api_key = os.environ.get("GALILEO_API_KEY")
print(f"🔍 Galileo Configuration:")
print(f" Project: {project_id}")
print(f" Log Stream: {log_stream}")
print(f" API Key: {'✅ Set' if api_key else '❌ Not Set'}")
if not os.environ.get("OPENAI_API_KEY"):
print("Error: OPENAI_API_KEY not set. Please set this environment variable.")
exit(1)
if not api_key:
print("Warning: GALILEO_API_KEY not set. Galileo logging will be disabled.")
app.run(debug=True, host="0.0.0.0", port=2021)