File size: 1,473 Bytes
1c8a9a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Q&A Chatbot
from langchain_community.llms import HuggingFaceEndpoint
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate

# from langchain.llms import OpenAI
# uncomment above line when I have credit in OpenAI
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env

import streamlit as st
import os

## Function to load AI model and get responses. Here I can incorporate prompt template also

def get_model_response(question):
    llm = HuggingFaceEndpoint(
        repo_id="mistralai/Mistral-7B-Instruct-v0.2", max_length=128, temperature=0.5, token=os.getenv("HUGGINGFACEHUB_API_TOKEN"))
    
    # llm = OpenAI(openai_api_key=os.getenv("OPENAI_API_KEY"), model_name="text-davinci-003", temperature=0.6, max_tokens=64)
    # uncomment above line and use it instead of HuggingfaceEndpoint when I have credit in OpenAI.
    template = """Question: {question}
    Answer:"""
    prompt = PromptTemplate.from_template(template)
    llm_chain = LLMChain(prompt=prompt, llm=llm)
    response = llm_chain.invoke({"question": question})
    return response


## Initialize our StreamLit app
st.set_page_config(page_title="Simple Chatbot")

st.header("Langchain Application - Simple Chatbot")

input = st.text_input("Input: ", key="input")
response = get_model_response(input)

submit = st.button("Ask the question")

## If ask button is clicked
if submit:
    st.subheader("The response is: ")
    st.write(response)