-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_chain.py
More file actions
49 lines (43 loc) · 1.92 KB
/
api_chain.py
File metadata and controls
49 lines (43 loc) · 1.92 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
# --- Standard Library ---
import json
import re
from typing import Any, Dict
# --- Core Third-Party Libraries ---
import requests
# --- Langchain and Langchain Community ---
from langchain.chains import RetrievalQA
from langchain_core.runnables import RunnableSerializable
from openinference.instrumentation import TracerProvider
from openinference.semconv.resource import ResourceAttributes
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from pydantic import Field
# --- Phoenix, OpenInference & OpenTelemetry (for tracing and evals) ---
from config import PHOENIX_ENDPOINT, PHOENIX_PROJECT_NAME
endpoint = PHOENIX_ENDPOINT
resource = Resource(attributes={ResourceAttributes.PROJECT_NAME: PHOENIX_PROJECT_NAME})
tracer_provider = TracerProvider(resource=resource)
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
tracer = tracer_provider.get_tracer(__name__)
answers=[]
class RequestsChain(RunnableSerializable):
endpoint: str = Field(..., description="API endpoint for chatbot requests")
@tracer.chain
def invoke(self, question) -> Dict[str, Any]:
try:
input = {"ques_text": question}
response = requests.post(self.endpoint, data=json.dumps(input))
response.raise_for_status()
data = response.json()
for item in data["0"]:
if item["type"] == "Text":
html = item["value"]
text = re.sub(r'<[^>]+>', '', html)
match = re.search(r'Answer:\s*(.*)', text, re.DOTALL)
if match:
answer_text = match.group(1).strip()
answers.append(answer_text)
return answers
except Exception as e:
return {"error": str(e), "input": input}