Skip to main content

Add your description here

Project description

Easy LLM

这个库的目的是帮助开发者轻松调用LLM,包括提示词、RAG、各种大模型的调用。提供一种轻量级的使用方法

这个库主要是为了实现一下几个目标:

  1. 供应商的封装:能够轻松调用不同供应商的模型(主要是兼容OpenAI格式)
  2. 提示词封装:用markdown替代写System, User, Asistant.
  3. 提示词管理:为用户存储,访问提示词提供一种简便的方法

参考示例:

模型调用

import os
from easyllm import EasyLLM
api_key = os.environ['DEEPSEEK_API_KEY']
llm = EasyLLM(model_name="deepseek-chat", model_provider="deepseek", api_key=api_key)
prompt = "what is 1 + 1?"
ans = llm(prmpt)

结构化输出

为了简化格式化输出,并且适配各大厂商输出模式,建议采用在提示词里面指定json输出格式,并使用参数json_mode=True 示例提示词

"""你是一个旅游助手,输出北京的特点和地理位置
输出一个json,格式如下:
{
    location
    char
}
"""

提示词

提示词使用jinja2作为模板,写提示词时候只需要考虑写字符串,不用写冗长的在字典。因为本质上都是给模型输入一个字符串,那么我们直接输入字符串的方式会更加的符合直觉。 对比示例:

OpenAI风格调用

from openai import OpenAI
client = OpenAI()
messages = [
    {"role": "system", "content": "你是一个{{ what }}助手,帮助用户解决他提出的问题"},
    {"role": "user", "content": "1+1 = ?"}
]
response = client.chat.completions.create(
    model="gpt-4",  
    messages=messages
)
print(response['choices'][0]['message']['content'])

LangChain

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import HumanMessage
from langchain.chains import LLMChain
from langchain.llms import OpenAI
import openai

prompt_template = ChatPromptTemplate([
    ("system", "You are a helpful assistant."),
    MessagesPlaceholder("msgs")
])
llm = OpenAI(model="gpt-4", openai_api_key="YOUR_API_KEY")
llm_chain = LLMChain(prompt=prompt_template, llm=llm)
user_input = [HumanMessage(content="hi!")]  # 用户的消息
response = llm_chain.run({"msgs": user_input})
print(response)

EasyLLM

from jinja2 import Template
prompt_t = Template(
    """
    # System
    你是一个{{ what }}助手,帮助用户解决他提出的问题
    # User
    1+1 = ?
    """
)
res = llm(prompt_t.render(what="数学"))
print(res)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_easy_llm-0.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_easy_llm-0.1.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file python_easy_llm-0.1.0.tar.gz.

File metadata

  • Download URL: python_easy_llm-0.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for python_easy_llm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c82e240f20b60f5174f4e1ca59c383cda507015250b101e9d4f239f29c0ab3c8
MD5 a4effb1b765de2ca9030ad2d348deb2a
BLAKE2b-256 ac7cac1024b41de274b63ed8e768510fe84758d4142cbb2ddb49b115cb0719d9

See more details on using hashes here.

File details

Details for the file python_easy_llm-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_easy_llm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c09b97ed1868eb2040fb8117b3500b94dabdca46f6b33d8ddad782aba6c0de0
MD5 68d84c184b1348c4329df6cff3b6e1d4
BLAKE2b-256 b4e3d72275469ad9a0bbafeeb02984b637e33a179f4a8436e93dae45d051a6df

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page