File size: 9,519 Bytes
2f14900
bba2b6a
2f14900
 
bba2b6a
2f14900
 
 
3f0124c
bba2b6a
2f14900
 
 
3f0124c
2dbb8c1
7a09004
bbed089
2f14900
 
 
bba2b6a
 
 
 
 
 
 
 
10ed1ae
bba2b6a
 
2f14900
 
bba2b6a
 
 
 
 
2f14900
 
2dbb8c1
5d8b12d
2dbb8c1
 
23ad2fb
2dbb8c1
bbed089
 
2dbb8c1
5d8b12d
bbed089
2dbb8c1
5d8b12d
bbed089
2dbb8c1
bba2b6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbed089
 
10ed1ae
 
bbed089
10ed1ae
bbed089
 
 
 
 
 
 
 
 
10ed1ae
bbed089
 
 
 
 
 
 
2dbb8c1
bbed089
bba2b6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57ea0b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bba2b6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# agent.py
# Implements a linear, deterministic AI pipeline to eliminate API looping and quota issues.

import os
import json
from dotenv import load_dotenv

from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import PromptTemplate
from langchain_core.messages import SystemMessage, HumanMessage

from tools.job_search import search_jobs_tool
from tools.company_info import company_info_tool
from tools.market_trends import market_trends_tool

from composio import Composio
import asyncio

load_dotenv()

