Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
from langchain.prompts import PromptTemplate
|
| 5 |
-
from langchain.
|
| 6 |
from langchain.memory import ConversationBufferMemory
|
|
|
|
| 7 |
|
| 8 |
# Set OpenAI API Key
|
| 9 |
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
|
@@ -23,21 +68,21 @@ prompt = PromptTemplate(
|
|
| 23 |
# Initialize conversation memory
|
| 24 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 25 |
|
| 26 |
-
# Define the LLM
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
memory=memory,
|
| 32 |
-
)
|
| 33 |
|
| 34 |
# Function to get chatbot response
|
| 35 |
def get_text_response(user_message, history):
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
# Create a Gradio chat interface
|
| 40 |
demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
| 43 |
demo.launch()
|
|
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# import gradio as gr
|
| 3 |
+
# from langchain.chat_models import ChatOpenAI
|
| 4 |
+
# from langchain.prompts import PromptTemplate
|
| 5 |
+
# from langchain.chains import LLMChain
|
| 6 |
+
# from langchain.memory import ConversationBufferMemory
|
| 7 |
+
|
| 8 |
+
# # Set OpenAI API Key
|
| 9 |
+
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
| 10 |
+
|
| 11 |
+
# # Define the template for the chatbot's response
|
| 12 |
+
# template = """You are a helpful assistant to answer all user queries.
|
| 13 |
+
# {chat_history}
|
| 14 |
+
# User: {user_message}
|
| 15 |
+
# Chatbot:"""
|
| 16 |
+
|
| 17 |
+
# # Define the prompt template
|
| 18 |
+
# prompt = PromptTemplate(
|
| 19 |
+
# input_variables=["chat_history", "user_message"],
|
| 20 |
+
# template=template
|
| 21 |
+
# )
|
| 22 |
+
|
| 23 |
+
# # Initialize conversation memory
|
| 24 |
+
# memory = ConversationBufferMemory(memory_key="chat_history")
|
| 25 |
+
|
| 26 |
+
# # Define the LLM chain with the ChatOpenAI model and conversation memory
|
| 27 |
+
# llm_chain = LLMChain(
|
| 28 |
+
# llm=ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo"), # Use 'model' instead of 'model_name'
|
| 29 |
+
# prompt=prompt,
|
| 30 |
+
# verbose=True,
|
| 31 |
+
# memory=memory,
|
| 32 |
+
# )
|
| 33 |
+
|
| 34 |
+
# # Function to get chatbot response
|
| 35 |
+
# def get_text_response(user_message, history):
|
| 36 |
+
# response = llm_chain.predict(user_message=user_message)
|
| 37 |
+
# return response
|
| 38 |
+
|
| 39 |
+
# # Create a Gradio chat interface
|
| 40 |
+
# demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")
|
| 41 |
+
|
| 42 |
+
# if __name__ == "__main__":
|
| 43 |
+
# demo.launch()
|
| 44 |
+
|
| 45 |
import os
|
| 46 |
import gradio as gr
|
| 47 |
+
from langchain_openai import ChatOpenAI
|
| 48 |
from langchain.prompts import PromptTemplate
|
| 49 |
+
from langchain.schema import BaseMemory
|
| 50 |
from langchain.memory import ConversationBufferMemory
|
| 51 |
+
from langchain.chains import RunnableSequence
|
| 52 |
|
| 53 |
# Set OpenAI API Key
|
| 54 |
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
|
|
|
| 68 |
# Initialize conversation memory
|
| 69 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 70 |
|
| 71 |
+
# Define the LLM (language model)
|
| 72 |
+
llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")
|
| 73 |
+
|
| 74 |
+
# Define the chain using RunnableSequence (replace LLMChain)
|
| 75 |
+
llm_chain = prompt | llm # Chaining the prompt and the LLM
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# Function to get chatbot response
|
| 78 |
def get_text_response(user_message, history):
|
| 79 |
+
inputs = {"chat_history": history, "user_message": user_message}
|
| 80 |
+
response = llm_chain(inputs)
|
| 81 |
+
return response['text']
|
| 82 |
|
| 83 |
# Create a Gradio chat interface
|
| 84 |
demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
demo.launch()
|
| 88 |
+
|