Skip to main content

The open-source yshs library.

Project description

Here is the open library of Yuan Shang Han Shan (YSHS) co. ltd.

Install

pip install yshs

Update logs

  • 20240617, v1.1.0 支持OpenAI格式

Usage

List Models

列出所有可用的模型

import os, sys
import yshs
yshs.api_key = os.getenv('YSHS_API_KEY')

response = yshs.Models.list(refresh=True, return_all_info=True)
print(response)

Reuqest Model via OpenAI Client

from openai import OpenAI

base_url = 'https://www.yshs.vip/v1'
api_key = os.getenv("YSHS_API_KEY")
model = "openai/gpt-3.5-turbo"
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "hello"},  
]

clent = OpenAI(
    base_url='https://www.yshs.vip/v1',
    api_key=os.getenv("YSHS_API_KEY")
)

rst = client.chat.completions.create(
    model=model,
    messages=messages,
    stream=True)

full_response = ''
for chunk in res:
    x = chunk.choices[0].delta.content
    if x:
        full_response += x
        print(x, end='', flush=True)
print()  

Request AI Model

import os, sys
import yshs
yshs.api_key = os.getenv('YSHS_API_KEY')

def request_model():
    responese = yshs.LLM.chat(
        model="openai/gpt-3.5-turbo",  # 选择模型
        messages=[
          {"role": "system", "content": "You are a helpful assistant."},  # 系统提示
          {"role": "user", "content": "Who won the world series in 2020?"},  # 第一个问题
          # {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},  # 第一个的答案
          # {"role": "user", "content": "Where was it played?"}  # 第二个问题
      ]
    )

    full_response = ""
    for x in responese:
        sys.stdout.write(x)  # 逐token输出
        sys.stdout.flush()
        full_response += x
    print()
    return full_response

answer = request_model()
# print(answer)

Continuous Conversation

Create thread to continue the conversation automatically.

from yshs import Client

client = Client()  # 注;一个Client可包含多个ChatThead, 一个ChatThead对应一个Conversation(根据chat_id区分),一个Conversation有多个轮次(turns)

prompt = "hello"  # user prompt
for chunk in client.send_prompt(prompt, chat_id=None):  # send_prompt()时自动创建ChatThead
    print(chunk['response'], end='', flush=True)
print()
chat_id = chunk['chat_id']
print(f'chat_id: {chat_id}')

prompt = 'who are you?'
for chunk in client.send_prompt(prompt, chat_id=chat_id):  # send_prompt()时自动匹配ChatThead,若不存在则创建
    print(chunk['response'], end='', flush=True)
print()
print(f'chat_id: {chat_id}')

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

yshs-1.1.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

yshs-1.1.0-py2.py3-none-any.whl (7.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file yshs-1.1.0.tar.gz.

File metadata

  • Download URL: yshs-1.1.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for yshs-1.1.0.tar.gz
Algorithm Hash digest
SHA256 5a18ca1a0a06eb7fed71abcf9f740ec42d90e1f8ba8d7a35e9a43e627993ab85
MD5 3c300ca29dc1b771758029d3a90a1634
BLAKE2b-256 5f76a5901cc5cf5f3ce34a8b144c581ca8abf6a7b2d71f678b2de4bbcb531f71

See more details on using hashes here.

File details

Details for the file yshs-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: yshs-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for yshs-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f5fdf27c4b8d4c5f8a7f084b08b0e5a789c10134e3cc156d4660bdd43fe2c098
MD5 ff76b2e51c4342911cdee4357f75c98a
BLAKE2b-256 a821d45d936511041f895ee7cb15e3c627e87731ed7aba614385af27ad7fb1f2

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