class LinearCareerAgent:
    def __init__(self):
        self.api_key = os.getenv("GOOGLE_API_KEY", "")
        if not self.api_key:
            raise EnvironmentError("GOOGLE_API_KEY is missing. Please add it to your environment variables.")

        # Main summarizer LLM
        self.llm = ChatGoogleGenerativeAI(
            model="gemini-1.5-flash",
            google_api_key=self.api_key,
            temperature=0.3,
        )

        # Fast router LLM
        self.router_llm = ChatGoogleGenerativeAI(
            model="gemini-2.5-flash",
            google_api_key=self.api_key,
            temperature=0.0,
        )

        # Init Composio
        self.composio_error = None
        composio_api_key = os.getenv("COMPOSIO_API_KEY")
        if composio_api_key:
            composio_api_key = composio_api_key.strip()
            try:
                # Direct SDK initialization 
                self.composio_sdk = Composio(api_key=composio_api_key)
            except Exception as e:
                self.composio_error = f"Composio initialization error: {str(e)}"
                self.composio_sdk = None
        else:
            self.composio_error = "COMPOSIO_API_KEY is not configured in Hugging Face Space Secrets."
            self.composio_sdk = None

    async def ask(self, user_query: str, chat_history: list = None) -> str:
        """
        The main linear pipeline: Route -> Fetch (Optional) -> Summarize
        """
        try:
            # Step 1: Route the query
            intent, extraction_arg = await self._route_query(user_query)

            # Step 2: Fetch raw data exactly once based on intent
            raw_data = ""
            if intent == "job_search":
                raw_data = await search_jobs_tool.ainvoke(extraction_arg)
            elif intent == "company_info":
                raw_data = await company_info_tool.ainvoke(extraction_arg)
            elif intent == "linkedin_profile":
                if self.composio_sdk:
                    try:
                        # Try to execute directly. 
                        # execute is a method of the tools object
                        response = await asyncio.to_thread(
                            self.composio_sdk.tools.execute,
                            slug="linkedin_get_profile",
                            arguments={"url": extraction_arg},
                            user_id="default_user"
                        )
                        raw_data = json.dumps(response)
                    except Exception as e:
                        # Fallback attempt if slug is slightly different
                        try:
                            response = await asyncio.to_thread(
                                self.composio_sdk.tools.execute,
                                slug="linkedin_profile",
                                arguments={"url": extraction_arg},
                                user_id="default_user"
                            )
                            raw_data = json.dumps(response)
                        except:
                            raw_data = json.dumps({"error": f"LinkedIn Direct Execution failed: {str(e)}"})
                else:
                    raw_data = json.dumps({"error": self.composio_error or "Composio SDK not initialized"})
            elif intent == "market_trends":
                raw_data = await market_trends_tool.ainvoke(extraction_arg)

            # Step 3: Summarize and respond
            return await self._generate_final_response(user_query, intent, raw_data, chat_history)

        except Exception as e:
            return f"⚠️ Something went wrong in the async pipeline: {e}"

    async def _route_query(self, query: str):
        """
        Deterministically decides which tool to use.
        Returns: Tuple(intent_name, argument_for_tool)
        """
        routing_prompt = f"""
        You are a smart router for a career application.
        Analyze the user's query and decide which specific tool to use.
        
        Available tools:
        - job_search: For finding actual job listings. You MUST extract a JSON representing the filter criteria, e.g '{{"title": "python", "location": "Bangalore"}}'. If the user provides a raw JSON string of filters, just output that exact JSON string.
        - company_info: For researching a specific company. Arg: The company name.
        - linkedin_profile: For summarizing a specific person's linkedin URL. Arg: The URL.
        - market_trends: For general knowledge about hiring. Arg: The local market/city.
        - none: For general chit-chat. Arg: none.

        Query: "{query}"
        
        Output strictly in this format: INTENT | ARGUMENT
        Example 1: job_search | {{"title": "developer", "location": "mumbai"}}
        Example 2: company_info | TCS
        Example 3: linkedin_profile | https://www.linkedin.com/in/billgates
        Example 4: none | none
        """
        
        route_decision = await self.router_llm.ainvoke(routing_prompt)
        route_text = route_decision.content.strip()
        
        try:
            intent, arg = route_text.split(" | ")
            return intent.strip(), arg.strip()
        except Exception:
            # Fallback
            return "none", "none"

    async def _generate_final_response(self, query: str, intent: str, raw_data: str, chat_history: list) -> str:
        """
        Takes the raw data and the user query, and generates a warm, final response.
        """
        system_instructions = """
        You are CareerBot, your friendly and proactive career companion. You specialize in the Indian job market.
        
        Guidelines:
        1. Warm & Natural: Speak like a trusted mentor.
        2. Indian Market Expert: Always mention salary in LPA (Lakhs Per Annum). 1 LPA = 1,00,000 INR per year.
        3. Structured formatting: If you receive RAW JSON data, you MUST include that RAW JSON data exactly as provided somewhere in your response (usually at the very bottom) so the UI can parse it into cards. Do not omit the JSON if it is provided.
        """

        if intent == "linkedin_profile":
            prompt = f"""
            User Query: {query}
            Intent Detected: {intent}
            Raw LinkedIn Data: 
            {raw_data if raw_data else 'No data retrieved.'}
            
            You are acting as an expert Career Coach for the Indian Job Market.
            Based on the LinkedIn Profile data above, provide a comprehensive analysis with these EXACT sections:
            
            1. **Executive Summary**: A professional 2-3 sentence overview of the person's identity and career stage.
            2. **Plus Points (Core Strengths)**: 3-4 bullet points highlighting their unique skills, experience, and certifications.
            3. **Minus Points (Gaps/Areas for Improvement)**: 2-3 bullet points on what's missing or how they can improve their profile for the Indian market.
            4. **Best Job Roles**: 3 specific roles they should target (e.g. 'Senior Frontend Engineer').
            5. **Where to Apply**: Types of companies or specific Indian companies (e.g. Zomato, Swiggy, TCS, Startups) that would value this profile.
            
            Ensure you mention salary expectations in **LPA (Lakhs Per Annum)**.
            """
        else:
            prompt = f"""
            User Query: {query}
            Intent Detected: {intent}
            Raw Data from API: 
            {raw_data if raw_data else 'No extra API data pulled for this query.'}
            
            Please provide a helpful, warm response to the user based on the raw data above.
            If the intent is a job search or company info, ensure you append the RAW API JSON Data to your response so the frontend can render it visually.
            """

        messages = [SystemMessage(content=system_instructions)]
        
        if chat_history:
            # We assume chat_history is a list of dicts: [{'role': 'user', 'content': 'hi'}]
            # We'll truncate to last 4 for context window safety
            for msg in chat_history[-4:]:
                if msg.get('role') == 'user':
                    messages.append(HumanMessage(content=msg.get('content', '')))
                else:
                    # assistant
                    messages.append(SystemMessage(content=msg.get('content', '')))
                    
        messages.append(HumanMessage(content=prompt))

        response = await self.llm.ainvoke(messages)
        return response.content

def build_agent(memory=None):
    """
    Adapter function to maintain compatibility with app.py.
    Returns the linear agent instance.
    """
    return LinearCareerAgent(), memory

async def ask_agent_async(agent_instance, question: str, chat_history: list = None) -> str:
    """
    Adapter function to trigger the async pipeline.
    """
    return await agent_instance.ask(question, chat_history)