Spaces:
Running
Running
File size: 2,048 Bytes
cf0997e 55755d8 cf0997e 55755d8 cf0997e |
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 |
import openai
import os
import gradio as gr
from llama_index.core.base.llms.types import ChatResponse
def on_submit_openai_key(openai_key):
os.environ["OPENAI_API_KEY"] = openai_key
# Test openai key
try:
client = openai.OpenAI()
response = client.chat.completions.create(
messages=[
{"role": "user", "content": "What is the capital of France?"},
],
model="gpt-4o-mini",
max_tokens=3,
)
assert isinstance(response.choices[0].message.content, str)
gr.Info("OpenAI API key submitted.", duration=3)
return "Setting complete."
except openai.AuthenticationError as e:
gr.Error("OpenAI API key is invalid.", duration=3)
return "Not Set"
except AssertionError as e:
gr.Error("OpenAI server is not working properly.", duration=3)
return "Not Set"
def on_submit_llama_cloud_key(llama_cloud_key):
from llama_parse import LlamaParse
os.environ["LLAMA_CLOUD_API_KEY"] = llama_cloud_key
# Test llama cloud key
try:
parser = LlamaParse(
result_type="markdown" # "markdown" and "text" are available
)
return "Setting complete."
except:
gr.Error("LLAMA Cloud API key is invalid.", duration=3)
return "Not Set"
def on_submit_upstage_key(upstage_key):
os.environ["UPSTAGE_API_KEY"] = upstage_key
# Test upstage key
try:
from llama_index.llms.upstage import Upstage
from llama_index.core.llms import ChatMessage
llm = Upstage()
response: ChatResponse = llm.chat(messages=[
ChatMessage(role="system", content="You are a helpful assistant."),
ChatMessage(role="user", content="Hi, how are you?")
], max_tokens=3)
assert isinstance(response.message.content, str)
assert bool(response.message.content)
return "Setting complete."
except:
gr.Error("Upstage API key is invalid.", duration=3)
return "Not Set"
|