Spaces:
Build error
Build error
| 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" | |