You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需Azure账号登录,如何部署共享基于Azure的RAG聊天机器人?

问题描述

我在Azure AI Foundry上搭建了一个功能正常的Retrieval-Augmented Generation(RAG)聊天机器人,但部署后发现用户必须登录Azure账号才能使用。我想把这个机器人分享给他人测试,但不想要求用户登录Azure。我目前考虑自建前端+后端,用Streamlit部署,但我对这个完全陌生,担心是不是想复杂了。有没有更简便的部署共享方法,让用户不用登录Azure就能用?

现有代码

app.py

import streamlit as st
import requests

API_URL = "http://127.0.0.1:8000/chat"

st.title("RAG Chatbot")

if 'messages' not in st.session_state:
    st.session_state.messages = []

for message in st.session_state.messages:
    with st.chat_message(message['role']):
        st.markdown(message['content'])

prompt = st.chat_input("Ask me something:")

if prompt:
    st.session_state.messages.append({'role': 'user', 'content': prompt})
    st.chat_message('user').markdown(prompt)

    response = requests.post(API_URL, json={"message": prompt})
    
    if response.status_code == 200:
        data = response.json()
        assistant_response = data.get("response", "I'm sorry, I couldn't generate a response.")
        references = data.get("references", [])
        
        response_text = assistant_response
        if references:
            response_text += "\n\n**References:**\n" + "\n".join([f"- [{ref['source']}]({ref['source']})" for ref in references])
        
        st.session_state.messages.append({'role': 'assistant', 'content': response_text})
        st.chat_message('assistant').markdown(response_text)
    else:
        st.error("Something went wrong!")

main.py

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from azure.ai.inference import ChatCompletionsClient
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
import os
from fastapi.middleware.cors import CORSMiddleware
from dotenv import load_dotenv

load_dotenv()

AZURE_API_KEY = os.getenv("AZURE_API_KEY")
AZURE_ENDPOINT = os.getenv("AZURE_ENDPOINT")
AZURE_SEARCH_SERVICE = os.getenv("AZURE_SEARCH_SERVICE")
AZURE_SEARCH_INDEX = os.getenv("AZURE_SEARCH_INDEX")
AZURE_SEARCH_KEY = os.getenv("AZURE_SEARCH_KEY")

if not all([AZURE_API_KEY, AZURE_ENDPOINT, AZURE_SEARCH_SERVICE, AZURE_SEARCH_INDEX, AZURE_SEARCH_KEY]):
    raise ValueError("Missing one or more required environment variables!")

chat_client = ChatCompletionsClient(endpoint=AZURE_ENDPOINT, credential=AzureKeyCredential(AZURE_API_KEY))

search_client = SearchClient(
    endpoint=f"https://{AZURE_SEARCH_SERVICE}.search.windows.net",
    index_name=AZURE_SEARCH_INDEX,
    credential=AzureKeyCredential(AZURE_SEARCH_KEY)
)

print("Clients initialized successfully!")

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class ChatRequest(BaseModel):
    message: str

def search_documents(query):
    try:
        results = search_client.search(search_text=query, top=3)
        sources = []
        
        for doc in results:
            content = doc.get("content", "No content available.")
            source_url = doc.get("source_url")  
            
            if source_url:
                sources.append({"snippet": content[:300] + "...", "source": source_url})
            else:
                sources.append({"snippet": content[:300] + "...", "source": "Unknown source"})
        
        return sources if sources else None 
    except Exception as e:
        print(f"Search error: {e}")
        return None

@app.post("/chat")
async def chat(request: ChatRequest):
    try:
        sources = search_documents(request.message)

        if not sources:
            return {
                "response": "I'm sorry, but I don't have enough information to answer that question.",
                "references": []
            }

        context = "\n".join([s["snippet"] for s in sources])

        messages = [
            {"role": "system", "content": "You are a helpful assistant that answers questions based ONLY on the provided documents."},
            {"role": "system", "content": f"Here is relevant information:\n{context}"},
            {"role": "user", "content": request.message}
        ]

        response = chat_client.complete(
            model="model_name", 
            messages=messages
        )

        return {
            "response": response.choices[0].message["content"],
            "references": sources 
        }
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
解决方案建议

方法1:整合逻辑到单文件Streamlit应用(最简便)

你当前的前后端分离架构可以简化,把FastAPI里的Azure客户端初始化、文档搜索、大模型调用逻辑直接移到Streamlit的app.py中,变成单文件应用:

  • 修改app.py,移除对FastAPI后端的请求代码,直接嵌入main.py里的核心逻辑
  • 将所有Azure相关的环境变量配置到Streamlit部署平台
  • 部署到Streamlit Community Cloud(免费):
    1. 把代码推送到GitHub仓库
    2. 登录Streamlit社区云,关联你的GitHub仓库
    3. 在部署设置中添加所有Azure环境变量(AZURE_API_KEY、AZURE_ENDPOINT等)
    4. 点击部署后生成的公开链接,可直接分享给他人使用,无需登录

方法2:调整Azure部署的访问权限

如果不想自建前端,可在Azure AI Studio中修改机器人的部署设置:

  • 找到已部署的聊天机器人,关闭Azure AD认证,开启匿名访问
  • 注意:此方式需谨慎,公开访问可能导致API资源被滥用,建议同时配置流量限制或使用Azure API Management管控访问

方法3:简化前后端分离部署

若要保留现有架构,可将FastAPI后端和Streamlit前端都部署到Azure App Service:

  • 把FastAPI应用部署到Azure App Service,确保CORS设置正确
  • 修改Streamlit的API_URL为FastAPI后端的App Service地址,再部署到另一个Azure App Service实例
  • 两个服务均设置为允许匿名访问,用户访问前端链接即可直接使用,无需登录Azure

内容的提问来源于stack exchange,提问作者Liam Mason

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 06:04:53