Spaces:
No application file
No application file
| import asyncio | |
| from agents import Runner, SQLiteSession, Agent, RunContextWrapper | |
| from _agents import content_agent, WebInspectorAgent, media_agent | |
| from pydantic import BaseModel, Field, ConfigDict | |
| from model import get_model | |
| from typing import Optional | |
| import nest_asyncio | |
| import logfire | |
| from langfuse import get_client | |
| import shutil | |
| from typing import Any | |
| import logging | |
| logging.basicConfig(level=logging.INFO) | |
| # set_tracing_disabled(disabled=True) | |
| # set_tracing_disabled(disabled=True) | |
| nest_asyncio.apply() | |
| logfire.configure( | |
| service_name='smpg_agent', | |
| send_to_logfire=False, | |
| ) | |
| logfire.instrument_openai_agents() | |
| langfuse = get_client() | |
| # Verify connection | |
| if langfuse.auth_check(): | |
| print("Langfuse client is authenticated and ready!") | |
| else: | |
| print("Authentication failed. Please check your credentials and host.") | |
| class AgentContext(BaseModel): | |
| user_input: Optional[str] = Field(default=None,description="The input provided by the user to the agent.") | |
| post_content: Optional[str] = Field(default=None,description="The generated content for the post.") | |
| raw_text: Optional[str] = Field(default=None,description="Raw text extracted from web pages.") | |
| model_config= ConfigDict(extra='forbid') | |
| def media_post_prompt(context: RunContextWrapper, agent: Agent): | |
| return """You are the Post Generation Agent for Social Media. | |
| Your task is to coordinate multiple agents to produce a complete social media post. | |
| You can execute the following tasks: | |
| - Content Generation | |
| - Media Generation | |
| Steps for Generating Content: | |
| 1. Extract website text using the Web Inspector Tool. | |
| 2. Provide the extracted text along with the user's brief to the Content Tool to generate engaging, platform-specific post text. | |
| Steps for Generating Media: | |
| 1. Extract key text and theme colors from the website. | |
| 2. Provide the generated post content (do not update use same as generated), the theme colors, user's brief, and the design brief to the Media Tool. | |
| Final Step: | |
| - Combine all outputs into a coherent, platform-ready social media post. | |
| - Return a full report to the user, including the generated content, images, and any design details. | |
| Rules: | |
| - Do not generate text or media yourself. | |
| - Always follow the steps in order. | |
| - Validate each agent's output before proceeding to the next step. | |
| - Ask the user for clarification only if the input is missing or ambiguous. | |
| """ | |
| async def main(): | |
| print("Hello from smpg!") | |
| session = SQLiteSession("conversation_123") | |
| agent_context = AgentContext( user_input="Write a LinkedIn post about the importance of AI in modern business strategies." ) | |
| social_media_post_gen_agent = Agent( | |
| name="social_media_post_gen_agent", | |
| instructions=media_post_prompt, | |
| tools=[ | |
| content_agent.as_tool( | |
| tool_name="content_agent", | |
| tool_description="""Generates engaging social media post content based on user input.""" | |
| ), | |
| WebInspectorAgent.as_tool( | |
| tool_name="WebInspectorAgent", | |
| tool_description="""Extracts details from a website using given URL, such as visible text content, theme colors etc.""" | |
| ), | |
| media_agent.as_tool( | |
| tool_name="media_agent", | |
| tool_description="""Generates high-quality, brand-aligned social media images based on the user's brief, | |
| design specifications, and extracted website content (text, colors, and theme). | |
| Follows design guidelines for style, composition, typography, colors, dark/light theme and platform-specific requirements. | |
| """ | |
| ) | |
| ], | |
| model=get_model('gemini-2.0-flash'), | |
| ) | |
| result = await Runner.run(social_media_post_gen_agent, | |
| input="""Create a LinkedIn post for website https://panaversity.org/, tell what they offer in service, can you give the stunning bento grids in middle with logos""" , | |
| session=session, | |
| context=agent_context, | |
| ) | |
| print('-----------------------------session--------------------------') | |
| # print(await session.get_items()) | |
| # print(result) | |
| if __name__ == "__main__": | |
| if not shutil.which("npx"): | |
| raise RuntimeError("npx is not installed. Please install it with `npm install -g npx`.") | |
| asyncio.run(main()) | |