File size: 5,501 Bytes
fcc054f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e66058
 
 
 
 
 
fcc054f
4e66058
 
fcc054f
 
 
4e66058
 
 
 
 
 
 
 
 
fe6af28
e128654
bd38261
8dc1e51
4e66058
fcc054f
 
 
 
 
 
 
 
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
import gradio as gr
from gradio_client import Client
from langgraph.graph import StateGraph, START, END
from typing import TypedDict, Optional
import io
from PIL import Image
import os 


#OPEN QUESTION: SHOULD WE PASS ALL PARAMS FROM THE ORCHESTRATOR TO THE NODES INSTEAD OF SETTING IN EACH MODULE?
HF_TOKEN = os.environ.get("HF_TOKEN")



import configparser
import logging
import os
import ast
import re
from dotenv import load_dotenv

# Local .env file
load_dotenv()

def getconfig(configfile_path: str):
    """
    Read the config file
    Params
    ----------------
    configfile_path: file path of .cfg file
    """
    config = configparser.ConfigParser()
    try:
        config.read_file(open(configfile_path))
        return config
    except:
        logging.warning("config file not found")


def get_auth(provider: str) -> dict:
    """Get authentication configuration for different providers"""
    auth_configs = {
        "huggingface": {"api_key": os.getenv("HF_TOKEN")},
        "qdrant": {"api_key": os.getenv("QDRANT_API_KEY")},
    }
    
    provider = provider.lower()  # Normalize to lowercase
    
    if provider not in auth_configs:
        raise ValueError(f"Unsupported provider: {provider}")
    
    auth_config = auth_configs[provider]
    api_key = auth_config.get("api_key")
    
    if not api_key:
        logging.warning(f"No API key found for provider '{provider}'. Please set the appropriate environment variable.")
        auth_config["api_key"] = None
    
    return auth_config


# Define the state schema
class GraphState(TypedDict):
    query: str
    context: str
    result: str
    # Add orchestrator-level parameters (addressing your open question)
    reports_filter: str
    sources_filter: str
    subtype_filter: str
    year_filter: str

# node 2: retriever
def retrieve_node(state: GraphState) -> GraphState:
    client = Client("giz/chatfed_retriever", hf_token=HF_TOKEN)  # HF repo name
    context = client.predict(
        query=state["query"],
        reports_filter=state.get("reports_filter", ""),
        sources_filter=state.get("sources_filter", ""),
        subtype_filter=state.get("subtype_filter", ""),
        year_filter=state.get("year_filter", ""),
        api_name="/retrieve"
    )
    return {"context": context}

# node 3: generator
def generate_node(state: GraphState) -> GraphState:
    client = Client("giz/chatfed_generator", hf_token=HF_TOKEN)
    result = client.predict(
        query=state["query"],
        context=state["context"],
        api_name="/generate"
    )
    return {"result": result}

# build the graph
workflow = StateGraph(GraphState)

# Add nodes
workflow.add_node("retrieve", retrieve_node)
workflow.add_node("generate", generate_node)

# Add edges
workflow.add_edge(START, "retrieve")
workflow.add_edge("retrieve", "generate")
workflow.add_edge("generate", END)

# Compile the graph
graph = workflow.compile()

# Single tool for processing queries
def process_query(
    query: str,
    reports_filter: str = "",
    sources_filter: str = "",
    subtype_filter: str = "",
    year_filter: str = ""
) -> str:
    """
    Execute the ChatFed orchestration pipeline to process a user query.
    
    This function orchestrates a two-step workflow:
    1. Retrieve relevant context using the ChatFed retriever service with optional filters
    2. Generate a response using the ChatFed generator service with the retrieved context
    
    Args:
        query (str): The user's input query/question to be processed
        reports_filter (str, optional): Filter for specific report types. Defaults to "".
        sources_filter (str, optional): Filter for specific data sources. Defaults to "".
        subtype_filter (str, optional): Filter for document subtypes. Defaults to "".
        year_filter (str, optional): Filter for specific years. Defaults to "".
        
    Returns:
        str: The generated response from the ChatFed generator service
    """
    initial_state = {
        "query": query, 
        "context": "", 
        "result": "",
        "reports_filter": reports_filter or "",
        "sources_filter": sources_filter or "",
        "subtype_filter": subtype_filter or "",
        "year_filter": year_filter or ""
    }
    final_state = graph.invoke(initial_state)
    return final_state["result"]

# Simple testing interface


# Guidance for ChatUI - can be removed later. Questionable whether front end even necessary. Maybe nice to show the graph.
with gr.Blocks(title="ChatFed Orchestrator") as demo:
    
    with gr.Row():
        # Left column - Graph visualization
        with gr.Column():
            query_input = gr.Textbox(
                label="query", 
                lines=2, 
                placeholder="Enter your search query here",
                info="The query to search for in the vector database"
            )

            submit_btn = gr.Button("Submit", variant="primary")
            
        
        # Right column - Interface and documentation  
        with gr.Column():
            output = gr.Textbox(
                label="answer",
                lines=10,
                show_copy_button=True
            )

    # UI event handler
    submit_btn.click(
        fn=process_query,
        inputs=query_input,
        outputs=output,
        api_name="process_query"
    )

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        mcp_server=True,
        show_error=True
    )