Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import json | |
# Function to interact with Vectara API | |
def query_vectara(question, chat_history): | |
api_endpoint = "https://api.vectara.io/v1/query" | |
customer_id = "<YOUR-CUSTOMER-ID>" | |
corpus_id = "<YOUR-CORPUS-ID>" | |
api_key = "<YOUR-API-KEY>" | |
# Get the user's message from the chat history | |
user_message = chat_history[-1][0] | |
query_body = { | |
"query": [ | |
{ | |
"query": user_message, # Use the user's message as the query | |
"start": 0, | |
"numResults": 10, | |
"corpusKey": [ | |
{ | |
"customerId": customer_id, | |
"corpusId": corpus_id, | |
"lexicalInterpolationConfig": {"lambda": 0.025} | |
} | |
], | |
"contextConfig": { | |
"sentencesBefore": 3, | |
"sentencesAfter": 3, | |
"startTag": "%START_TAG%", | |
"endTag": "%END_TAG%" | |
}, | |
"summary": [ | |
{ | |
"responseLang": "eng", | |
"maxSummarizedResults": 7, | |
"summarizerPromptName": "vectara-summarizer-ext-v1.3.0" | |
} | |
] | |
} | |
] | |
} | |
post_headers = { | |
"Content-type": "application/json", | |
"Accept": "application/json", | |
"customer-id": customer_id, | |
"x-api-key": api_key | |
} | |
response = requests.post(api_endpoint, json=query_body, headers=post_headers) | |
if response.status_code == 200: | |
return response.json() | |
else: | |
return {"error": "Failed to query Vectara API"} | |
# Create a Gradio ChatInterface | |
iface = gr.ChatInterface( | |
fn=query_vectara, | |
examples=["Hello", "What is the weather today?", "Tell me a joke"], | |
title="Vectara Chatbot", | |
description="Ask me anything using the Vectara API!", | |
) | |
# Run the Gradio interface | |
iface.launch() | |