mirror of
https://github.com/hwchase17/langchain.git
synced 2026-02-21 06:33:41 +00:00
Thank you for contributing to LangChain!
- [x] **PR title**: "community:add Yi LLM", "docs:add Yi Documentation"
- [x] **PR message**: ***Delete this entire checklist*** and replace
with
- **Description:** This PR adds support for the Yi model to LangChain.
- **Dependencies:**
[langchain_core,requests,contextlib,typing,logging,json,langchain_community]
- **Twitter handle:** 01.AI
- [x] **Add tests and docs**: I've added the corresponding documentation
to the relevant paths
---------
Co-authored-by: Bagatur <baskaryan@gmail.com>
Co-authored-by: isaac hershenson <ihershenson@hmc.edu>
134 lines
3.4 KiB
Plaintext
134 lines
3.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Yi\n",
|
|
"[01.AI](https://www.lingyiwanwu.com/en), founded by Dr. Kai-Fu Lee, is a global company at the forefront of AI 2.0. They offer cutting-edge large language models, including the Yi series, which range from 6B to hundreds of billions of parameters. 01.AI also provides multimodal models, an open API platform, and open-source options like Yi-34B/9B/6B and Yi-VL."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"## Installing the langchain packages needed to use the integration\n",
|
|
"%pip install -qU langchain-community"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Prerequisite\n",
|
|
"An API key is required to access Yi LLM API. Visit https://www.lingyiwanwu.com/ to get your API key. When applying for the API key, you need to specify whether it's for domestic (China) or international use."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Use Yi LLM"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\n",
|
|
"\n",
|
|
"os.environ[\"YI_API_KEY\"] = \"YOUR_API_KEY\"\n",
|
|
"\n",
|
|
"from langchain_community.llms import YiLLM\n",
|
|
"\n",
|
|
"# Load the model\n",
|
|
"llm = YiLLM(model=\"yi-large\")\n",
|
|
"\n",
|
|
"# You can specify the region if needed (default is \"auto\")\n",
|
|
"# llm = YiLLM(model=\"yi-large\", region=\"domestic\") # or \"international\"\n",
|
|
"\n",
|
|
"# Basic usage\n",
|
|
"res = llm.invoke(\"What's your name?\")\n",
|
|
"print(res)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Generate method\n",
|
|
"res = llm.generate(\n",
|
|
" prompts=[\n",
|
|
" \"Explain the concept of large language models.\",\n",
|
|
" \"What are the potential applications of AI in healthcare?\",\n",
|
|
" ]\n",
|
|
")\n",
|
|
"print(res)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Streaming\n",
|
|
"for chunk in llm.stream(\"Describe the key features of the Yi language model series.\"):\n",
|
|
" print(chunk, end=\"\", flush=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Asynchronous streaming\n",
|
|
"import asyncio\n",
|
|
"\n",
|
|
"\n",
|
|
"async def run_aio_stream():\n",
|
|
" async for chunk in llm.astream(\n",
|
|
" \"Write a brief on the future of AI according to Dr. Kai-Fu Lee's vision.\"\n",
|
|
" ):\n",
|
|
" print(chunk, end=\"\", flush=True)\n",
|
|
"\n",
|
|
"\n",
|
|
"asyncio.run(run_aio_stream())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Adjusting parameters\n",
|
|
"llm_with_params = YiLLM(\n",
|
|
" model=\"yi-large\",\n",
|
|
" temperature=0.7,\n",
|
|
" top_p=0.9,\n",
|
|
")\n",
|
|
"\n",
|
|
"res = llm_with_params(\n",
|
|
" \"Propose an innovative AI application that could benefit society.\"\n",
|
|
")\n",
|
|
"print(res)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|