Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +50 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_core.prompts import ChatPromptTemplate
|
2 |
+
from langchain_openai import ChatOpenAI
|
3 |
+
from langchain_groq import ChatGroq
|
4 |
+
import streamlit as st
|
5 |
+
from langchain_core.output_parsers import StrOutputParser
|
6 |
+
import os
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
os.environ['LANGCHAIN_TRACING_V2'] = "true"
|
12 |
+
os.environ['LANGCHAIN_API_KEY'] = os.getenv("LANGCHAIN_API_KEY")
|
13 |
+
|
14 |
+
|
15 |
+
def get_llm_response(llm_choice, input_text):
|
16 |
+
output_parser = StrOutputParser()
|
17 |
+
|
18 |
+
if llm_choice == "OpenAI":
|
19 |
+
llm = ChatOpenAI(temperature=0.5, model="mistralai/mistral-7b-instruct:free",base_url="https://openrouter.ai/api/v1",api_key=os.getenv("OPENAI_API_KEY"))
|
20 |
+
else:
|
21 |
+
llm = ChatGroq(groq_api_key=os.getenv("GROQ_API_KEY"),model_name="mixtral-8x7b-32768")
|
22 |
+
|
23 |
+
chain = prompt | llm | output_parser
|
24 |
+
|
25 |
+
if input_text:
|
26 |
+
return chain.invoke({"question": input_text})
|
27 |
+
else:
|
28 |
+
return None
|
29 |
+
|
30 |
+
prompt = ChatPromptTemplate.from_messages(
|
31 |
+
[
|
32 |
+
("system", "You are a helpful assistant. Please respond to the queries"),
|
33 |
+
("user", "Question: {question}")
|
34 |
+
]
|
35 |
+
)
|
36 |
+
|
37 |
+
st.title("Chat with OpenAI and ChatGroq")
|
38 |
+
st.caption("Made By - Samagra Shrivastava with ♥")
|
39 |
+
input_text = st.chat_input("Enter your question here..")
|
40 |
+
|
41 |
+
llm_options = ['OpenAI', 'ChatGroq']
|
42 |
+
with st.sidebar:
|
43 |
+
st.title("Select the model of your choice")
|
44 |
+
llm_choice = st.selectbox("Choose LLM of your choice", llm_options)
|
45 |
+
|
46 |
+
response = get_llm_response(llm_choice=llm_choice, input_text=input_text)
|
47 |
+
|
48 |
+
if response:
|
49 |
+
st.write(f"**Response from {llm_choice}:**")
|
50 |
+
st.write(response)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
langchain-openai
|
3 |
+
langchain-groq
|
4 |
+
langchain-community
|
5 |
+
langchain-core
|
6 |
+
streamlit
|
7 |
+
python-dotenv
|