Saravanan666/pypi-org huggingface dataset:
Package: langchain-llm
Langchain LLMGet StartedInstallpipinstalllangchain_llm==0.1.19Inference UsageHuggingFace InferenceCompletion Usagefromlangchain_llmimportHuggingFaceLLMllm=HuggingFaceLLM(model_name="qwen-7b-chat",model_path="/data/checkpoints/Qwen-7B-Chat",load_model_kwargs={"device_map":"auto"},)# invoke methodprompt="
user\n你是谁？
\n
assistant\n"print(llm.invoke(prompt,stop=["
"]))# Token Streamingforchunkinllm.stream(prompt,stop=["
"]):print(chunk,end="",flush=True)# openai usageprint(llm.call_as_openai(prompt,stop=["
"]))# Streamingforchunkinllm.call_as_openai(prompt,stop=["
"],stream=True):print(chunk.choices[0].text,end="",flush=True)Chat Completion Usagefromlangchain_llmimportChatHuggingFacechat_llm=ChatHuggingFace(llm=llm)# invoke methodquery="你是谁？"print(chat_llm.invoke(query))# Token Streamingforchunkinchat_llm.stream(query):print(chunk.content,end="",flush=True)# openai usagemessages=[{"role":"user","content":query}]print(chat_llm.call_as_openai(messages))# Streamingforchunkinchat_llm.call_as_openai(messages,stream=True):print(chunk.choices[0].delta.contentor"",end="",flush=True)VLLM InferenceCompletion Usagefromlangchain_llmimportVLLMllm=VLLM(model_name="qwen",model="/data/checkpoints/Qwen-7B-Chat",trust_remote_code=True,)# invoke methodprompt="
user\n你是谁？
\n
assistant\n"print(llm.invoke(prompt,stop=["
"]))# openai usageprint(llm.call_as_openai(prompt,stop=["
"]))Chat Completion Usagefromlangchain_llmimportChatVLLMchat_llm=ChatVLLM(llm=llm)# invoke methodquery="你是谁？"print(chat_llm.invoke(query))# openai usagemessages=[{"role":"user","content":query}]print(chat_llm.call_as_openai(messages))Custom Chat templatefromlangchain_llmimportBaseTemplate,ChatHuggingFaceclassCustomTemplate(BaseTemplate):@propertydeftemplate(self)->str:return("{% for message in messages %}""{{ '
' + message['role'] + '\\n' + message['content'] + '
' + '\\n' }}""{% endfor %}""{% if add_generation_prompt %}""{{ '
assistant\\n' }}""{% endif %}")chat_llm=ChatHuggingFace(llm=llm,prompt_adapter=CustomTemplate())Load Model Kwargsmodel_name_or_path: model name or path.use_fast_tokenizer: default false.device_map: "auto"、"cuda:0" etc.dtype: "half", "bfloat16", "float32".load_in_8bit: Load model in 8 bit.load_in_4bit: Load model in 4 bit.rope_scaling: Which scaling strategy should be adopted for the RoPE embeddings. Literal["linear", "dynamic"].flash_attn: Enable FlashAttention-2.Merge Lora modelfromlangchain_llmimportapply_loraapply_lora("base_model_path","lora_path","target_model_path")
Package: langchain-llm-api
Langchain LLM APIA Langchain compatible implementation which enables the integration withLLM-APIThe main reason for implementing this package is to be able to use Langchain with any model run locally.UsageYou can install this as a python library using the command (until it's integrated with langchain itself)pip install langchain-llm-apiTo use this langchain implementation with the LLM-API:from langchain_llm_api import LLMAPI, APIEmbeddings

llm = LLMAPI(
    params={"temp": 0.2},
    verbose=True
)

llm("What is the capital of France?")Or with streaming:from langchain_llm_api import LLMAPI, APIEmbeddings
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

llm = LLMAPI(
    params={"temp": 0.2},
    verbose=True,
    streaming=True,
    callback_manager=CallbackManager([StreamingStdOutCallbackHandler()])
)

llm("What is the capital of France?")CheckLLM-APIfor the possible models and thier paramsEmbeddingsto use the embeddings endpoint:emb = APIEmbeddings(
    host_name="your api host name",
    params = {"n_predict": 300, "temp": 0.2, ...}
)
Package: langchain-mistralai
langchain-mistralaiThis package contains the LangChain integrations forMistralAIthrough theirmistralaiSDK.Installationpipinstall-Ulangchain-mistralaiChat ModelsThis package contains theChatMistralAIclass, which is the recommended way to interface with MistralAI models.To use, install the requirements, and configure your environment.exportMISTRAL_API_KEY=your-api-keyThen initializefromlangchain_core.messagesimportHumanMessagefromlangchain_mistralai.chat_modelsimportChatMistralAIchat=ChatMistralAI(model="mistral-small")messages=[HumanMessage(content="say a brief hello")]chat.invoke(messages)ChatMistralAIalso supports async and streaming functionality:# For async...awaitchat.ainvoke(messages)# For streaming...forchunkinchat.stream(messages):print(chunk.content,end="",flush=True)EmbeddingsWithMistralAIEmbeddings, you can directly use the default model 'mistral-embed', or set a different one if available.Choose modelembedding.model = 'mistral-embed'Simple queryres_query = embedding.embed_query("The test information")Documentsres_document = embedding.embed_documents(["test1", "another test"])
Package: langchain-nomic
langchain-nomicThis package contains the LangChain integration with NomicInstallationpipinstall-Ulangchain-nomicAnd you should configure credentials by setting the following environment variables:NOMIC_API_KEY: your nomic API keyEmbeddingsNomicEmbeddingsclass exposes embeddings from Nomic.fromlangchain_nomicimportNomicEmbeddingsembeddings=NomicEmbeddings()embeddings.embed_query("What is the meaning of life?")
Package: langchain-openai
langchain-openaiThis package contains the LangChain integrations for OpenAI through theiropenaiSDK.Installation and SetupInstall the LangChain partner packagepipinstalllangchain-openaiGet an OpenAI api key and set it as an environment variable (OPENAI_API_KEY)LLMSee ausage example.fromlangchain_openaiimportOpenAIIf you are using a model hosted onAzure, you should use different wrapper for that:fromlangchain_openaiimportAzureOpenAIFor a more detailed walkthrough of theAzurewrapper, seehereChat modelSee ausage example.fromlangchain_openaiimportChatOpenAIIf you are using a model hosted onAzure, you should use different wrapper for that:fromlangchain_openaiimportAzureChatOpenAIFor a more detailed walkthrough of theAzurewrapper, seehereText Embedding ModelSee ausage examplefromlangchain_openaiimportOpenAIEmbeddingsIf you are using a model hosted onAzure, you should use different wrapper for that:fromlangchain_openaiimportAzureOpenAIEmbeddingsFor a more detailed walkthrough of theAzurewrapper, seehere
Package: langchain-openai-limiter
Langchain OpenAI limiterGoalBy default langchain only do retries if OpenAI queries hit limits. Which could lead to spending many resources in some cases.Moreover, OpenAI haveverydifferent tiers for different users.Like by default for GPT-4 it's something like 10 000 TPM (token per minute) and 1000 RPM (request per minute) - and up to something like 10 000 RPM and 150 000 TPM (in my personal case).Fortunately, they provide response headers with all the required info, so we don't have to monitor it ourselves.Unfortunately, neither OpenAI python library nor LangChain built on top of that do not provide easy built-in access to them.So I made this packageInstallationYou should be able to install it via pip, likepipinstalllangchain_openai_limiterExamplesYou could seeexample.ipynbnotebook for examples. However:Chat completion# LangChain built-in modelchat_model=ChatOpenAI(model_name="gpt-4-0613",streaming=True,)# Thing which will await for rate/token limitschat_model_limit_await=LimitAwaitChatOpenAI(chat_openai=chat_model,limit_await_timeout=60.0,limit_await_sleep=0.1,)# Thing which will do key rotationchat_model_key_choose=ChooseKeyChatOpenAI(chat_openai=chat_model_limit_await,openai_api_keys=[os.environ["OPENAI_API_KEY0"],os.environ["OPENAI_API_KEY1"],])all three things is compatible with LangChain's ChatModel, so:history=[SystemMessage(content="You are a helpful assistant that translates English to French."),HumanMessage(content="Translate this sentence from English to French. I love programming."),]print(chat_model_key_choose.invoke(history).content)J'aime la programmation.Async and streaming methods implemented as well.EmbeddingsPretty often we do not only need chat models - we need embeddings (for RAG, for instance) too:# LangChain built-in modelembedder_model=OpenAIEmbeddings(model="text-embedding-ada-002",)# Thing which will await for rate/token limitsembedder_model_limit_await=LimitAwaitOpenAIEmbeddings(openai_embeddings=embedder_model,limit_await_timeout=60.0,limit_await_sleep=0.1,)# Thing which will do key rotationembedder_model_key_choose=ChooseKeyOpenAIEmbeddings(openai_embeddings=embedder_model_limit_await,openai_api_keys=[os.environ["OPENAI_API_KEY0"],os.environ["OPENAI_API_KEY1"],])docs=embedder_model_key_choose.embed_documents(["Markdown is a lightweight markup language","Brainfuck is an esoteric programming language",])query=embedder_model_key_choose.embed_query("What is Markdown?")-0.01  0.03 -0.00 -0.00  0.00 ...-0.02  0.00 -0.01 -0.00 -0.00 ...-0.01  0.01  0.00 -0.01  0.00 ...TestingTo run tests - you can do the following stuffpip install langchain_openai_limiter[dev]
pytest
Package: langchain-pinecone
langchain-pineconeThis package contains the LangChain integration with Pinecone.Installationpipinstall-Ulangchain-pineconeAnd you should configure credentials by setting the following environment variables:PINECONE_API_KEYPINECONE_INDEX_NAMEUsageThePineconeclass exposes the connection to the Pinecone vector store.fromlangchain_pineconeimportPineconeVectorStoreembeddings=...# use a LangChain Embeddings classvectorstore=Pinecone(embeddings=embeddings)
Package: langchain-plantuml
Visualization UML diagram tool for LangChain workflowsSubscribe to events using a callback and store them in PlantUML format to easily visualize LangChain workflow in Activity Diagram and Sequence Diagram.
You can easily subscribe to events and keep them in a form that is easy to visualize and analyze using PlantUML.Activity DiagramSequence DiagramQuick StartInstall this library:pipinstalllangchain-plantumlThen:Add import langchain_plantuml as the first import in your Python entrypoint fileCreate a callback using the activity_diagram_callback functionHook into your LLM applicationCall the export_uml_content method of activity_diagram_callback to export the PlantUML contentSave PlantUML content to a fileExporting PlantUML to PNGRunning the minimal activity diagram example.fromlangchainimportOpenAI,LLMChain,PromptTemplatefromlangchain.memoryimportConversationBufferMemoryfromlangchain_plantumlimportdiagramtemplate="""You are a chatbot having a conversation with a human.{chat_history}Human:{human_input}Chatbot:"""prompt=PromptTemplate(input_variables=["chat_history","human_input"],template=template)memory=ConversationBufferMemory(memory_key="chat_history")activity_diagram=diagram.activity_diagram_callback(note_max_length=2000)sequence_diagram=diagram.sequence_diagram_callback(note_max_length=2000)llm_chain=LLMChain(llm=OpenAI(),prompt=prompt,verbose=True,memory=memory,callbacks=[activity_diagram,sequence_diagram])try:llm_chain.predict(human_input="What did biden say about ketanji brown jackson in the state of the union address?")finally:activity_diagram.save_uml_content("example_1_activity-plantuml.puml")sequence_diagram.save_uml_content("example_1_sequence-plantuml.puml")You will get the following PlantUML activity diagramSequence Diagramcallback_handler=diagram.sequence_diagram_callback()Custom note max Length(default 1000)callback_handler=diagram.activity_diagram_callback(note_max_length=2000)Custom note wrap width(default 500)callback_handler=diagram.activity_diagram_callback(note_wrap_width=500)Exporting PlantUML to PNGYou can downloadplantuml.1.2023.10.jarjava-DPLANTUML_LIMIT_SIZE=81920-jarplantuml-1.2023.10.jarexample-activity.puml
Package: langchain-prefect
langchain-prefectRead theaccompanying articlefor backgroundOrchestrate and observe langchain using PrefectLarge Language Models (LLMs) are interesting and useful  -  building apps that use them responsibly feels like a no-brainer. Tools likeLangchainmake it easier to build apps using LLMs. We need to know details about how our apps work, even when we want to use tools with convenient abstractions that may obfuscate those details.Prefect is built to help data people build, run, and observe event-driven workflows wherever they want. It provides a framework for creating deployments on a whole slew of runtime environments (from Lambda to Kubernetes), and is cloud agnostic (best supports AWS, GCP, Azure). For this reason, it could be a great fit for observing apps that use LLMs.FeaturesRecordLLMCallsis aContextDecoratorthat can be used to track LLM calls made by Langchain LLMs as Prefect flows.Call an LLM and track the invocation with Prefect:fromlangchain.llmsimportOpenAIfromlangchain_prefect.pluginsimportRecordLLMCallswithRecordLLMCalls():llm=OpenAI(temperature=0.9)text=("What would be a good company name for a company that makes colorful socks?")llm(text)and a flow run will be created to track the invocation of the LLM:Run several LLM calls via langchain agent as Prefect subflows:fromlangchain.agentsimportinitialize_agent,load_toolsfromlangchain.llmsimportOpenAIfromprefectimportflowllm=OpenAI(temperature=0)tools=load_tools(["llm-math"],llm=llm)agent=initialize_agent(tools,llm)@flowdefmy_flow():agent.run("How old is the current Dalai Lama? ""What is his age divided by 2 (rounded to the nearest integer)?")withRecordLLMCalls(tags={"agent"}):my_flow()Find more exampleshere.How do I get a Prefect UI?The easiest way is to use thePrefect CloudUI for free. You can find details on getting setuphere.If you don't want to sign up for cloud, you can use the dashboard locally by runningprefect server startin your terminal - more detailshere.ResourcesInstallationpipinstalllangchain-prefectRequires an installation of Python 3.10+.FeedbackIf you encounter any bugs while usinglangchain-prefect, feel free to open an issue in thelangchain-prefectrepository.Feel free to star or watchlangchain-prefectfor updates too!ContributingIf you'd like to help contribute to fix an issue or add a feature tolangchain-prefect, pleasepropose changes through a pull request from a fork of the repository.Here are the steps:Fork the repositoryClone the forked repositoryInstall the repository and its dependencies:pip install -e ".[dev]"Make desired changesAdd testsInsert an entry toCHANGELOG.mdInstallpre-committo perform quality checks prior to commit:pre-commit installgit commit,git push, and create a pull request
Package: langchain-qa-with-references
We believe thathallucinationspose a major problem in the adoption of LLMs (Language Model Models).
It is imperative to provide a simple and quick solution that allows the user to verify the coherence of the answers
to the questions they are asked. Don't forget that even answers to questions can be subject to hallucinationsThe conventional approach is to provide a list of URLs of the documents that helped in answering (see qa_with_source).
However, this approach is unsatisfactory in several scenarios:The question is asked about a PDF of over 100 pages. Each fragment comes from the same document, but from where?Some documents do not have URLs (data retrieved from a database or otherloaders).Other technical considerations make the URL approach tricky.
This is because prompts do not work well with complex URLs. This consumes a
huge number of tokens, and in the end, the result is too big.It appears essential to have a means of retrieving all references to the actual data sources
used by the model to answer the question.
It is better to return a list ofDocumentsthan a list of URLs.This includes:The precise list of documents really used for the answer (theDocuments, along with their metadata that may contain page numbers,
slide numbers, or any other information allowing the retrieval of the fragment in the original document).The excerpts of text used for the answer in each fragment. Even if a fragment is used, the LLM only utilizes a
small portion to generate the answer. Access to these verbatim excerpts helps to quickly ascertain the validity of the answer.We propose a two pipelines:qa_with_referenceandqa_with_reference_and_verbatimsfor this purpose.
It is a Question/Answer type pipeline that returns the list of documents used, and in the metadata, the list of verbatim
excerpts exploited to produce the answer. It is very similar toqa_with_sources_chainin that it is inspired by it.If the verbatim is not really from the original document, it's removed.Installpip install langchain-qa_with_referenceSample  notebookSeeherelangchain Pull-requestThis is a temporary project while I wait for my langchainpull-requestto be validated.It's experimentalFor the moment, the code is being tested in a number of environments to validate and adjust it.
The langchain framework is very instable. Some features can become depreciated.
We work to maintain compatibility as far as possible.
Package: langchain-rag
Langchain-RAGNote: Apull-requestwith this code was proposed to langchain.When splitting documents for retrieval, there are often conflicting desires:You may want to keep documents small, ensuring that their embeddings accurately represent their meaning. If they become too long, the embeddings can lose their meaning.You also want to maintain documents long enough to retain the context of each chunk.When you have a lot of documents, and therefore a lot of pieces, it's likely that dozens of pieces have a distance close to the question. Taking only the top 4 is not a good idea. The answer may lie in the 6 or 7 tracks. How can we improve the match between the question and a fragment? By preparing several versions of the fragment, each with an embedding. In this way, one of the versions can be closer to the question than the original fragment. This version is stripped of context. But the context is still needed to answer the question correctly. One strategy consists of breaking down each fragment into different versions, but using the retriever to return to the original fragment.TheRAGVectorStorestrikes a balance by splitting and storing small chunks and different variations of data. During retrieval, it initially retrieves the small chunks but then looks up the parent IDs for those chunks and returns the larger documents.The challenge lies in correctly managing the lifecycle of the three levels of documents:Original documentsChunks extracted from the original documentsTransformations of chunks to generate more vectors for improved retrievalTheRAGVectorStore, in combination with other components, is designed to address this challenge.DemoReadthis notebookOr :poetry run python -m ipykernel install --user --name langchain-ragjupyter labTipspoetry run python -m ipykernel install --user --name langchain-parent
Package: langchain-runpod-llm
Runpod LLM API Endpoint Lib for LangChainInstallation# Install the latest release version
pip install runpod-llm

# or

# Install the latest development version (main branch)
pip install git+https://https://github.com/tsangwailam/langchain-runpod-llmGet Runpod API keyGotowww.runpod.io. Create a RunPod account.From the portal, goto Settings>APIKeysCreate a new API key by click the "+ API Key" button.Usagefromrunpod_llmimportRunpodLlama2llm=RunpodLlama2(apikey="YOU_RUNPOD_API_KEY",llm_type="7b|13b",config={"max_tokens":500,#Maximum number of tokens to generate per output sequence."n":1,# Number of output sequences to return for the given prompt."best_of":1,# Number of output sequences that are generated from the prompt. From these best_of sequences, the top n sequences are returned. best_of must be greater than or equal to n. This is treated as the beam width when use_beam_search is True. By default, best_of is set to n."Presence penalty":0.2,# Float that penalizes new tokens based on whether they appear in the generated text so far. Values > 0 encourage the model to use new tokens, while values < 0 encourage the model to repeat tokens."Frequency penalty":0.5,# Float that penalizes new tokens based on their frequency in the generated text so far. Values > 0 encourage the model to use new tokens, while values < 0 encourage the model to repeat tokens."temperature":0.3,# Float that controls the randomness of the sampling. Lower values make the model more deterministic, while higher values make the model more random. Zero means greedy sampling."top_p":1,# Float that controls the cumulative probability of the top tokens to consider. Must be in (0, 1]. Set to 1 to consider all tokens."top_k":-1,# Integer that controls the number of top tokens to consider. Set to -1 to consider all tokens."use_beam_search":False,# Whether to use beam search instead of sampling.},verbose=True,# verbose output)some_prompt_template=xxxxxoutput_chain=some_prompt_template|llmoutput_chain.invoke({"input":"some input to prompt template"})
Package: langchain-token-usage
Langchain Token UsageThis repository contains a python module to track the consumption of tokens of a Large Language Models (LLMs) within aLangChainapplication.InstallationYou can installLangChain Token Usageviapip:pipinstallgit+https://github.com/mrtj/langchain_token_usage.gitor in requirements.txt:langchain-token-usage @ git+https://github.com/mrtj/langchain_token_usage.gitUsageToken usage tracking is implemented as aLangChain Callbackso it is easy to integrate in any LangChain application. The collection of the token usage metrics is LLM specific: currently only OpenAI LLMs are supported.The metrics collected can be processed locally, or sent to a metrics repository via a Reporter. Current implementation includes sending the metrics toAmazon CloudWatchservice.If you wish to use CloudWatch, you should ensure that you haveconfigured your boto3 clientto credentials that are associated to an IAM Role that can write data to CloudWatch Metrics.Example usage:fromlangchain.chainsimportLLMChainfromlangchain.chat_modelsimportChatOpenAIfromlangchain.promptsimportPromptTemplatefromlangchain_token_usage.handlersimportOpenAITokenUsageCallbackHandlerfromlangchain_token_usage.reportersimportCloudWatchTokenUsageReporterreporter=CloudWatchTokenUsageReporter(namespace="openai_token_usage",dimensions={"project":"my_test_project"})handler=OpenAITokenUsageCallbackHandler(reporter)llm=ChatOpenAI(model="gpt-4",callbacks=[handler])prompt=PromptTemplate.from_template("1 +{number}= ")chain=LLMChain(llm=llm,prompt=prompt)chain.run(number=2)The above code will perform a single call to an OpenAI GPT-4 backed LLM model, and send token usage metrics and cost estimation to CloudWatch. You can define the desired namespace of your CloudWatch metrics, and add arbitrary metrics dimensions asstr -> strmapping.ContributingContributions are very welcome.
To learn more, see theContributor Guide.LicenseDistributed under the terms of theMIT license,Langchain Token Usageis free and open source software.IssuesIf you encounter any problems,
pleasefile an issuealong with a detailed description.CreditsThis project was generated from@cjolowicz'sHypermodern Python Cookiecuttertemplate.
Package: langchain-wenxin
langchain-wenxin - Langchain Baidu WENXINWORKSHOP wrapperTable of ContentsInstallationLicenseInstallationpip install langchain-wenxinDocumentWENXINWORKSHOP API:https://cloud.baidu.com/doc/WENXINWORKSHOP/s/flfmc9do2How to useexportBAIDU_API_KEY="xxxxx"exportBAIDU_SECRET_KEY="xxxxx"fromlangchain_wenxin.llmsimportWenxin# Wenxin modelllm=Wenxin(model="ernie-bot-turbo")print(llm("你好"))# stream callforiinllm.stream("你好"):print(i)# async callimportasyncioprint(asyncio.run(llm._acall("你好")))# Wenxin chat modelfromlangchain_wenxin.chat_modelsimportChatWenxinfromlangchain.schemaimportHumanMessagellm=ChatWenxin()print(llm([HumanMessage(content="你好")]))# Wenxin embeddings modelfromlangchain_wenxin.embeddingsimportWenxinEmbeddingswenxin_embed=WenxinEmbeddings(truncate="END")print(wenxin_embed.embed_query("hello"))print(wenxin_embed.embed_documents(["hello"]))Support models:ernie-bot: Standard model,https://cloud.baidu.com/doc/WENXINWORKSHOP/s/jlil56u11Also namedwenxinfor compatibility.ernie-bot-turbo: Fast model,https://cloud.baidu.com/doc/WENXINWORKSHOP/s/4lilb2lpfAlso namedeb-instantfor compatibility.other endpoints: eg: bloomz_7b1 or other custom endpoint.Qianfan PrivateYou can setBAIDU_API_URLandBAIDU_ACCESS_CODEin environment variables.Development# Create virtual environmenthatchenvcreate# Activate virtual environmenthatchshell# Run testexportBAIDU_API_KEY="xxxxxxxx"exportBAIDU_SECRET_KEY="xxxxxxxx"hatchruntestLicenselangchain-wenxinis distributed under the terms of theMITlicense.