LangChain
LangChain is a framework for developing applications powered by language models. To learn more, visit the LangChain website.
We offer the following modules:
- Chat adapter for most of our LLMs
- LLM adapter for most of our LLMs
- Embeddings adapter for all of our Embeddings models
Install LangChain
pip install langchain
pip install langchain-community
LLM Examples
The examples below show how to use LangChain with ByteCompute for language models. Make sure to get your API key from ByteCompute. You have to Login and get your token.
Please set os.environ["ByteCompute_API_TOKEN"] with your token.
Read comments in the code for better understanding.
import os
from langchain_community.llms import ByteCompute
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
# Make sure to get your API key from ByteCompute. You have to Login and get a new token.
os.environ["ByteCompute_API_TOKEN"] = '<your ByteCompute API token>'
# Create the ByteCompute instance. You can view a list of available parameters in the model page
llm = ByteCompute(model_id="meta-llama/Meta-Llama-3-8B-Instruct")
llm.model_kwargs = {
"temperature": 0.7,
"repetition_penalty": 1.2,
"max_new_tokens": 250,
"top_p": 0.9,
}
def example1():
# run inference
print(llm.invoke("Who let the dogs out?"))
def example2():
# run streaming inference
for chunk in llm.stream("Who let the dogs out?"):
print(chunk)
def example3():
# create a prompt template for Question and Answer
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate(template=template, input_variables=["question"])
# initiate the chain
llm_chain = prompt | llm
# provide a question and run the LLMChain
question = "Can penguins reach the North pole?"
print(llm_chain.invoke(question))
# run examples
example1()
Chat Examples
Ensure the ByteCompute_API_KEY env is set to your api key.
import os
# or pass ByteCompute_api_token parameter to the ChatByteCompute constructor
os.environ["ByteCompute_API_TOKEN"] = ByteCompute_API_TOKEN
from langchain_community.chat_models import ChatByteCompute
from langchain_core.messages import HumanMessage
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
messages = [
HumanMessage(
content="Translate this sentence from English to French. I love programming."
)
]
def example_sync():
chat = ChatByteCompute(model="meta-llama/Meta-Llama-3-8B-Instruct")
print(chat.invoke(messages))
async def example_async():
chat = ChatByteCompute(model="meta-llama/Meta-Llama-3-8B-Instruct")
await chat.agenerate([messages])
def example_stream():
chat = ChatByteCompute(
streaming=True,
verbose=True,
callbacks=[StreamingStdOutCallbackHandler()],
)
print(chat.invoke(messages))
Embeddings
import os
os.environ["ByteCompute_API_TOKEN"] = ByteCompute_API_TOKEN
from langchain_community.embeddings import ByteComputeEmbeddings
embeddings = ByteComputeEmbeddings(
model_id="sentence-transformers/clip-ViT-B-32",
query_instruction="",
embed_instruction="",
)
docs = ["Dog is not a cat", "Beta is the second letter of Greek alphabet"]
document_result = embeddings.embed_documents(docs)
print(document_result)
