-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
langchain_falcon_langsmith.py
57 lines (39 loc) · 1.7 KB
/
langchain_falcon_langsmith.py
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
from langchain import HuggingFaceHub
from langchain import PromptTemplate, LLMChain
import os
from dotenv import load_dotenv
import chainlit as cl
# Load environment variables from .env file
load_dotenv()
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
LANGCHAIN_TRACING_V2 = os.getenv("LANGCHAIN_TRACING_V2")
LANGCHAIN_ENDPOINT = os.getenv("LANGCHAIN_ENDPOINT")
LANGCHAIN_API_KEY = os.getenv("LANGCHAIN_API_KEY")
LANGCHAIN_PROJECT = os.getenv("LANGCHAIN_PROJECT")
repo_id = "tiiuae/falcon-7b-instruct"
llm = HuggingFaceHub(huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
repo_id=repo_id,
model_kwargs={"temperature":0.7, "max_new_tokens":500})
template = """Question: {question}
Answer: Let's think step by step."""
@cl.on_chat_start
async def main():
# Sending an image with the local file path
elements = [
cl.Image(name="image1", display="inline", path="falcon.jpeg")
]
await cl.Message(content="Hello there, I am Falcon. How can I help you ?", elements=elements).send()
# Instantiate the chain for that user session
prompt = PromptTemplate(template=template, input_variables=["question"])
llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=True)
# Store the chain in the user session
cl.user_session.set("llm_chain", llm_chain)
@cl.on_message
async def main(message: str):
# Retrieve the chain from the user session
llm_chain = cl.user_session.get("llm_chain") # type: LLMChain
# Call the chain asynchronously
res = await llm_chain.acall(message, callbacks=[cl.AsyncLangchainCallbackHandler()])
# Do any post processing here
# Send the response
await cl.Message(content=res["text"]).send()