mirror of
https://github.com/hwchase17/langchain.git
synced 2025-10-22 17:50:03 +00:00
…tch]: import models from community ran ```bash git grep -l 'from langchain\.chat_models' | xargs -L 1 sed -i '' "s/from\ langchain\.chat_models/from\ langchain_community.chat_models/g" git grep -l 'from langchain\.llms' | xargs -L 1 sed -i '' "s/from\ langchain\.llms/from\ langchain_community.llms/g" git grep -l 'from langchain\.embeddings' | xargs -L 1 sed -i '' "s/from\ langchain\.embeddings/from\ langchain_community.embeddings/g" git checkout master libs/langchain/tests/unit_tests/llms git checkout master libs/langchain/tests/unit_tests/chat_models git checkout master libs/langchain/tests/unit_tests/embeddings/test_imports.py make format cd libs/langchain; make format cd ../experimental; make format cd ../core; make format ```
43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
import os
|
|
|
|
import cassio
|
|
import langchain
|
|
from langchain.cache import CassandraCache
|
|
from langchain.prompts import ChatPromptTemplate
|
|
from langchain.schema import BaseMessage
|
|
from langchain_community.chat_models import ChatOpenAI
|
|
from langchain_core.runnables import RunnableLambda
|
|
|
|
use_cassandra = int(os.environ.get("USE_CASSANDRA_CLUSTER", "0"))
|
|
if use_cassandra:
|
|
from .cassandra_cluster_init import get_cassandra_connection
|
|
|
|
session, keyspace = get_cassandra_connection()
|
|
cassio.init(
|
|
session=session,
|
|
keyspace=keyspace,
|
|
)
|
|
else:
|
|
cassio.init(
|
|
token=os.environ["ASTRA_DB_APPLICATION_TOKEN"],
|
|
database_id=os.environ["ASTRA_DB_ID"],
|
|
keyspace=os.environ.get("ASTRA_DB_KEYSPACE"),
|
|
)
|
|
|
|
# inits
|
|
langchain.llm_cache = CassandraCache(session=None, keyspace=None)
|
|
llm = ChatOpenAI()
|
|
|
|
|
|
# custom runnables
|
|
def msg_splitter(msg: BaseMessage):
|
|
return [w.strip() for w in msg.content.split(",") if w.strip()]
|
|
|
|
|
|
# synonym-route preparation
|
|
synonym_prompt = ChatPromptTemplate.from_template(
|
|
"List up to five comma-separated synonyms of this word: {word}"
|
|
)
|
|
|
|
chain = synonym_prompt | llm | RunnableLambda(msg_splitter)
|