{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["\"在\n"]}, {"cell_type": "markdown", "metadata": {}, "source": ["# LM Studio\n"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## 设置\n"]}, {"cell_type": "markdown", "metadata": {}, "source": ["1. 下载并安装LM Studio\n", "2. 按照[README](https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/llms/llama-index-llms-lmstudio/README.md)中提到的步骤进行操作。\n"]}, {"cell_type": "markdown", "metadata": {}, "source": ["如果在collab中尚未安装,请安装*llama-index*和*lmstudio*集成。\n"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["%pip install llama-index-core llama-index llama-index-llms-lmstudio"]}, {"cell_type": "markdown", "metadata": {}, "source": ["修复 \"RuntimeError: This event loop is already running\" 的问题\n", "\n", "当在使用 asyncio 库时出现 \"RuntimeError: This event loop is already running\" 错误时,可以尝试以下解决方法:\n", "\n", "1. 使用 `asyncio.get_event_loop()` 获取事件循环对象,并在运行事件循环之前检查是否已经运行。例如:\n", "\n", "```python\n", "import asyncio\n", "\n", "loop = asyncio.get_event_loop()\n", "if not loop.is_running():\n", " loop.run_until_complete(some_coroutine())\n", "```\n", "\n", "2. 在 Jupyter 环境中,可以使用 `nest_asyncio` 库来解决此问题。首先安装 `nest_asyncio` 库,然后在代码中使用以下方式解决问题:\n", "\n", "```python\n", "import nest_asyncio\n", "nest_asyncio.apply()\n", "```\n", "\n", "通过以上方法,可以解决 \"RuntimeError: This event loop is already running\" 错误。\n"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["import nest_asyncio\n", "\n", "nest_asyncio.apply()"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from llama_index.llms.lmstudio import LMStudio\n", "from llama_index.core.base.llms.types import ChatMessage, MessageRole"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["llm = LMStudio(\n", " model_name=\"Hermes-2-Pro-Llama-3-8B\",\n", " base_url=\"http://localhost:1234/v1\",\n", " temperature=0.7,\n", ")"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["The result of 2 + 2 is 4.\n"]}], "source": ["response = llm.complete(\"Hey there, what is 2+2?\")\n", "print(str(response))"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["The result of 7 + 3 is 10."]}], "source": ["# 使用llm.stream_complete\n", "response = llm.stream_complete(\"What is 7+3?\")\n", "for r in response:\n", " print(r.delta, end=\"\")"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["messages = [\n", " ChatMessage(\n", " role=MessageRole.SYSTEM,\n", " content=\"You an expert AI assistant. Help User with their queries.\",\n", " ),\n", " ChatMessage(\n", " role=MessageRole.USER,\n", " content=\"What is the significance of the number 42?\",\n", " ),\n", "]"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["assistant: The number 42 has been significant in various contexts throughout history and across different cultures, often holding symbolic or philosophical meanings. \n", "\n", "1. In mathematics: 42 is a relatively simple but still interesting whole number with no factors other than 1 and itself.\n", "\n", "2. In popular culture: Douglas Adams' science fiction series \"The Hitchhiker's Guide to the Galaxy\" presents the ultimate answer to the meaning of life as 42, which has become a well-known joke and meme since its introduction in the first book published in 1979.\n", "\n", "3. In religion and mythology: The number 42 appears in various religious texts or myths with different meanings, such as the Biblical Book of Numbers where Moses spent 42 years tending to his father-in-law's flock before receiving the call from God, or in Norse mythology when Odin spent 42 nights suspended on Yggdrasil (the World Tree) to gain knowledge.\n", "\n", "4. In sports: In baseball, a perfect game is considered to be an immaculate game with no hits, errors, or runners allowed to reach base; only 15 players can achieve this in Major League Baseball history, and the number of their names added together equals 42 (6 + 2 = 8, 3 + 4 = 7).\n", "\n", "5. In music: The English rock band Coldplay's popular song \"42\" is about lead singer Chris Martin reflecting on his age during the time it took for the band to gain success.\n", "\n", "The significance of the number 42 varies depending on the context and cultural background. It has often been used symbolically or metaphorically, making it a versatile and intriguing number in various aspects of human life.\n"]}], "source": ["response = llm.chat(messages=messages)\n", "print(str(response))"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["The number 42 has various significances in different contexts:\n", "\n", "1. In popular culture: The famous \"Answer to the Ultimate Question of Life, the Universe and Everything\" from Douglas Adams' science fiction series \"The Hitchhiker's Guide to the Galaxy\" is 42. This has led to widespread recognition of the number as something meaningful or profound.\n", "\n", "2. Mathematics: The number 42 is a highly composite number with many divisors (1, 2, 3, 6, 7, 14, 21, and 42). In mathematics, the study of factors and divisors plays an essential role in various concepts such as prime factorization and greatest common denominators.\n", "\n", "3. Christianity: According to a story from The Book of Kells (an illuminated manuscript), it is said that St. Patrick used the number 42 to calculate when to begin his mission to convert Ireland to Christianity.\n", "\n", "4. Astrology: In astrology, the 42nd day after the Winter Solstice marks the beginning of the new astrological year and the start of a 13-month cycle in some traditions.\n", "\n", "5. Literature: The number 42 is mentioned several times throughout William Shakespeare's plays, such as \"Hamlet\" and \"Henry IV.\" It appears as a coincidence or possibly with symbolic intent in these works.\n", "\n", "6. In the field of computer science, the popular programming language 'Python' uses 42 as its \"magic number\" to represent the start-up code for the interpreter.\n", "\n", "Each context assigns a different significance to the number 42, making it multi-faceted and culturally relevant in various ways."]}], "source": ["response = llm.stream_chat(messages=messages)\n", "for r in response:\n", " print(r.delta, end=\"\")"]}], "metadata": {"kernelspec": {"display_name": "llama_index", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3"}}, "nbformat": 4, "nbformat_minor": 2}