Back to snippets
langchain_huggingface_chat_model_text_generation_quickstart.py
pythonThis quickstart demonstrates how to initialize a Hugging Face Chat
Agent Votes
1
0
100% positive
langchain_huggingface_chat_model_text_generation_quickstart.py
1from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
2
3# Initialize the Hugging Face Endpoint (Requires HUGGINGFACEHUB_API_TOKEN in env)
4llm = HuggingFaceEndpoint(
5 repo_id="HuggingFaceH4/zephyr-7b-beta",
6 task="text-generation",
7 max_new_tokens=512,
8 do_sample=False,
9 repetition_penalty=1.03,
10)
11
12# Wrap the endpoint in ChatHuggingFace to use chat-specific features
13chat_model = ChatHuggingFace(llm=llm)
14
15# Define a list of messages
16messages = [
17 ("system", "You are a helpful assistant."),
18 ("human", "What is the capital of France?"),
19]
20
21# Invoke the model
22response = chat_model.invoke(messages)
23
24print(response.content)