Skip to main content

Langchain Integration

Langchain

LangChain is a framework for developing applications powered by large language models (LLMs)

To use Langchain with Telnyx's OpenAI compatible API run:

pip install langchain-openai

Here's some sample code to get you started

Langchain Sample Code

import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
base_url="https://api.telnyx.com/v2/ai",
api_key=os.getenv("TELNYX_API_KEY"),
model="meta-llama/Meta-Llama-3.1-8B-Instruct"
)

for chunk in llm.stream("Help me plan my vacation"):
print(chunk.content, end="", flush=True)

Note: Make sure you have set the TELNYX_API_KEY environment variable

LlamaIndex

LlamaIndex is a simple, flexible data framework for connecting custom data sources to large language models (LLMs)

To use LlamaIndex with Telnyx's OpenAI compatible API run:

pip install llama-index-core
pip install llama-index-llms-openai-like

Here's some sample code to get you started

import os
from llama_index.llms.openai_like import OpenAILike
from llama_index.core.llms import ChatMessage


llm = OpenAILike(
api_base="https://api.telnyx.com/v2/ai",
api_key=os.getenv("TELNYX_API_KEY"),
model="meta-llama/Meta-Llama-3.1-8B-Instruct",
is_chat_model=True,
)

chat = llm.stream_chat(
[
ChatMessage(role="user", content="help me plan my vacation"),
]
)
for chunk in chat:
print(chunk.delta, end="")

Note: Make sure you have set the TELNYX_API_KEY environment variable