File size: 9,055 Bytes
0318caf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3295268
 
 
 
 
 
e5bba07
b3ac5bf
3295268
b3ac5bf
 
4b46c96
b3ac5bf
 
 
 
 
 
 
 
 
 
 
4b46c96
b3ac5bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417a23b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d53d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dda8f2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3ac5bf
dda8f2a
d35c879
dda8f2a
 
 
 
0318caf
d5a8216
dda8f2a
d5a8216
d35c879
b307974
 
d35c879
d5a8216
 
 
 
 
 
 
 
 
 
 
 
dda8f2a
 
417a23b
dda8f2a
 
 
 
 
 
 
d5a8216
 
b307974
417a23b
dda8f2a
 
b3ac5bf
d35c879
b307974
 
d35c879
 
b307974
0318caf
4b46c96
417a23b
0d53d23
dda8f2a
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# import os
# import gradio as gr
# from langchain.chat_models import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.chains import LLMChain
# from langchain.memory import ConversationBufferMemory

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory
# memory = ConversationBufferMemory(memory_key="chat_history")

# # Define the LLM chain with the ChatOpenAI model and conversation memory
# llm_chain = LLMChain(
#     llm=ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo"),  # Use 'model' instead of 'model_name'
#     prompt=prompt,
#     verbose=True,
#     memory=memory,
# )

# # Function to get chatbot response
# def get_text_response(user_message, history):
#     response = llm_chain.predict(user_message=user_message)
#     return response

# # Create a Gradio chat interface
# demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")

# if __name__ == "__main__":
#     demo.launch()

# import subprocess

# # Run the Bash script that installs dependencies and runs the app
# subprocess.run(['./run.sh'])

# # Rest of your application code can go here
# import subprocess
# import os

# # Ensure the run.sh script has executable permissions
# # subprocess.run(['chmod', '+x', './run.sh'])

# # Run the Bash script that installs dependencies and runs the app
# # subprocess.run(['./run.sh'])

# import gradio as gr
# from langchain_openai import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.memory import ConversationBufferMemory
# from langchain.chains import RunnableSequence

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory
# memory = ConversationBufferMemory(memory_key="chat_history")

# # Define the LLM (language model)
# llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")

# # Define the chain using RunnableSequence (replace LLMChain)
# llm_chain = prompt | llm  # Chaining the prompt and the LLM

# # Function to get chatbot response
# def get_text_response(user_message, history):
#     inputs = {"chat_history": history, "user_message": user_message}
#     response = llm_chain(inputs)
#     return response['text']

# # Create a Gradio chat interface
# demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")

# if __name__ == "__main__":
#     demo.launch()


# import os
# import gradio as gr
# from langchain_openai import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.memory import ConversationBufferMemory
# from langchain.chains import LLMChain

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory
# memory = ConversationBufferMemory(memory_key="chat_history")

# # Define the LLM (language model) and chain
# llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")
# llm_chain = LLMChain(
#     llm=llm,
#     prompt=prompt,
#     verbose=True,
#     memory=memory,
# )

# # Function to get chatbot response
# def get_text_response(user_message, history):
#     response = llm_chain.predict(user_message=user_message)
#     return response

# # Create a Gradio chat interface
# demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")

# if __name__ == "__main__":
#     demo.launch()

# import os
# import gradio as gr
# from langchain_openai import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.memory import ConversationBufferMemory
# from langchain.schema import AIMessage, HumanMessage
# from langchain.chains import RunnableSequence

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory (following migration guide)
# memory = ConversationBufferMemory(return_messages=True)  # Use return_messages=True for updated usage

# # Define the LLM (language model)
# llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")

# # Create the RunnableSequence instead of LLMChain
# llm_sequence = prompt | llm  # This pipelines the prompt into the language model

# # Function to get chatbot response
# def get_text_response(user_message, history):
#     # Prepare the conversation history
#     chat_history = [HumanMessage(content=user_message)]
    
#     # Pass the prompt and history to the language model sequence
#     response = llm_sequence.invoke({"chat_history": history, "user_message": user_message})
    
#     return response

# # Create a Gradio chat interface
# demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")

# if __name__ == "__main__":
#     demo.launch()

# import os
# import gradio as gr
# from langchain_openai import ChatOpenAI
# from langchain.prompts import PromptTemplate
# from langchain.memory import ConversationBufferMemory
# from langchain.schema import AIMessage, HumanMessage
# from langchain import Runnable  # Using Runnable instead of RunnableSequence

# # Set OpenAI API Key
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# # Define the template for the chatbot's response
# template = """You are a helpful assistant to answer all user queries.
# {chat_history}
# User: {user_message}
# Chatbot:"""

# # Define the prompt template
# prompt = PromptTemplate(
#     input_variables=["chat_history", "user_message"], 
#     template=template
# )

# # Initialize conversation memory (following migration guide)
# memory = ConversationBufferMemory(return_messages=True)  # Use return_messages=True for updated usage

# # Define the LLM (language model)
# llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")

# # Create the Runnable instead of RunnableSequence
# llm_runnable = Runnable(lambda inputs: prompt.format(**inputs)) | llm

# # Function to get chatbot response
# def get_text_response(user_message, history):
#     # Prepare the conversation history
#     chat_history = [HumanMessage(content=user_message)]
    
#     # Pass the prompt and history to the language model sequence
#     response = llm_runnable.invoke({"chat_history": history, "user_message": user_message})
    
#     return response

# # Create a Gradio chat interface
# demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")

# if __name__ == "__main__":
#     demo.launch()

import os
import subprocess
import gradio as gr

# Install necessary packages
subprocess.check_call(["pip", "install", "-U", "langchain-openai", "gradio", "langchain-community"])

from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemory

# Set OpenAI API Key
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

# Define the template for the chatbot's response
template = """You are a helpful assistant to answer all user queries.
{chat_history}
User: {user_message}
Chatbot:"""

# Define the prompt template
prompt = PromptTemplate(
    input_variables=["chat_history", "user_message"], 
    template=template
)

# Initialize conversation memory
memory = ConversationBufferMemory(memory_key="chat_history")

# Define the LLM chain with the ChatOpenAI model and conversation memory
llm_chain = LLMChain(
    llm=ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo"),  # Use 'model' instead of 'model_name'
    prompt=prompt,
    verbose=True,
    memory=memory,
)

# Function to get chatbot response
def get_text_response(user_message, history):
    # Prepare the conversation history
    chat_history = history + [f"User: {user_message}"]
    response = llm_chain.predict(user_message=user_message, chat_history=chat_history)
    return response

# Create a Gradio chat interface
demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")

if __name__ == "__main__":
    demo.launch()