SysModeler commited on
Commit
eaf3ce7
·
verified ·
1 Parent(s): 7e38020

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -27
app.py CHANGED
@@ -3,10 +3,10 @@ import warnings
3
  import gradio as gr
4
  from dotenv import load_dotenv
5
 
6
- from langchain.chains import ConversationalRetrievalChain
7
  from langchain_community.vectorstores import FAISS
8
  from langchain_community.embeddings import AzureOpenAIEmbeddings
9
- from langchain_community.chat_models import AzureChatOpenAI
 
10
 
11
  # Patch Gradio bug (schema parsing issue)
12
  import gradio_client.utils
@@ -17,49 +17,56 @@ load_dotenv()
17
  AZURE_OPENAI_API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
18
  AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
19
  AZURE_OPENAI_LLM_DEPLOYMENT = os.getenv("AZURE_OPENAI_LLM_DEPLOYMENT")
 
20
 
21
- if not all([AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_LLM_DEPLOYMENT]):
22
  raise ValueError("Azure OpenAI environment variables are missing.")
23
 
24
  # Suppress warnings
25
  warnings.filterwarnings("ignore")
26
 
27
- # Initialize embedding model (must be deployed separately)
28
- embeddings = AzureOpenAIEmbeddings(
29
- azure_deployment="text-embedding-ada-002", # make sure this deployment exists
30
- azure_endpoint=AZURE_OPENAI_ENDPOINT,
31
- openai_api_key=AZURE_OPENAI_API_KEY,
32
- openai_api_version="2024-08-01-preview",
33
- chunk_size=1000
34
- )
35
 
36
  # Load FAISS vector store
37
  vectorstore = FAISS.load_local(
38
  "faiss_index_sysml", embeddings, allow_dangerous_deserialization=True
39
  )
40
 
41
- # Initialize LLM
42
- llm = AzureChatOpenAI(
43
- deployment_name=AZURE_OPENAI_LLM_DEPLOYMENT,
44
  azure_endpoint=AZURE_OPENAI_ENDPOINT,
45
- openai_api_key=AZURE_OPENAI_API_KEY,
46
- openai_api_version="2024-08-01-preview",
47
- temperature=0.5
48
- )
49
-
50
- # Build conversational chain with history
51
- qa = ConversationalRetrievalChain.from_llm(
52
- llm=llm,
53
- retriever=vectorstore.as_retriever(),
54
- return_source_documents=False
55
  )
56
 
57
  history = []
58
 
59
- # Chatbot logic
60
  def sysml_chatbot(message, history):
61
- result = qa({"question": message, "chat_history": history})
62
- answer = result["answer"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  history.append((message, answer))
64
  return "", history
65
 
 
3
  import gradio as gr
4
  from dotenv import load_dotenv
5
 
 
6
  from langchain_community.vectorstores import FAISS
7
  from langchain_community.embeddings import AzureOpenAIEmbeddings
8
+ from langchain.embeddings.openai import OpenAIEmbeddings
9
+ from openai import AzureOpenAI
10
 
11
  # Patch Gradio bug (schema parsing issue)
12
  import gradio_client.utils
 
17
  AZURE_OPENAI_API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
18
  AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
19
  AZURE_OPENAI_LLM_DEPLOYMENT = os.getenv("AZURE_OPENAI_LLM_DEPLOYMENT")
20
+ embeddings = OpenAIEmbeddings()
21
 
22
+ if not all([AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_LLM_DEPLOYMENT, OPENAI_EMBEDDING]):
23
  raise ValueError("Azure OpenAI environment variables are missing.")
24
 
25
  # Suppress warnings
26
  warnings.filterwarnings("ignore")
27
 
28
+ # Initialize embedding model
29
+ # embeddings = AzureOpenAIEmbeddings(
30
+ # azure_deployment=OPENAI_EMBEDDING,
31
+ # azure_endpoint=AZURE_OPENAI_ENDPOINT,
32
+ # openai_api_key=AZURE_OPENAI_API_KEY,
33
+ # openai_api_version="2024-08-01-preview",
34
+ # chunk_size=1000
35
+ # )
36
 
37
  # Load FAISS vector store
38
  vectorstore = FAISS.load_local(
39
  "faiss_index_sysml", embeddings, allow_dangerous_deserialization=True
40
  )
41
 
42
+ # Initialize Azure OpenAI client directly
43
+ client = AzureOpenAI(
44
+ api_key=AZURE_OPENAI_API_KEY,
45
  azure_endpoint=AZURE_OPENAI_ENDPOINT,
46
+ api_version="2024-08-01-preview"
 
 
 
 
 
 
 
 
 
47
  )
48
 
49
  history = []
50
 
51
+ # Chatbot logic using AzureOpenAI directly
52
  def sysml_chatbot(message, history):
53
+ # Perform retrieval
54
+ retriever = vectorstore.as_retriever()
55
+ docs = retriever.get_relevant_documents(message)
56
+ context = "\n\n".join(doc.page_content for doc in docs[:4])
57
+
58
+ # Compose prompt with retrieved context
59
+ system_prompt = "You are a helpful assistant knowledgeable in SysML. Use the context below to answer the user's question.\n\nContext:\n" + context
60
+
61
+ response = client.chat.completions.create(
62
+ model=AZURE_OPENAI_LLM_DEPLOYMENT,
63
+ messages=[
64
+ {"role": "system", "content": system_prompt},
65
+ {"role": "user", "content": message}
66
+ ]
67
+ )
68
+
69
+ answer = response.choices[0].message.content
70
  history.append((message, answer))
71
  return "", history
72