repo
stringlengths
7
90
file_url
stringlengths
81
315
file_path
stringlengths
4
228
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 14:38:15
2026-01-05 02:33:18
truncated
bool
2 classes
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/openms/trafoxml.py
pyteomics/openms/trafoxml.py
""" trafoxml - reader for trafoXML files ======================================== Summary ------- **trafoXML** is a format specified in the `OpenMS <http://open-ms.sourceforge.net/about/>`_ project. It defines a transformation, which is a result of retention time alignment. This module provides a minimalistic way to extract information from **trafoXML** files. You can use the old functional interface (:py:func:`read`) or the new object-oriented interface (:py:class:`TrafoXML`) to iterate over entries in ``<Pair>`` elements. Data access ----------- :py:class:`TrafoXML` - a class representing a single trafoXML file. Other data access functions use this class internally. :py:func:`read` - iterate through pairs in a trafoXML file. Data from a single trafo are converted to a human-readable dict. :py:func:`chain` - read multiple trafoXML files at once. :py:func:`chain.from_iterable` - read multiple files at once, using an iterable of files. Dependencies ------------ This module requres :py:mod:`lxml`. -------------------------------------------------------------------------------- """ from .. import xml, auxiliary as aux, _schema_defaults class TrafoXML(xml.XML): """Parser class for trafoXML files.""" file_format = 'trafoXML' _root_element = 'TrafoXML' _default_schema = _schema_defaults._trafoxml_schema_defaults _default_version = '1.0' _default_iter_tag = 'Pair' _schema_location_param = 'noNamespaceSchemaLocation' def _get_info_smart(self, element, **kw): kw['recursive'] = kw.get('recursive', True) info = self._get_info(element, **kw) return info def read(source, read_schema=True, iterative=True): """Parse `source` and iterate through pairs. Parameters ---------- source : str or file A path to a target trafoXML file or the file object itself. read_schema : bool, optional If :py:const:`True`, attempt to extract information from the XML schema mentioned in the file header (default). Otherwise, use default parameters. Disable this to avoid waiting on slow network connections or if you don't like to get the related warnings. iterative : bool, optional Defines whether iterative parsing should be used. It helps reduce memory usage at almost the same parsing speed. Default is :py:const:`True`. Returns ------- out : iterator An iterator over the dicts with feature properties. """ return TrafoXML(source, read_schema=read_schema, iterative=iterative) chain = aux._make_chain(read, 'read')
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/openms/__init__.py
pyteomics/openms/__init__.py
from . import featurexml, trafoxml, idxml
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/mass/unimod.py
pyteomics/mass/unimod.py
""" unimod - interface to the Unimod database ========================================= This module provides an interface to the relational Unimod database. The main class is :py:class:`Unimod`. Dependencies ------------ This module requires :py:mod:`lxml` and :py:mod:`sqlalchemy`. """ # Copyright 2015 Joshua Klein, Lev Levitsky # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import re from urllib.request import urlopen from lxml import etree try: from sqlalchemy.orm import declarative_base, DeclarativeMeta except ImportError: # Moved in sqlalchemy 2.0 from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta from sqlalchemy.orm import relationship, backref, object_session from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy import (Numeric, Unicode, Column, Integer, ForeignKey, UnicodeText, Boolean, event) from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from . import mass from ..auxiliary import ensure_url_prefix model_registry = set() class SubclassRegisteringDeclarativeMeta(DeclarativeMeta): def __new__(cls, name, parents, attrs): new_type = super(SubclassRegisteringDeclarativeMeta, cls).__new__(cls, name, parents, attrs) model_registry.add(new_type) return new_type Base = declarative_base(metaclass=SubclassRegisteringDeclarativeMeta) _unimod_xml_download_url = 'http://www.unimod.org/xml/unimod_tables.xml' CompositionType = mass.Composition def simple_repr(self): # pragma: no cover template = '{self.__class__.__name__}({d})' d = {'%s=%r' % (k, v) for k, v in self.__dict__.items() if not k.startswith('_')} return template.format(self=self, d=', '.join(d)) Base.__repr__ = simple_repr def remove_namespace(doc, namespace): """Remove namespace in the passed document in place.""" ns = f'{{{namespace}}}' nsl = len(ns) for elem in doc.getiterator(): if elem.tag.startswith(ns): elem.tag = elem.tag[nsl:] def preprocess_xml(doc_path): """ Parse and drop namespaces from an XML document. Parameters ---------- doc_path : str Returns ------- out : etree.ElementTree """ urlpath = ensure_url_prefix(doc_path) tree = etree.parse(urlopen(urlpath)) root = tree.getroot() for ns in root.nsmap.values(): remove_namespace(tree, ns) return tree def _formula_parser(formula, session): """ Parse a unimod formula composed of elements, isotopes, and other bricks. In order to look up a Brick's composition, this function must have access to a session. Parameters ---------- formula : str A Unimod formula of the form `A(n) B(m)...` where A, B, ... are element names or bricks and (n), (m)... are parenthesized possibly signed integers or omitted in which case they are interpreted as 1 session : Session An active SQLAlchemy session for looking up bricks in the database Returns ------- out : CompositionType """ composition = CompositionType() for token in formula.split(' '): match = re.search(r'(?P<isotope>\d+)?(?P<elemet>[^\(]+)(?:\((?P<count>-?\d+)\))?', token) if match: isotope, element, count = match.groups() if count is not None: count = int(count) else: count = 1 if isotope is not None: name = mass._make_isotope_string(element, isotope) else: name = element is_brick = session.query(Brick).filter(Brick.brick == name).first() if is_brick is None: composition[name] += count else: composition += is_brick.composition * count return composition def _composition_listener(attr): """ Attach event listeners to an InstrumentedAttribute to trigger formula parsing on load and on change. """ @event.listens_for(attr, 'set') def _update_composition_from_formula(target, value, oldvalue, initiator): session = object_session(target) if value == '' or value is None: return # If the object hasn't been associated with a session, # we can't look up bricks. if session is None: return target.composition = _formula_parser(value, session) @event.listens_for(attr.class_, 'load') def _update_composition_on_load(target, context): value = getattr(target, attr.prop.key) if value == '' or value is None: return session = object_session(target) target.composition = _formula_parser(value, session) def has_composition(attr_name): """ A decorator to simplify flagging a Model with a column to be treated as a formula for parsing. Calls :func:`_composition_listener` internally. """ def decorator(model): _composition_listener(getattr(model, attr_name)) return model return decorator class HasFullNameMixin(object): """ A simple mixin to standardize equality operators for models with a :attr:`full_name` attribute. """ def __eq__(self, other): try: return self.full_name == other.full_name except AttributeError: return False def __ne__(self, other): return not self == other class AlternativeName(Base): __tablename__ = 'AlternativeName' _tag_name = 'alt_names_row' @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), alt_name=attrib['alt_name'], modification_id=int(attrib['mod_key']) ) return inst id = Column(Integer, primary_key=True) alt_name = Column(Unicode(256), index=True) modification_id = Column(Integer, ForeignKey('Modification.id'), index=True) class AminoAcid(Base, HasFullNameMixin): __tablename__ = 'AminoAcid' _tag_name = 'amino_acids_row' @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), full_name=attrib['full_name'], one_letter=attrib['one_letter'], three_letter=attrib['three_letter'], num_H=int(attrib['num_H']), num_O=int(attrib['num_O']), num_C=int(attrib['num_C']), num_N=int(attrib['num_N']), num_S=int(attrib['num_S']), ) return inst id = Column(Integer, primary_key=True) num_H = Column(Integer) num_O = Column(Integer) num_C = Column(Integer) num_N = Column(Integer) num_S = Column(Integer) full_name = Column(Unicode(25), index=True) one_letter = Column(Unicode(10), index=True) three_letter = Column(Unicode(10), index=True) class Classification(Base): __tablename__ = 'Classification' _tag_name = 'classifications_row' @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), classification=attrib['classification'] ) return inst id = Column(Integer, primary_key=True) classification = Column(Unicode(30), index=True) class Position(Base): __tablename__ = 'Position' _tag_name = 'positions_row' @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), position=attrib['position'] ) return inst id = Column(Integer, primary_key=True) position = Column(Unicode(20), index=True) class Brick(Base, HasFullNameMixin): __tablename__ = 'Brick' _tag_name = 'bricks_row' @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), brick=attrib['brick'], full_name=attrib['full_name'] ) return inst id = Column(Integer, primary_key=True) brick = Column(Unicode(64), index=True) full_name = Column(Unicode(128), index=True) elements = relationship('BrickToElement') @property def composition(self): composition = CompositionType() for element_relation in self.elements: symbol = element_relation.element isotope, element = re.search(r'(?P<isotope>\d+)?(?P<element>\S+)', symbol).groups() if isotope: isotope = int(isotope) iso_str = mass._make_isotope_string(element, isotope) else: iso_str = element count = element_relation.count composition[iso_str] = count return composition class Fragment(Base): __tablename__ = 'Fragment' _tag_name = 'fragments_row' @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), modification_id=int(attrib['mod_key']) ) return inst id = Column(Integer, primary_key=True) modification_id = Column(Integer, ForeignKey('Modification.id'), index=True) _fragment_composition = relationship('FragmentComposition') @property def composition(self): composition = CompositionType() session = object_session(self) for fragment_composition_relation in self._fragment_composition: symbol = fragment_composition_relation.brick_string isotope, element = re.search(r'(?P<isotope>\d+)?(?P<element>\S+)', symbol).groups() count = fragment_composition_relation.count if count is not None: count = int(count) else: count = 1 if isotope: name = mass._make_isotope_string(element, isotope) else: name = element is_brick = session.query(Brick).filter(Brick.brick == name).first() if is_brick is None: composition[name] += count else: composition += is_brick.composition * count return composition class FragmentComposition(Base): __tablename__ = 'FragmentComposition' _tag_name = 'fragment_comp_row' @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), brick_string=attrib['brick'], fragment_id=int(attrib['fragments_key']), count=int(attrib['num_brick']) ) return inst id = Column(Integer, primary_key=True) brick_string = Column(Unicode(64), ForeignKey(Brick.brick), index=True) fragment_id = Column(Integer, ForeignKey('Fragment.id'), index=True) count = Column(Integer) class ModificationToBrick(Base): __tablename__ = 'ModificationToBrick' _tag_name = 'mod2brick_row' @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), brick_string=(attrib['brick']), modification_id=int(attrib['mod_key']), count=int(attrib['num_brick']) ) return inst id = Column(Integer, primary_key=True) brick_string = Column(Unicode(64), ForeignKey(Brick.brick), index=True) modification_id = Column(Integer, ForeignKey('Modification.id'), index=True) count = Column(Integer) class BrickToElement(Base): __tablename__ = 'BrickToElement' _tag_name = 'brick2element_row' @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), brick_id=int(attrib['brick_key']), count=int(attrib['num_element']), element=attrib['element'] ) return inst id = Column(Integer, primary_key=True) brick_id = Column(Integer, ForeignKey(Brick.id), index=True) element = Column(Unicode(16), ForeignKey('Element.element'), index=True) element_obj = relationship('Element', uselist=False) count = Column(Integer) class Element(Base, HasFullNameMixin): __tablename__ = 'Element' _tag_name = 'elements_row' @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), average_mass=float(attrib['avge_mass']), monoisotopic_mass=float(attrib['mono_mass']), full_name=attrib['full_name'], element=attrib['element'] ) return inst id = Column(Integer, primary_key=True) average_mass = Column(Numeric(12, 6, asdecimal=False)) monoisotopic_mass = Column(Numeric(12, 6, asdecimal=False)) full_name = Column(Unicode(64), index=True) element = Column(Unicode(16), index=True) @has_composition('_composition') class Modification(Base, HasFullNameMixin): __tablename__ = 'Modification' _tag_name = 'modifications_row' id = Column(Integer, primary_key=True) username_of_poster = Column(Unicode(128)) average_mass = Column(Numeric(12, 6, asdecimal=False), index=True) ex_code_name = Column(Unicode(64), index=True) monoisotopic_mass = Column(Numeric(12, 6, asdecimal=False), index=True) full_name = Column(Unicode(128), index=True) code_name = Column(Unicode(128), index=True) _composition = Column(Unicode(128), index=True) approved = Column(Boolean, index=True) notes = relationship('MiscNotesModifications') specificities = relationship('Specificity') bricks = relationship(ModificationToBrick) _fragments = relationship(Fragment) _alt_names = relationship(AlternativeName, backref=backref('modification')) # Maps the list of AlternativeName instances loaded dynamically from _alt_names # into a list of plain strings, since the AlternativeName type contains no # additional information. alternative_names = association_proxy('_alt_names', 'alt_name') fragments = association_proxy('_fragments', 'composition') @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), username_of_poster=attrib['username_of_poster'], average_mass=float(attrib['avge_mass']), monoisotopic_mass=float(attrib['mono_mass']), ex_code_name=attrib['ex_code_name'], code_name=attrib['code_name'], full_name=attrib['full_name'], approved=bool(int(attrib['approved'])), _composition=attrib['composition'] ) for note in tag: if note.tag == MiscNotesModifications._tag_name: model_note = MiscNotesModifications._from_tag(note, inst.id) if model_note is not None: inst.notes.append(model_note) return inst class MiscNotesModifications(Base): __tablename__ = 'MiscNotesModifications' _tag_name = 'misc_notes' id = Column(Integer, primary_key=True) modification_id = Column(Integer, ForeignKey(Modification.id), index=True) text = Column(UnicodeText) @classmethod def _from_tag(cls, tag, modification_id): if tag.text is None: return return cls(text=tag.text, modification_id=modification_id) class Specificity(Base): __tablename__ = 'Specificity' _tag_name = 'specificity_row' id = Column(Integer, primary_key=True) position_id = Column(Integer, ForeignKey(Position.id), index=True) classification_id = Column(Integer, ForeignKey(Classification.id), index=True) classification = relationship('Classification', uselist=False) # Map through one_letter amino_acid = Column(Unicode(10), ForeignKey(AminoAcid.one_letter), index=True) modification_id = Column(Integer, ForeignKey(Modification.id), index=True) hidden = Column(Boolean, index=True) group = Column(Integer, index=True) neutral_losses = relationship('SpecificityToNeutralLoss', back_populates='specificity') @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), position_id=int(attrib['position_key']), classification_id=int(attrib['classifications_key']), hidden=bool(int(attrib['hidden'])), amino_acid=attrib['one_letter'], modification_id=int(attrib['mod_key']), ) return inst class NeutralLoss(Base): __tablename__ = 'NeutralLoss' _tag_name = 'neutral_losses_row' @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), brick_string=(attrib['brick']), count=int(attrib['num_brick']), specificity_id=int(attrib['spec_key']) ) return inst id = Column(Integer, primary_key=True) brick_string = Column(Unicode(64), index=True) specificity_id = Column(Integer, ForeignKey(Specificity.id), index=True) count = Column(Integer) @has_composition('_composition') class SpecificityToNeutralLoss(Base): __tablename__ = 'SpecificityToNeutralLoss' _tag_name = 'spec2nl_row' @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls( id=int(attrib['record_id']), specificity_id=int(attrib['spec_key']), monoisotopic_mass=float(attrib['nl_mono_mass']), average_mass=float(attrib['nl_avge_mass']), is_required_peptide_neutral_loss=bool(int(attrib['is_req_pep_nl'])), is_peptide_neutral_loss=bool(int(attrib['is_pep_nl'])), is_slave=bool(int(attrib['is_slave_nl'])), _composition=attrib['nl_composition'] ) return inst id = Column(Integer, primary_key=True) specificity_id = Column(Integer, ForeignKey(Specificity.id), index=True) specificity = relationship(Specificity, uselist=False, back_populates='neutral_losses') monoisotopic_mass = Column(Numeric(12, 6, asdecimal=False), index=True) average_mass = Column(Numeric(12, 6, asdecimal=False), index=True) _composition = Column(Unicode(128)) is_slave = Column(Boolean, index=True) is_peptide_neutral_loss = Column(Boolean, index=True) is_required_peptide_neutral_loss = Column(Boolean, index=True) class CrossreferenceSource(Base): __tablename__ = 'CrossreferenceSource' _tag_name = 'xref_sources_row' id = Column(Integer, primary_key=True) source = Column(Unicode(64), index=True) @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls() inst.id = int(attrib['record_id']) inst.source = attrib['xref_source'] return inst class Crossreference(Base): __tablename__ = 'Crossreference' _tag_name = 'xrefs_row' id = Column(Integer, primary_key=True) source_id = Column(Integer, ForeignKey(CrossreferenceSource.id), index=True) source = relationship(CrossreferenceSource, uselist=False) url = Column(Unicode(128)) modification_id = Column(Integer, ForeignKey(Modification.id), index=True) text = Column(UnicodeText) @classmethod def from_tag(cls, tag): attrib = tag.attrib inst = cls() inst.id = int(attrib['record_id']) inst.url = attrib['xref_url'] inst.source_id = int(attrib['xref_source_key']) inst.modification_id = int(attrib['mod_key']) text = [] for node in tag.getchildren(): if node.tag == 'xref_text': if node.text is not None: text.append(node.text) inst.text = '\n'.join(text) return inst def load(doc_path, output_path='sqlite://'): """ Parse the relational table-like XML file provided by http://www.unimod.org/downloads.html and convert each <tag>_row into an equivalent database entry. By default the table will be held in memory. """ tree = preprocess_xml(doc_path) engine = create_engine(output_path) Base.metadata.create_all(engine) session = sessionmaker(bind=engine, autoflush=False)() for model in model_registry: if hasattr(model, '_tag_name') and hasattr(model, 'from_tag'): for tag in tree.iterfind('.//' + model._tag_name): session.add(model.from_tag(tag)) session.commit() return session def session(path='sqlite:///unimod.db'): engine = create_engine(path) Base.metadata.create_all(engine) session = sessionmaker(bind=engine, autoflush=False)() return session class Unimod(object): """ Main class representing the relational Unimod database. Examples -------- If you just wish to get a new copy of the data and store it in a temporary in-memory database, invoking the type without parameters works without issue. >>> new_db = Unimod() If you want to persist a snapshot of the Unimod database to disk and query it from there, or to re-use a previously downloaded database copy, pass a database driver prefixed path: >>> reused_db = Unimod("sqlite:///path/to/unimod.db") If the path did not previously exist, a new copy of Unimod will be downloaded and stored there on the first use, but be immediately available on subsequent uses. """ def __init__(self, path=None): """ Initialize the object from a database file. Parameters ---------- path : str or None, optional If :py:class:`str`, should point to a database. Use a dialect-specific prefix, like ``'sqlite://'``. If :py:const:`None` (default), a relational XML file will be downloaded from default location. """ if path is None: self.path = None self.session = load(_unimod_xml_download_url) else: self.path = path try: self.session = session(path) if self.session.query(Modification).first() is None: raise Exception() except Exception: # Database may not yet exist at that location self.session = load(_unimod_xml_download_url, path) self.session.query(Modification).first() def get(self, identifier, strict=True): """ Get a modification matching `identifier`. Replaces both :py:mod:`by_name` and :py:mod:`by_title` methods in the old class. Parameters ---------- identifier : str strict : bool, optional Defaults to :py:const:`True`. Returns ------- out : Modification """ if isinstance(identifier, int): mod = self.session.get(Modification, identifier) if mod is None: raise KeyError(identifier) return mod elif isinstance(identifier, (str, bytes)): if strict: mod = self.session.query(Modification).filter( (Modification.full_name == identifier) | (Modification.code_name == identifier) | (Modification.ex_code_name == identifier)).first() if mod is None: alt_name = self.session.query(AlternativeName).filter( AlternativeName.alt_name == identifier).first() if alt_name is None: raise KeyError(identifier) mod = alt_name.modification return mod else: qname = '%%%s%%' % identifier mod = self.session.query(Modification).filter( (Modification.full_name.like(qname)) | (Modification.code_name.like(qname)) | (Modification.ex_code_name.like(qname))).first() if mod is None: alt_name = self.session.query(AlternativeName).filter( AlternativeName.alt_name.like(qname)).first() if alt_name is None: raise KeyError(identifier) mod = alt_name.modification return mod by_title = by_name = get __getitem__ = get @property def mods(self): return self.session.query(Modification).all() def __iter__(self): return iter(self.session.query(Modification).yield_per(1000)) def query(self, *args): '''Compose an SQL query using SQLAlchemy's ORM interface. See :mod:`sqlalchemy`'s Session documentation for more details. ''' return self.session.query(*args) def execute(self, *args, **kwargs): '''Execute an SQLAlchemy statement or a SQL string against the database, returning the resulting database cursor. See :mod:`sqlalchemy`'s Session documentation for more details. ''' return self.session.execute(*args, **kwargs)
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/mass/mass.py
pyteomics/mass/mass.py
""" mass - molecular masses and isotope distributions ================================================= Summary ------- This module defines general functions for mass and isotope abundance calculations. For most of the functions, the user can define a given substance in various formats, but all of them would be reduced to the :py:func:`Composition <Composition.__init__>` object describing its chemical composition. Classes ------- :py:func:`Composition <Composition.__init__>` - a class storing chemical composition of a substance. :py:class:`Unimod` - a class representing a Python interface to the `Unimod database <http://unimod.org/>`_ (see :py:mod:`pyteomics.mass.unimod` for a much more powerful alternative). Mass calculations ----------------- :py:func:`calculate_mass` - a general routine for mass / m/z calculation. Can calculate mass for a polypeptide sequence, chemical formula or elemental composition. Supplied with an ion type and charge, the function would calculate m/z. :py:func:`fast_mass` - a less powerful but much faster function for polypeptide mass calculation. :py:func:`fast_mass2` - a version of `fast_mass` that supports *modX* notation. Isotopic abundances ------------------- :py:func:`isotopic_composition_abundance` - calculate the relative abundance of a given isotopic composition. :py:func:`most_probable_isotopic_composition` - finds the most abundant isotopic composition for a molecule defined by a polypeptide sequence, chemical formula or elemental composition. :py:func:`isotopologues` - iterate over possible isotopic conposition of a molecule, possibly filtered by abundance. Data ---- :py:data:`nist_mass` - a dict with exact masses of the most abundant isotopes. :py:data:`std_aa_comp` - a dict with the elemental compositions of the standard twenty amino acid residues, selenocysteine and pyrrolysine. :py:data:`std_ion_comp` - a dict with the relative elemental compositions of the standard peptide fragment ions. :py:data:`std_aa_mass` - a dict with the monoisotopic masses of the standard twenty amino acid residues, selenocysteine and pyrrolysine. ----------------------------------------------------------------------------- """ # Copyright 2012 Anton Goloborodko, Lev Levitsky # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import math from .. import parser from ..auxiliary import PyteomicsError, _nist_mass, BasicComposition, ensure_url_prefix from itertools import chain, product, combinations_with_replacement from collections import defaultdict from urllib.request import urlopen from datetime import datetime import re import operator import warnings from pathlib import Path nist_mass = _nist_mass """ A dict with the exact element masses downloaded from the NIST website: http://www.nist.gov/pml/data/comp.cfm . There are entries for each element containing the masses and relative abundances of several abundant isotopes and a separate entry for undefined isotope with zero key, mass of the most abundant isotope and 1.0 abundance. """ PROTON = 'H+' def _make_isotope_string(element_name, isotope_num): """Form a string label for an isotope.""" if isotope_num == 0: return element_name else: return '{}[{}]'.format(element_name, isotope_num) def _parse_isotope_string(label): """Parse an string with an isotope label and return the element name and the isotope number. >>> _parse_isotope_string('C') ('C', 0) >>> _parse_isotope_string('C[12]') ('C', 12) """ element_name, num = re.match(_isotope_string, label).groups() isotope_num = int(num) if num else 0 return element_name, isotope_num # Initialize std_aa_comp and std_ion_comp before the Composition class # description, fill it later. std_aa_comp = {} """A dictionary with elemental compositions of the twenty standard amino acid residues, selenocysteine, pyrrolysine, and standard H- and -OH terminal groups. """ std_ion_comp = {} """A dict with relative elemental compositions of the standard peptide fragment ions. An elemental composition of a fragment ion is calculated as a difference between the total elemental composition of an ion and the sum of elemental compositions of its constituting amino acid residues. """ _isotope_string = r'^((?:[A-Z][a-z+]*)|e-|e\*)(?:\[(\d+)\])?$' _atom = r'([A-Z][a-z+]*)(?:\[(\d+)\])?([+-]?\d+)?' _formula = r'^({})*$'.format(_atom) def _raise_term_label_exception(what='comp'): raise PyteomicsError("Cannot use a mod label as a terminal group. Provide correct group {0}" " in `aa_{0}`.".format(what)) class Composition(BasicComposition): """ A Composition object stores a chemical composition of a substance. Basically, it is a dict object, with the names of chemical elements as keys and values equal to an integer number of atoms of the corresponding element in a substance. The main improvement over dict is that Composition objects allow adding and subtraction. """ _kw_sources = {'formula', 'sequence', 'parsed_sequence', 'split_sequence', 'composition'} _carrier_spec = r"^(?P<formula>\S+?)(?:(?P<sign>[+-])(?P<charge>\d+)?)?$" def _from_parsed_sequence(self, parsed_sequence, aa_comp): self.clear() comp = defaultdict(int) failflag = False for label in parsed_sequence: if label in aa_comp: for elem, cnt in aa_comp[label].items(): comp[elem] += cnt elif parser.is_term_group(label): slabel = label.strip('-') if slabel in aa_comp: # a modification label used as terminal group. This is prone to errors and not allowed _raise_term_label_exception() elif re.match(_formula, slabel): comp += Composition(formula=slabel) else: failflag = True else: try: mod, aa = parser._split_label(label) for elem, cnt in chain( aa_comp[mod].items(), aa_comp[aa].items()): comp[elem] += cnt except (PyteomicsError, KeyError): failflag = True if failflag: raise PyteomicsError('No information for %s in `aa_comp`' % label) self._from_composition(comp) def _from_split_sequence(self, split_sequence, aa_comp): self.clear() comp = defaultdict(int) for group in split_sequence: i = 0 while i < len(group): for j in range(len(group) + 1, -1, -1): label = ''.join(group[i:j]) if label in aa_comp: for elem, cnt in aa_comp[label].items(): comp[elem] += cnt elif parser.is_term_group(label) and label.strip('-') in aa_comp: _raise_term_label_exception() else: continue i = j break if j == 0: raise PyteomicsError("Invalid group starting from position %d: %s" % (i + 1, group)) self._from_composition(comp) def _from_sequence(self, sequence, aa_comp): parsed_sequence = parser.parse( sequence, labels=aa_comp, show_unmodified_termini=True, allow_unknown_modifications=True) self._from_parsed_sequence(parsed_sequence, aa_comp) def _from_formula(self, formula): if not re.match(_formula, formula): raise PyteomicsError('Invalid formula: ' + formula) for elem, isotope, number in re.findall(_atom, formula): self[_make_isotope_string(elem, int(isotope) if isotope else 0)] += int(number) if number else 1 def _from_composition(self, comp): for isotope_string, num_atoms in comp.items(): element_name, isotope_num = _parse_isotope_string( isotope_string) # Remove explicitly undefined isotopes (e.g. X[0]). self[_make_isotope_string(element_name, isotope_num)] = num_atoms def __init__(self, *args, **kwargs): """ A Composition object stores a chemical composition of a substance. Basically it is a dict object, in which keys are the names of chemical elements and values contain integer numbers of corresponding atoms in a substance. The main improvement over dict is that Composition objects allow addition and subtraction. A Composition object can be initialized with one of the following arguments: formula, sequence, parsed_sequence or split_sequence. If none of these are specified, the constructor will look at the first positional argument and try to build the object from it. Without positional arguments, a Composition will be constructed directly from keyword arguments. If there's an ambiguity, i.e. the argument is both a valid sequence and a formula (such as 'HCN'), it will be treated as a sequence. You need to provide the 'formula' keyword to override this. .. warning:: Be careful when supplying a list with a parsed sequence or a split sequence as a keyword argument. It must be obtained with enabled `show_unmodified_termini` option. When supplying it as a positional argument, the option doesn't matter, because the positional argument is always converted to a sequence prior to any processing. Parameters ---------- formula : str, optional A string with a chemical formula. sequence : str, optional A polypeptide sequence string in modX notation. parsed_sequence : list of str, optional A polypeptide sequence parsed into a list of amino acids. split_sequence : list of tuples of str, optional A polypeptyde sequence parsed into a list of tuples (as returned be :py:func:`pyteomics.parser.parse` with ``split=True``). aa_comp : dict, optional A dict with the elemental composition of the amino acids (the default value is std_aa_comp). ion_comp : dict, optional A dict with the relative elemental compositions of peptide ion fragments (default is :py:data:`std_ion_comp`). ion_type : str, optional If specified, then the polypeptide is considered to be in the form of the corresponding ion. """ defaultdict.__init__(self, int) aa_comp = kwargs.get('aa_comp', std_aa_comp) kw_given = self._kw_sources.intersection(kwargs) if len(kw_given) > 1: raise PyteomicsError( 'Only one of {} can be specified!\n' 'Given: {}'.format(', '.join(self._kw_sources), ', '.join(kw_given))) elif kw_given: kwa = kw_given.pop() if kwa == 'formula': self._from_formula(kwargs['formula']) else: getattr(self, '_from_' + kwa)(kwargs[kwa], aa_comp) # can't build from kwargs elif args: if isinstance(args[0], dict): self._from_composition(args[0]) elif isinstance(args[0], str): try: self._from_sequence(args[0], aa_comp) except PyteomicsError: try: self._from_formula(args[0]) except PyteomicsError: raise PyteomicsError( 'Could not create a Composition object from ' 'string: "{}": not a valid sequence or ' 'formula'.format(args[0])) else: try: self._from_sequence(parser.tostring(args[0], True), aa_comp) except Exception: raise PyteomicsError( 'Could not create a Composition object from `{}`. A Composition object must be ' 'specified by sequence, parsed or split sequence, formula or dict.'.format(args[0])) else: self._from_composition(kwargs) ion_comp = kwargs.get('ion_comp', std_ion_comp) if 'ion_type' in kwargs: self += ion_comp[kwargs['ion_type']] # Charge is not supported in kwargs charge = self['H+'] if 'charge' in kwargs: if charge: raise PyteomicsError('Charge is specified both by the number of protons and `charge` in kwargs') else: warnings.warn( 'charge and charge carrier should be specified when calling mass(). ' 'Support for charge in Composition.__init__ will be removed in a future version.', FutureWarning) self['H+'] = kwargs['charge'] @classmethod def _parse_carrier(cls, spec): """Parse a charge carrier spec. The spec syntax is: <formula>[+-][N] <formula> is a chemical formula as supported by :py:meth:`_from_formula`. [+-] is one of "+" or "-", N is a natural number (1 is assumed if omitted). If both the sign and the charge are missing, the charge of this group can be specified as the number of protons in `<formula>`. Otherwise, having protons in `<formula>` is an error. Returns ------- out : tuple Parsed :py:class:`Composition` and charge of the charge carrier. """ if spec is None: return cls({PROTON: 1}), 1 try: formula, sign, charge = re.match(cls._carrier_spec, spec).groups() except AttributeError: raise PyteomicsError('Invalid charge carrier specification: ' + spec) comp = cls(formula=formula) if sign is not None and PROTON in comp: raise PyteomicsError('Carrier contains protons and also has a charge specified.') if sign is None: # only formula is given if PROTON not in comp: charge = None charge = comp[PROTON] elif charge is None: charge = (-1, 1)[sign == '+'] else: charge = int(charge) * (-1, 1)[sign == '+'] return comp, charge @staticmethod def _mass_to_mz(mass, composition=None, **kwargs): mass_data = kwargs.get('mass_data', nist_mass) absolute = kwargs.get('absolute', True) average = kwargs.get('average', False) # Calculate m/z if required charge = kwargs.get('charge') if charge: # get charge carrier mass and charge charge_carrier = kwargs.get('charge_carrier') ccharge = kwargs.get('carrier_charge') if isinstance(charge_carrier, dict): carrier_comp = Composition(charge_carrier) if ccharge and PROTON in carrier_comp: raise PyteomicsError('`carrier_charge` specified but the charge carrier contains protons.') carrier_charge = ccharge or carrier_comp[PROTON] if not carrier_charge: raise PyteomicsError('Charge carrier charge not specified.') else: carrier_comp, carrier_charge = (composition or Composition)._parse_carrier(charge_carrier) if carrier_charge and ccharge: raise PyteomicsError('Both `carrier_charge` and charge in carrier spec are given.') carrier_charge = ccharge or carrier_charge if not carrier_charge: raise PyteomicsError('Charge of the charge carrier group not specified.') if charge % carrier_charge: raise PyteomicsError('The `charge` must be a multiple of the carrier charge. Given: {} and {}'.format( charge, carrier_charge)) num = charge // carrier_charge carrier_mass = carrier_comp.mass(mass_data=mass_data, average=average, charge=0) if charge and (composition is None or not composition['H+']): mass += carrier_mass * num if charge and composition and composition['H+']: raise PyteomicsError('Composition contains protons and charge is explicitly specified.') if charge is None and composition and composition['H+']: warnings.warn('Charge is not specified, but the Composition contains protons. Assuming m/z calculation.') charge = composition['H+'] if charge: mass /= charge if charge and charge < 0 and absolute: mass = -mass return mass def mass(self, **kwargs): """Calculate the mass or *m/z* of a :py:class:`Composition`. Parameters ---------- average : bool, optional If :py:const:`True` then the average mass is calculated. Note that mass is not averaged for elements with specified isotopes. Default is :py:const:`False`. charge : int, optional If not 0 then m/z is calculated. See also: `charge_carrier`. charge_carrier : str or dict, optional Chemical group carrying the charge. Defaults to a proton, "H+". If string, must be a chemical formula, as supported by the :class:`Composition` `formula` argument, except it must end with a charge formatted as "[+-][N]". If N is omitted, single charge is assumed. Examples of `charge_carrier`: "H+", "NH3+" (here, 3 is part of the composition, and + is a single charge), "Fe+2" ("Fe" is the formula and "+2" is the charge). .. note :: `charge` must be a multiple of `charge_carrier` charge. If dict, it is the atomic composition of the group. In this case, the charge can be passed separately as `carrier_charge` or it will be deduced from the number of protons in `charge_carrier`. carrier_charge : int, optional Charge of the charge carrier group (if `charge_carrier` is specified as a composition dict). .. note :: `charge` must be a multiple of `charge_charge`. mass_data : dict, optional A dict with the masses of the chemical elements (the default value is :py:data:`nist_mass`). ion_comp : dict, optional A dict with the relative elemental compositions of peptide ion fragments (default is :py:data:`std_ion_comp`). ion_type : str, optional If specified, then the polypeptide is considered to be in the form of the corresponding ion. Do not forget to specify the charge state! absolute : bool, optional If :py:const:`True` (default), the m/z value returned will always be positive, even for negatively charged ions. .. note :: `absolute` only applies when `charge` is negative. The mass can still be negative for negative compositions. Returns ------- mass : float """ mass_data = kwargs.get('mass_data', nist_mass) # Calculate mass mass = 0.0 average = kwargs.get('average', False) try: for isotope_string, amount in self.items(): element_name, isotope_num = _parse_isotope_string(isotope_string) # Calculate average mass if required and the isotope number is # not specified. if (not isotope_num) and average: for isotope, data in mass_data[element_name].items(): if isotope: mass += (amount * data[0] * data[1]) else: mass += (amount * mass_data[element_name][isotope_num][0]) except KeyError as e: raise PyteomicsError('No mass information for element: {}'.format(e.args[0])) return self._mass_to_mz(mass, self, **kwargs) std_aa_comp.update({ 'A': Composition({'H': 5, 'C': 3, 'O': 1, 'N': 1}), 'C': Composition({'H': 5, 'C': 3, 'S': 1, 'O': 1, 'N': 1}), 'D': Composition({'H': 5, 'C': 4, 'O': 3, 'N': 1}), 'E': Composition({'H': 7, 'C': 5, 'O': 3, 'N': 1}), 'F': Composition({'H': 9, 'C': 9, 'O': 1, 'N': 1}), 'G': Composition({'H': 3, 'C': 2, 'O': 1, 'N': 1}), 'H': Composition({'H': 7, 'C': 6, 'N': 3, 'O': 1}), 'I': Composition({'H': 11, 'C': 6, 'O': 1, 'N': 1}), 'J': Composition({'H': 11, 'C': 6, 'O': 1, 'N': 1}), 'K': Composition({'H': 12, 'C': 6, 'N': 2, 'O': 1}), 'L': Composition({'H': 11, 'C': 6, 'O': 1, 'N': 1}), 'M': Composition({'H': 9, 'C': 5, 'S': 1, 'O': 1, 'N': 1}), 'N': Composition({'H': 6, 'C': 4, 'O': 2, 'N': 2}), 'P': Composition({'H': 7, 'C': 5, 'O': 1, 'N': 1}), 'Q': Composition({'H': 8, 'C': 5, 'O': 2, 'N': 2}), 'R': Composition({'H': 12, 'C': 6, 'N': 4, 'O': 1}), 'S': Composition({'H': 5, 'C': 3, 'O': 2, 'N': 1}), 'T': Composition({'H': 7, 'C': 4, 'O': 2, 'N': 1}), 'V': Composition({'H': 9, 'C': 5, 'O': 1, 'N': 1}), 'W': Composition({'C': 11, 'H': 10, 'N': 2, 'O': 1}), 'Y': Composition({'H': 9, 'C': 9, 'O': 2, 'N': 1}), 'U': Composition({'H': 5, 'C': 3, 'O': 1, 'N': 1, 'Se': 1}), 'O': Composition({'H': 19, 'C': 12, 'O': 2, 'N': 3}), 'H-': Composition({'H': 1}), '-OH': Composition({'O': 1, 'H': 1}), }) std_ion_comp.update({ 'M': Composition(formula=''), 'M-H2O': Composition(formula='H-2O-1'), 'M-NH3': Composition(formula='N-1H-3'), 'a': Composition(formula='H-2O-1' + 'C-1O-1'), 'a-H2O': Composition(formula='H-2O-1' + 'C-1O-1' + 'H-2O-1'), 'a-NH3': Composition(formula='H-2O-1' + 'C-1O-1' + 'N-1H-3'), 'b': Composition(formula='H-2O-1'), 'b-H2O': Composition(formula='H-2O-1' + 'H-2O-1'), 'b-NH3': Composition(formula='H-2O-1' + 'N-1H-3'), 'c': Composition(formula='H-2O-1' + 'NH3'), 'c-1': Composition(formula='H-2O-1' + 'NH3' + 'H-1'), 'c-dot': Composition(formula='H-2O-1' + 'NH3' + 'H1'), 'c+1': Composition(formula='H-2O-1' + 'NH3' + 'H1'), 'c+2': Composition(formula='H-2O-1' + 'NH3' + 'H2'), 'c-H2O': Composition(formula='H-2O-1' + 'NH3' + 'H-2O-1'), 'c-NH3': Composition(formula='H-2O-1'), 'x': Composition(formula='H-2O-1' + 'CO2'), 'x-H2O': Composition(formula='H-2O-1' + 'CO2' + 'H-2O-1'), 'x-NH3': Composition(formula='H-2O-1' + 'CO2' + 'N-1H-3'), 'y': Composition(formula=''), 'y-H2O': Composition(formula='H-2O-1'), 'y-NH3': Composition(formula='N-1H-3'), 'z': Composition(formula='H-2O-1' + 'ON-1H-1'), 'z-dot': Composition(formula='H-2O-1' + 'ON-1'), 'z+1': Composition(formula='H-2O-1' + 'ON-1H1'), 'z+2': Composition(formula='H-2O-1' + 'ON-1H2'), 'z+3': Composition(formula='H-2O-1' + 'ON-1H3'), 'z-H2O': Composition(formula='H-2O-1' + 'ON-1H-1' + 'H-2O-1'), 'z-NH3': Composition(formula='H-2O-1' + 'ON-1H-1' + 'N-1H-3'), }) def calculate_mass(*args, **kwargs): """Calculates the monoisotopic mass of a polypeptide defined by a sequence string, parsed sequence, chemical formula or Composition object. One or none of the following keyword arguments is required: **formula**, **sequence**, **parsed_sequence**, **split_sequence** or **composition**. All arguments given are used to create a :py:class:`Composition` object, unless an existing one is passed as a keyword argument. Note that if a sequence string is supplied and terminal groups are not explicitly shown, then the mass is calculated for a polypeptide with standard terminal groups (NH2- and -OH). .. warning:: Be careful when supplying a list with a parsed sequence. It must be obtained with enabled `show_unmodified_termini` option. Parameters ---------- formula : str, optional A string with a chemical formula. sequence : str, optional A polypeptide sequence string in modX notation. proforma : str, optional A polypeptide sequeence string in `ProForma notation <https://www.psidev.info/proforma>`_, or a :py:class:`pyteomics.proforma.ProForma` object. parsed_sequence : list of str, optional A polypeptide sequence parsed into a list of amino acids. composition : Composition, optional A Composition object with the elemental composition of a substance. aa_comp : dict, optional A dict with the elemental composition of the amino acids (the default value is std_aa_comp). average : bool, optional If :py:const:`True` then the average mass is calculated. Note that mass is not averaged for elements with specified isotopes. Default is :py:const:`False`. charge : int, optional If not 0 then m/z is calculated: the mass is increased by the corresponding number of proton masses and divided by `charge`. charge_carrier : str or dict, optional Chemical group carrying the charge. Defaults to a proton, "H+". If string, must be a chemical formula, as supported by the :class:`Composition` `formula` argument, except it must end with a charge formatted as "[+-][N]". If N is omitted, single charge is assumed. Examples of `charge_carrier`: "H+", "NH3+" (here, 3 is part of the composition, and + is a single charge), "Fe+2" ("Fe" is the formula and "+2" is the charge). .. note :: `charge` must be a multiple of `charge_carrier` charge. If dict, it is the atomic composition of the group. In this case, the charge can be passed separately as `carrier_charge` or it will be deduced from the number of protons in `charge_carrier`. carrier_charge : int, optional Charge of the charge carrier group (if `charge_carrier` is specified as a composition dict). .. note :: `charge` must be a multiple of `charge_charge`. mass_data : dict, optional A dict with the masses of the chemical elements (the default value is :py:data:`nist_mass`). ion_comp : dict, optional A dict with the relative elemental compositions of peptide ion fragments (default is :py:data:`std_ion_comp`). ion_type : str, optional If specified, then the polypeptide is considered to be in the form of the corresponding ion. Do not forget to specify the charge state! absolute : bool, optional If :py:const:`True` (default), the m/z value returned will always be positive, even for negatively charged ions. .. note :: `absolute` only applies when `charge` is negative. The mass can still be negative for negative compositions. Returns ------- mass : float """ if 'proforma' in kwargs: # do not try to create a composition from .. import proforma proteoform = kwargs.pop('proforma') if isinstance(proteoform, str): proteoform = proforma.ProForma.parse(proteoform) return Composition._mass_to_mz(proteoform.mass, **kwargs) # These parameters must be passed to mass(), not __init__ mass_kw = {} for k in ['charge', 'charge_carrier', 'carrier_charge', 'absolute']: if k in kwargs: mass_kw[k] = kwargs.pop(k) # Make a copy of `composition` keyword argument. if 'composition' in kwargs: composition = Composition(kwargs.pop('composition')) else: composition = Composition(*args, **kwargs) kwargs.update(mass_kw) return composition.mass(**kwargs) def most_probable_isotopic_composition(*args, **kwargs): """Calculate the most probable isotopic composition of a peptide molecule/ion defined by a sequence string, parsed sequence, chemical formula or :py:class:`Composition` object. Note that if a sequence string without terminal groups is supplied then the isotopic composition is calculated for a polypeptide with standard terminal groups (H- and -OH). For each element, only two most abundant isotopes are considered. Parameters ---------- formula : str, optional A string with a chemical formula. sequence : str, optional A polypeptide sequence string in modX notation. parsed_sequence : list of str, optional A polypeptide sequence parsed into a list of amino acids. composition : :py:class:`Composition`, optional A :py:class:`Composition` object with the elemental composition of a substance. elements_with_isotopes : list of str A list of elements to be considered in isotopic distribution (by default, every element has a isotopic distribution). aa_comp : dict, optional A dict with the elemental composition of the amino acids (the default value is :py:data:`std_aa_comp`). mass_data : dict, optional A dict with the masses of chemical elements (the default value is :py:data:`nist_mass`). ion_comp : dict, optional A dict with the relative elemental compositions of peptide ion fragments (default is :py:data:`std_ion_comp`). Returns ------- out: tuple (Composition, float) A tuple with the most probable isotopic composition and its relative abundance. """ composition = (dict(kwargs['composition']) if 'composition' in kwargs else Composition(*args, **kwargs)) # Removing isotopes from the composition. for isotope_string in composition: element_name, isotope_num = _parse_isotope_string(isotope_string) if isotope_num: composition[element_name] += composition.pop(isotope_string) mass_data = kwargs.get('mass_data', nist_mass) elements_with_isotopes = kwargs.get('elements_with_isotopes') isotopic_composition = Composition() for element_name in composition: if not elements_with_isotopes or (element_name in elements_with_isotopes): # Take the two most abundant isotopes. first_iso, second_iso = sorted([(i[0], i[1][1]) for i in mass_data[element_name].items() if i[0]], key=lambda x: -x[1])[:2] # Write the number of isotopes of the most abundant type. first_iso_str = _make_isotope_string(element_name, first_iso[0]) isotopic_composition[first_iso_str] = int(math.ceil( composition[element_name])) * first_iso[1] # Write the number of the second isotopes. second_iso_str = _make_isotope_string(element_name, second_iso[0]) isotopic_composition[second_iso_str] = composition[element_name] - isotopic_composition[first_iso_str] else: isotopic_composition[element_name] = composition[element_name] return (isotopic_composition, isotopic_composition_abundance(composition=isotopic_composition, mass_data=mass_data)) def isotopic_composition_abundance(*args, **kwargs): """Calculate the relative abundance of a given isotopic composition of a molecule. Parameters ---------- formula : str, optional A string with a chemical formula. composition : Composition, optional A Composition object with the isotopic composition of a substance. mass_data : dict, optional A dict with the masses of chemical elements (the default value is :py:data:`nist_mass`). Returns ------- relative_abundance : float The relative abundance of a given isotopic composition. """
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
true
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/mass/__init__.py
pyteomics/mass/__init__.py
from .mass import * try: from . import unimod except ImportError: # SQLAlchemy is not available pass
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/auxiliary/file_helpers.py
pyteomics/auxiliary/file_helpers.py
import sys import codecs import re from functools import wraps from contextlib import contextmanager from collections import OrderedDict, defaultdict import json import multiprocessing as mp import threading import warnings import os from abc import ABCMeta from queue import Empty from collections.abc import Sequence try: import pandas as pd except ImportError: pd = None try: import numpy as np except ImportError: np = None try: import dill except ImportError: dill = None import pickle serializer = pickle else: serializer = dill from .structures import PyteomicsError from .utils import add_metaclass def _get_default_start_method(): supported_methods = mp.get_all_start_methods() if supported_methods[0] == 'fork': for alternative in ['forkserver', 'spawn']: if alternative in supported_methods: return alternative else: raise RuntimeError('Cannot determine a suitable process start method.') return supported_methods[0] ctx = mp.get_context(_get_default_start_method()) def set_start_method(method): global ctx ctx = mp.get_context(method) FileReadingProcess.__bases__ = (ctx.Process,) FileReadingProcess._event_class = ctx.Event def _keepstate(func): """Decorator to help keep the position in open files passed as positional arguments to functions""" @wraps(func) def wrapped(*args, **kwargs): positions = [getattr(arg, 'seek', None) and getattr(arg, 'tell', type(None))() for arg in args] for arg, pos in zip(args, positions): if pos is not None: arg.seek(0) res = func(*args, **kwargs) for arg, pos in zip(args, positions): if pos is not None: try: arg.seek(pos) except ValueError: pass return res return wrapped def _keepstate_method(func): """Decorator for :py:class:`FileReader` methods to help keep the position in the underlying file. """ @wraps(func) def wrapped(self, *args, **kwargs): position = self.tell() self.seek(0) try: return func(self, *args, **kwargs) finally: self.seek(position) return wrapped class _file_obj(object): """Check if `f` is a file name and open the file in `mode`. A context manager.""" def __init__(self, f, mode, encoding=None): self._file_spec = None self.mode = mode if f is None: self.file = {'r': sys.stdin, 'a': sys.stdout, 'w': sys.stdout }[mode[0]] self._file_spec = None elif isinstance(f, (str, bytes)): self.file = codecs.open(f, mode, encoding) self._file_spec = f else: self._file_spec = f self.file = f self.encoding = getattr(self.file, 'encoding', encoding) self.close_file = (self.file is not f) def __enter__(self): return self def __reduce_ex__(self, protocol): return self.__class__, (self._file_spec, self.mode, self.encoding) def __exit__(self, *args, **kwargs): if (not self.close_file) or self._file_spec is None: return # do nothing # clean up exit = getattr(self.file, '__exit__', None) if exit is not None: return exit(*args, **kwargs) else: exit = getattr(self.file, 'close', None) if exit is not None: exit() def __getattr__(self, attr): return getattr(self.file, attr) def __iter__(self): return iter(self.file) class NoOpBaseReader(object): def __init__(self, *args, **kwargs): pass class IteratorContextManager(NoOpBaseReader): def __init__(self, *args, **kwargs): self._func = kwargs.pop('parser_func') self._args = args self._kwargs = kwargs if type(self) is IteratorContextManager: self.reset() super(IteratorContextManager, self).__init__(*args, **kwargs) def __getstate__(self): state = {} state['_iterator_args'] = self._args state['_iterator_kwargs'] = self._kwargs return state def __setstate__(self, state): self._args = state['_iterator_args'] self._kwargs = state['_iterator_kwargs'] def reset(self): """Resets the iterator to its initial state.""" try: self._reader = self._func(*self._args, **self._kwargs) except Exception: self.__exit__(*sys.exc_info()) raise def __enter__(self): return self def __exit__(self, *args, **kwargs): pass def __iter__(self): return self def __next__(self): return next(self._reader) @add_metaclass(ABCMeta) class FileReader(IteratorContextManager): """Abstract class implementing context manager protocol for file readers. """ def __init__(self, source, **kwargs): func = kwargs['parser_func'] super(FileReader, self).__init__(*kwargs['args'], parser_func=func, **kwargs['kwargs']) self._pass_file = kwargs['pass_file'] self._source_init = source self._mode = kwargs['mode'] self._encoding = kwargs.get('encoding') self.reset() def reset(self): if hasattr(self, '_source'): self._source.__exit__(None, None, None) self._source = _file_obj(self._source_init, self._mode, self._encoding) try: if self._pass_file: self._reader = self._func( self._source, *self._args, **self._kwargs) else: self._reader = self._func(*self._args, **self._kwargs) except Exception: # clean up on any error self.__exit__(*sys.exc_info()) raise def __exit__(self, *args, **kwargs): self._source.__exit__(*args, **kwargs) # delegate everything else to file object def __getattr__(self, attr): if attr == '_source': raise AttributeError return getattr(self._source, attr) def remove_bom(bstr): return bstr.replace(codecs.BOM_LE, b'').lstrip(b"\x00") class IndexedReaderMixin(NoOpBaseReader): """Common interface for :py:class:`IndexedTextReader` and :py:class:`IndexedXML`.""" @property def index(self): return self._offset_index @property def default_index(self): return self._offset_index def __len__(self): return len(self._offset_index) def __contains__(self, key): return key in self._offset_index def _item_from_offsets(self, offsets): raise NotImplementedError def get_by_id(self, elem_id): index = self.default_index if index is None: raise PyteomicsError('Access by ID requires building an offset index.') offsets = index[elem_id] return self._item_from_offsets(offsets) def get_by_ids(self, ids): return [self.get_by_id(key) for key in ids] def get_by_index(self, i): try: key = self.default_index.from_index(i, False) except AttributeError: raise PyteomicsError('Positional access requires building an offset index.') return self.get_by_id(key) def get_by_indexes(self, indexes): return [self.get_by_index(i) for i in indexes] def get_by_index_slice(self, s): try: keys = self.default_index.from_slice(s, False) except AttributeError: raise PyteomicsError('Positional access requires building an offset index.') return self.get_by_ids(keys) def get_by_key_slice(self, s): keys = self.default_index.between(s.start, s.stop) if s.step: keys = keys[::s.step] return self.get_by_ids(keys) def __getitem__(self, key): if isinstance(key, (str, bytes)): return self.get_by_id(key) if isinstance(key, int): return self.get_by_index(key) if isinstance(key, Sequence): if not key: return [] if isinstance(key[0], int): return self.get_by_indexes(key) if isinstance(key[0], (str, bytes)): return self.get_by_ids(key) if isinstance(key, slice): for item in (key.start, key.stop, key.step): if item is not None: break if isinstance(item, int): return self.get_by_index_slice(key) if isinstance(item, (str, bytes)): return self.get_by_key_slice(key) if item is None: return list(self) raise PyteomicsError('Unsupported query key: {}'.format(key)) class RTLocator(): def __init__(self, reader): self._reader = reader def _get_scan_by_time(self, time): """Retrieve the scan object for the specified scan time. Parameters ---------- time : float The time to get the nearest scan from Returns ------- tuple: (scan_id, scan, scan_time) """ if not self._reader.default_index: raise PyteomicsError("This method requires the index. Please pass `use_index=True` during initialization") scan_ids = tuple(self._reader.default_index) lo = 0 hi = len(scan_ids) best_match = None best_error = float('inf') best_time = None best_id = None if time == float('inf'): scan = self._reader.get_by_id(scan_ids[-1]) return scan_ids[-1], scan, self._reader._get_time(scan) while hi != lo: mid = (hi + lo) // 2 sid = scan_ids[mid] scan = self._reader.get_by_id(sid) scan_time = self._reader._get_time(scan) err = abs(scan_time - time) if err < best_error: best_error = err best_match = scan best_time = scan_time best_id = sid if scan_time == time: return sid, scan, scan_time elif (hi - lo) == 1: return best_id, best_match, best_time elif scan_time > time: hi = mid else: lo = mid def __getitem__(self, key): if isinstance(key, (int, float)): return self._get_scan_by_time(key)[1] if isinstance(key, Sequence): return [self._get_scan_by_time(t)[1] for t in key] if isinstance(key, slice): if key.start is None: start_index = self._reader.default_index.from_index(0) else: start_index = self._get_scan_by_time(key.start)[0] if key.stop is None: stop_index = self._reader.default_index.from_index(-1) else: stop_index = self._get_scan_by_time(key.stop)[0] return self._reader[start_index:stop_index:key.step] class TimeOrderedIndexedReaderMixin(IndexedReaderMixin): @property def time(self): return self._time def __init__(self, *args, **kwargs): super(TimeOrderedIndexedReaderMixin, self).__init__(*args, **kwargs) self._time = RTLocator(self) @staticmethod def _get_time(scan): raise NotImplementedError class IndexedTextReader(IndexedReaderMixin, FileReader): """Abstract class for text file readers that keep an index of records for random access. This requires reading the file in binary mode.""" delimiter = None label = None block_size = 1000000 label_group = 1 _kw_keys = ['delimiter', 'label', 'block_size', 'label_group'] _warn_if_empty = True def __init__(self, source, **kwargs): # the underlying _file_obj gets None as encoding # to avoid transparent decoding of StreamReader on read() calls encoding = kwargs.pop('encoding', 'utf-8') if 'warn_if_empty' in kwargs: self._warn_if_empty = kwargs.pop('warn_if_empty') super(IndexedTextReader, self).__init__(source, mode='rb', encoding=None, **kwargs) self.encoding = encoding for attr in self._kw_keys: if attr in kwargs: setattr(self, attr, kwargs.pop(attr)) self._offset_index = None if not kwargs.pop('_skip_index', False): self._offset_index = self.build_byte_index() def __getstate__(self): state = super(IndexedTextReader, self).__getstate__() state['offset_index'] = self._offset_index for key in self._kw_keys: state[key] = getattr(self, key) return state def __setstate__(self, state): super(IndexedTextReader, self).__setstate__(state) self._offset_index = state['offset_index'] for key in self._kw_keys: if key in state: setattr(self, key, state[key]) def _chunk_iterator(self): fh = self._source.file delim = remove_bom(self.delimiter.encode(self.encoding)) buff = fh.read(self.block_size) parts = buff.split(delim) started_with_delim = buff.startswith(delim) tail = parts[-1] front = parts[:-1] i = 0 for part in front: i += 1 if part == b"": continue if i == 1: if started_with_delim: yield delim + part else: yield part else: yield delim + part running = True while running: buff = fh.read(self.block_size) if len(buff) == 0: running = False buff = tail else: buff = tail + buff parts = buff.split(delim) tail = parts[-1] front = parts[:-1] for part in front: yield delim + part yield delim + tail def _generate_offsets(self): i = 0 pattern = re.compile(remove_bom(self.label.encode(self.encoding))) for chunk in self._chunk_iterator(): match = pattern.search(chunk) if match: label = match.group(self.label_group) yield i, label.decode(self.encoding), match i += len(chunk) yield i, None, None def build_byte_index(self): index = OffsetIndex() g = self._generate_offsets() last_offset = 0 last_label = None for offset, label, keyline in g: if last_label is not None: index[last_label.strip()] = (last_offset, offset) last_label = label last_offset = offset assert last_label is None if self._warn_if_empty and not index: self._warn_empty() return index def _read_lines_from_offsets(self, start, end): self._source.seek(start) lines = self._source.read(end - start).decode(self.encoding).split('\n') return lines def _warn_empty(self): warnings.warn("{} object has an empty index for file {}. If this is unexpected, consider adjusting `label`.".format( self.__class__.__name__, getattr(self._source, 'name', self._source_init))) class IndexSavingMixin(NoOpBaseReader): """Common interface for :py:class:`IndexSavingXML` and :py:class:`IndexSavingTextReader`.""" _index_class = NotImplemented @property def _byte_offset_filename(self): try: path = self._source.name except AttributeError: return None name, ext = os.path.splitext(path) byte_offset_filename = '{}-{}-byte-offsets.json'.format(name, ext[1:]) return byte_offset_filename def _check_has_byte_offset_file(self): """Check if the file at :attr:`_byte_offset_filename` exists Returns ------- bool Whether the file exists """ path = self._byte_offset_filename if path is None: return False return os.path.exists(path) @classmethod def prebuild_byte_offset_file(cls, path): """Construct a new XML reader, build its byte offset index and write it to file Parameters ---------- path : str The path to the file to parse """ with cls(path) as inst: inst.write_byte_offsets() def write_byte_offsets(self): """Write the byte offsets in :attr:`_offset_index` to the file at :attr:`_byte_offset_filename` """ with open(self._byte_offset_filename, 'w') as f: self._offset_index.save(f) @_keepstate_method def build_byte_index(self): """Build the byte offset index by either reading these offsets from the file at :attr:`_byte_offset_filename`, or falling back to the method used by :class:`IndexedXML` or :class:`IndexedTextReader` if this operation fails due to an IOError """ if not getattr(self, '_use_index', True): return # indexed text readers do not have `_use_index` try: return self._read_byte_offsets() except (IOError, AttributeError, TypeError): return super(IndexSavingMixin, self).build_byte_index() def _read_byte_offsets(self): """Read the byte offset index JSON file at :attr:`_byte_offset_filename` and populate :attr:`_offset_index` """ with open(self._byte_offset_filename, 'r') as f: return self._index_class.load(f) def _file_reader(_mode='r'): # a lot of the code below is borrowed from # http://stackoverflow.com/a/14095585/1258041 def decorator(_func): """A decorator implementing the context manager protocol for functions that read files. Note: 'close' must be in kwargs! Otherwise it won't be respected. """ @wraps(_func) def helper(*args, **kwargs): if args: return FileReader(args[0], mode=_mode, parser_func=_func, pass_file=True, args=args[1:], kwargs=kwargs, encoding=kwargs.pop('encoding', None)) source = kwargs.pop('source', None) return FileReader(source, mode=_mode, parser_func=_func, pass_file=True, args=(), kwargs=kwargs, encoding=kwargs.pop('encoding', None)) return helper return decorator def _file_writer(_mode='w'): def decorator(_func): """A decorator that opens output files for writer functions. """ @wraps(_func) def helper(*args, **kwargs): m = kwargs.pop('file_mode', _mode) enc = kwargs.pop('encoding', None) if len(args) > 1: out_arg = args[1] else: out_arg = kwargs.pop('output', None) with _file_obj(out_arg, m, encoding=enc) as out: if len(args) > 1: call_args = (args[0], out) + args[2:] call_kwargs = kwargs else: call_args = args call_kwargs = dict(output=out, **kwargs) return _func(*call_args, **call_kwargs) return helper return decorator class WritableIndex(object): schema_version = (1, 0, 0) _schema_version_tag_key = "@pyteomics_schema_version" def _serializable_container(self): container = {'index': list(self.items())} return container def save(self, fp): container = self._serializable_container() container[self._schema_version_tag_key] = self.schema_version json.dump(container, fp) @classmethod def load(cls, fp): container = json.load(fp, object_hook=OrderedDict) version_tag = container.get(cls._schema_version_tag_key) if version_tag is None: # The legacy case, no special processing yet inst = cls() inst.schema_version = None return inst version_tag = tuple(version_tag) index = container.get("index") if version_tag < cls.schema_version: # schema upgrade case, no special processing yet inst = cls(index) inst.schema_version = version_tag return inst # no need to upgrade return cls(index) class OffsetIndex(OrderedDict, WritableIndex): '''An augmented OrderedDict that formally wraps getting items by index ''' def __init__(self, *args, **kwargs): super(OffsetIndex, self).__init__(*args, **kwargs) self._index_sequence = None def _invalidate(self): self._index_sequence = None @property def index_sequence(self): """Keeps a cached copy of the :meth:`items` sequence stored as a :class:`tuple` to avoid repeatedly copying the sequence over many method calls. Returns ------- :class:`tuple` """ if self._index_sequence is None: self._index_sequence = tuple(self.items()) return self._index_sequence def __setitem__(self, key, value): self._invalidate() return super(OffsetIndex, self).__setitem__(key, value) def pop(self, *args, **kwargs): self._invalidate() return super(OffsetIndex, self).pop(*args, **kwargs) def find(self, key, *args, **kwargs): return self[key] def from_index(self, index, include_value=False): '''Get an entry by its integer index in the ordered sequence of this mapping. Parameters ---------- index: int The index to retrieve. include_value: bool Whether to return both the key and the value or just the key. Defaults to :const:`False`. Returns ------- object: If ``include_value`` is :const:`True`, a tuple of (key, value) at ``index`` else just the key at ``index``. ''' items = self.index_sequence if include_value: return items[index] else: return items[index][0] def from_slice(self, spec, include_value=False): '''Get a slice along index in the ordered sequence of this mapping. Parameters ---------- spec: slice The slice over the range of indices to retrieve include_value: bool Whether to return both the key and the value or just the key. Defaults to :const:`False` Returns ------- list: If ``include_value`` is :const:`True`, a tuple of (key, value) at ``index`` else just the key at ``index`` for each ``index`` in ``spec`` ''' items = self.index_sequence return [(k, v) if include_value else k for k, v in items[spec]] def between(self, start, stop, include_value=False): keys = list(self) if start is not None: try: start_index = keys.index(start) except ValueError: raise KeyError(start) else: start_index = 0 if stop is not None: try: stop_index = keys.index(stop) except ValueError: raise KeyError(stop) else: stop_index = len(keys) - 1 if start is None or stop is None: pass # won't switch indices else: start_index, stop_index = min(start_index, stop_index), max(start_index, stop_index) if include_value: return [(k, self[k]) for k in keys[start_index:stop_index + 1]] return keys[start_index:stop_index + 1] def __repr__(self): template = "{self.__class__.__name__}({items})" return template.format(self=self, items=list(self.items())) def _integrity_check(self): indices = list(self.values()) sorted_indices = sorted(self.values()) return indices == sorted_indices def sort(self): sorted_pairs = sorted(self.items(), key=lambda x: x[1]) self.clear() self._invalidate() for key, value in sorted_pairs: self[key] = value return self class IndexSavingTextReader(IndexSavingMixin, IndexedTextReader): _index_class = OffsetIndex class HierarchicalOffsetIndex(WritableIndex): _inner_type = OffsetIndex def __init__(self, base=None): self.mapping = defaultdict(self._inner_type) for key, value in (base or {}).items(): self.mapping[key] = self._inner_type(value) def _integrity_check(self): for key, value in self.items(): if not value._integrity_check(): return False return True def sort(self): for key, value in self.items(): value.sort() return self def __getitem__(self, key): return self.mapping[key] def __setitem__(self, key, value): self.mapping[key] = value def __iter__(self): return iter(self.mapping) def __len__(self): return sum(len(group) for key, group in self.items()) def __contains__(self, key): return key in self.mapping def find(self, key, element_type=None): if element_type is None: for element_type in self.keys(): try: return self.find(key, element_type) except KeyError: continue raise KeyError(key) else: return self[element_type][key] def find_no_type(self, key): """Try to find `key` in each of the lower-level indexes, returning both value and the element type that match the key.""" for element_type in self.keys(): try: return self.find(key, element_type), element_type except KeyError: continue raise KeyError(key) def update(self, *args, **kwargs): self.mapping.update(*args, **kwargs) def pop(self, key, default=None): return self.mapping.pop(key, default) def keys(self): return self.mapping.keys() def values(self): return self.mapping.values() def items(self): return self.mapping.items() def _serializable_container(self): encoded_index = {} container = { 'keys': list(self.keys()) } for key, offset in self.items(): encoded_index[key] = list(offset.items()) container['index'] = encoded_index return container def _make_chain(reader, readername, full_output=False): def concat_results(*args, **kwargs): results = [reader(arg, **kwargs) for arg in args] if pd is not None and all(isinstance(a, pd.DataFrame) for a in args): return pd.concat(results) return np.concatenate(results) def _iter(files, kwargs): for f in files: with reader(f, **kwargs) as r: for item in r: yield item def chain(*files, **kwargs): return _iter(files, kwargs) def from_iterable(files, **kwargs): return _iter(files, kwargs) @contextmanager def _chain(*files, **kwargs): yield chain(*files, **kwargs) @contextmanager def _from_iterable(files, **kwargs): yield from_iterable(files, **kwargs) def dispatch(*args, **kwargs): return dispatch_from_iterable(args, **kwargs) def dispatch_from_iterable(args, **kwargs): if kwargs.get('full_output', full_output): return concat_results(*args, **kwargs) return _chain(*args, **kwargs) dispatch.__doc__ = """Chain :py:func:`{0}` for several files. Positional arguments should be file names or file objects. Keyword arguments are passed to the :py:func:`{0}` function. """.format(readername) dispatch_from_iterable.__doc__ = """Chain :py:func:`{0}` for several files. Keyword arguments are passed to the :py:func:`{0}` function. Parameters ---------- files : iterable Iterable of file names or file objects. """.format(readername) dispatch.from_iterable = dispatch_from_iterable return dispatch def _check_use_index(source, use_index, default): try: if use_index is not None: use_index = bool(use_index) # if a file name is given, do not override anything; short-circuit if isinstance(source, (str, bytes)): return use_index if use_index is not None else default # collect information on source if hasattr(source, 'seekable'): seekable = source.seekable() else: seekable = None if hasattr(source, 'mode'): binary = 'b' in source.mode else: binary = None # now check for conflicts if seekable is False: if binary: raise PyteomicsError('Cannot work with non-seekable file in binary mode: {}.'.format(source)) if use_index: warnings.warn('Cannot use indexing as {} is not seekable. Setting `use_index` to False.'.format(source)) use_index = False elif binary is not None: if use_index is not None and binary != use_index: warnings.warn('use_index is {}, but the file mode is {}. ' 'Setting `use_index` to {}'.format(use_index, source.mode, binary)) use_index = binary elif use_index is None: warnings.warn('Could not check mode on {}. Specify `use_index` explicitly to avoid errors.'.format(source)) if use_index is not None: return use_index return default except PyteomicsError: raise except Exception as e: if use_index is None: warnings.warn('Could not check mode on {}. Reason: {!r}. ' 'Specify `use_index` explicitly to avoid errors.'.format(source, e)) return default return use_index class FileReadingProcess(ctx.Process): """Process that does a share of distributed work on entries read from file. Reconstructs a reader object, parses an entries from given indexes, optionally does additional processing, sends results back. The reader class must support the :py:meth:`__getitem__` dict-like lookup. """ _event_class = ctx.Event def __init__(self, reader_spec, target_spec, qin, qout, args_spec, kwargs_spec): super(FileReadingProcess, self).__init__(name='pyteomics-map-worker') self.reader_spec = reader_spec self.target_spec = target_spec self.args_spec = args_spec self.kwargs_spec = kwargs_spec self._qin = qin self._qout = qout # self._in_flag = in_flag self._done_flag = self._event_class() self.daemon = True def run(self): reader = serializer.loads(self.reader_spec) target = serializer.loads(self.target_spec) args = serializer.loads(self.args_spec) kwargs = serializer.loads(self.kwargs_spec) for key in iter(self._qin.get, None): item = reader[key] if target is not None: result = target(item, *args, **kwargs) else: result = item self._qout.put(result) self._done_flag.set() def is_done(self): return self._done_flag.is_set() try: _NPROC = mp.cpu_count() except NotImplementedError: _NPROC = 4 _QUEUE_TIMEOUT = 4 _QUEUE_SIZE = int(1e7) class TaskMappingMixin(NoOpBaseReader): def __init__(self, *args, **kwargs): ''' Instantiate a :py:class:`TaskMappingMixin` object, set default parameters for IPC. Parameters ---------- queue_timeout : float, keyword only, optional The number of seconds to block, waiting for a result before checking to see if all workers are done. queue_size : int, keyword only, optional The length of IPC queue used. processes : int, keyword only, optional Number of worker processes to spawn when :py:meth:`map` is called. This can also be specified in the :py:meth:`map` call. ''' self._queue_size = kwargs.pop('queue_size', _QUEUE_SIZE) self._queue_timeout = kwargs.pop('timeout', _QUEUE_TIMEOUT)
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
true
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/auxiliary/target_decoy.py
pyteomics/auxiliary/target_decoy.py
import re import operator as op import math try: from collections.abc import Container, Sized except ImportError: from collections import Container, Sized from bisect import bisect_right from contextlib import contextmanager try: import pandas as pd except ImportError: pd = None from .structures import PyteomicsError from .file_helpers import _keepstate, IteratorContextManager, _make_chain, ChainBase def _fix_docstring(f, **defaults): for argname, v in defaults.items(): if v is not None: f.__doc__ = re.sub('{} : .*'.format(argname), lambda m: m.group() + ', optional', f.__doc__) def _calculate_qvalues(scores, isdecoy, peps=False, **kwargs): """Actual q-value calculation. Parameters ---------- scores : numpy.ndarray Sorted array of PSMs. isdecoy : numpy.ndarray Sorted array of bools (decoy/target) or floats (PEPs). Returns ------- out : numpy.ndarray Calculated q-values. """ correction = kwargs.pop('correction', 0) ratio = kwargs.pop('ratio', 1) if ratio == 0: raise PyteomicsError('Size ratio cannot be zero!') remove_decoy = kwargs.get('remove_decoy', False) formula = kwargs.pop('formula', (2, 1)[bool(remove_decoy)]) if formula not in {1, 2}: raise PyteomicsError('`formula` must be either 1 or 2') # score_label = kwargs['score_label'] cumsum = isdecoy.cumsum(dtype=np.float64) tfalse = cumsum.copy() ind = np.arange(1., scores.shape[0] + 1., dtype=np.float64) if peps: q = cumsum / ind else: if isinstance(correction, int): if correction == 1: tfalse += 1 elif correction == 2: p = 1. / (1. + ratio) targ = ind - cumsum for i in range(tfalse.size): tfalse[i] = _expectation(cumsum[i], targ[i], p) elif 0 < correction < 1: p = 1. / (1. + ratio) targ = ind - cumsum for i in range(tfalse.size): tfalse[i] = _confidence_value( correction, cumsum[i], targ[i], p) elif correction: raise PyteomicsError('Invalid value for `correction`: {}.'.format(correction)) if formula == 1: q = tfalse / (ind - cumsum) / ratio else: q = (cumsum + tfalse / ratio) / ind # Make sure that q-values are equal for equal scores (conservatively) # and that q-values are monotonic for i in range(scores.size - 1, 0, -1): if (scores[i] == scores[i - 1] or q[i - 1] > q[i]): q[i - 1] = q[i] return q def _qvalues_df(psms, keyf, isdecoy, **kwargs): full = kwargs.get('full_output', False) remove_decoy = kwargs.get('remove_decoy', False) peps = kwargs.get('pep') decoy_or_pep_label = _decoy_or_pep_label(**kwargs) q_label = kwargs.setdefault('q_label', 'q') score_label = kwargs.setdefault('score_label', 'score') if callable(keyf): keyf = psms.apply(keyf, axis=1) if callable(isdecoy): isdecoy = psms.apply(isdecoy, axis=1) if not isinstance(keyf, (str, bytes)): if psms.shape[0]: psms[score_label] = keyf else: psms[score_label] = [] keyf = kwargs['score_label'] if not isinstance(isdecoy, (str, bytes)): if psms.shape[0]: psms[decoy_or_pep_label] = isdecoy else: psms[decoy_or_pep_label] = [] isdecoy = decoy_or_pep_label reverse = kwargs.get('reverse', False) if not full: # create fields early if peps is None: fields = [(keyf, np.float64), (isdecoy, np.bool_), (q_label, np.float64)] else: fields = [(isdecoy, np.float64), (q_label, np.float64)] dtype = np.dtype(fields) psms.sort_values([keyf, isdecoy], ascending=[ not reverse, True], inplace=True) if not psms.shape[0]: if full: psms[q_label] = [] return psms else: return np.array([], dtype=dtype) q = _calculate_qvalues(psms[keyf].values, psms[ isdecoy].values, peps is not None, **kwargs) if remove_decoy: q = q[~psms[isdecoy].values] psms = psms[~psms[isdecoy]].copy() if not full: psms_ = np.empty_like(q, dtype=dtype) if peps is None: psms_[keyf] = psms[keyf] psms_[isdecoy] = psms[isdecoy] psms_[q_label] = q psms = psms_ else: q_label = kwargs['q_label'] psms[q_label] = q return psms def _decoy_or_pep_label(**kwargs): peps = kwargs.get('pep') return kwargs.get('decoy_label', 'is decoy') if peps is None else kwargs.get( 'pep_label', peps if isinstance(peps, (str, bytes)) else 'PEP') def _construct_dtype(*args, **kwargs): full = kwargs.pop('full_output', False) peps = kwargs.get('pep') q_label = kwargs.setdefault('q_label', 'q') score_label = kwargs.setdefault('score_label', 'score') fields = [(score_label, np.float64), (_decoy_or_pep_label(**kwargs), np.bool_ if peps is None else np.float64), (q_label, np.float64)] # if all args are NumPy arrays with common dtype, use it in the output if full: dtypes = {getattr(arg, 'dtype', None) for arg in args} if len(dtypes) == 1 and None not in dtypes: psm_dtype = dtypes.pop() else: psm_dtype = np.object_ dtype = np.dtype(fields + [('psm', psm_dtype)]) else: dtype = np.dtype(fields) return dtype def _make_qvalues(read, is_decoy_prefix, is_decoy_suffix, key): """Create a function that reads PSMs from a file and calculates q-values for each value of `key`.""" def qvalues(*args, **kwargs): """Read `args` and return a NumPy array with scores and q-values. q-values are calculated either using TDA or based on provided values of PEP. Requires :py:mod:`numpy` (and optionally :py:mod:`pandas`). Parameters ---------- positional args : file or str Files to read PSMs from. All positional arguments are treated as files. The rest of the arguments must be named. key : callable / array-like / iterable / str, keyword only If callable, a function used for sorting of PSMs. Should accept exactly one argument (PSM) and return a number (the smaller the better). If array-like, should contain scores for all given PSMs. If string, it is used as a field name (PSMs must be in a record array or a :py:class:`DataFrame`). .. warning:: The default function may not work with your files, because format flavours are diverse. reverse : bool, keyword only, optional If :py:const:`True`, then PSMs are sorted in descending order, i.e. the value of the key function is higher for better PSMs. Default is :py:const:`False`. is_decoy : callable / array-like / iterable / str, keyword only If callable, a function used to determine if the PSM is decoy or not. Should accept exactly one argument (PSM) and return a truthy value if the PSM should be considered decoy. If array-like, should contain boolean values for all given PSMs. If string, it is used as a field name (PSMs must be in a record array or a :py:class:`DataFrame`). .. warning:: The default function may not work with your files, because format flavours are diverse. decoy_prefix : str, optional If the default `is_decoy` function works for you, this parameter specifies which protein name prefix to use to detect decoy matches. If you provide your own `is_decoy`, or if you specify `decoy_suffix`, this parameter has no effect. Default is `"DECOY_"`. decoy_suffix : str, optional If the default `is_decoy` function works for you, this parameter specifies which protein name suffix to use to detect decoy matches. If you provide your own `is_decoy`, this parameter has no effect. Mutually exclusive with `decoy_prefix`. pep : callable / array-like / iterable / str, keyword only, optional If callable, a function used to determine the posterior error probability (PEP). Should accept exactly one argument (PSM) and return a float. If array-like, should contain float values for all given PSMs. If string, it is used as a field name (PSMs must be in a record array or a :py:class:`DataFrame`). .. note:: If this parameter is given, then PEP values will be used to calculate q-values. Otherwise, decoy PSMs will be used instead. This option conflicts with: `is_decoy`, `remove_decoy`, `formula`, `ratio`, `correction`. `key` can still be provided. Without `key`, PSMs will be sorted by PEP. remove_decoy : bool, keyword only, optional Defines whether decoy matches should be removed from the output. Default is :py:const:`False`. .. note:: If set to :py:const:`False`, then by default the decoy PSMs will be taken into account when estimating FDR. Refer to the documentation of :py:func:`fdr` for math; basically, if `remove_decoy` is :py:const:`True`, then formula 1 is used to control output FDR, otherwise it's formula 2. This can be changed by overriding the `formula` argument. formula : int, keyword only, optional Can be either 1 or 2, defines which formula should be used for FDR estimation. Default is 1 if `remove_decoy` is :py:const:`True`, else 2 (see :py:func:`fdr` for definitions). ratio : float, keyword only, optional The size ratio between the decoy and target databases. Default is 1. In theory, the "size" of the database is the number of theoretical peptides eligible for assignment to spectra that are produced by *in silico* cleavage of that database. correction : int or float, keyword only, optional Possible values are 0, 1 and 2, or floating point numbers between 0 and 1. 0 (default): no correction; 1: enable "+1" correction. This accounts for the probability that a false positive scores better than the first excluded decoy PSM; 2: this also corrects that probability for finite size of the sample, so the correction will be slightly less than "+1". If a floating point number is given, then instead of the expectation value for the number of false PSMs, the confidence value is used. The value of `correction` is then interpreted as desired confidence level. E.g., if correction=0.95, then the calculated q-values do not exceed the "real" q-values with 95% probability. See `this paper <http://dx.doi.org/10.1021/acs.jproteome.6b00144>`_ for further explanation. q_label : str, optional Field name for q-value in the output. Default is ``'q'``. score_label : str, optional Field name for score in the output. Default is ``'score'``. decoy_label : str, optional Field name for the decoy flag in the output. Default is ``'is decoy'``. pep_label : str, optional Field name for PEP in the output. Default is ``'PEP'``. full_output : bool, keyword only, optional If :py:const:`True`, then the returned array has PSM objects along with scores and q-values. Default is :py:const:`False`. **kwargs : passed to the :py:func:`chain` function. Returns ------- out : numpy.ndarray A sorted array of records with the following fields: - 'score': :py:class:`np.float64` - 'is decoy': :py:class:`np.bool_` - 'q': :py:class:`np.float64` - 'psm': :py:class:`np.object_` (if `full_output` is :py:const:`True`) """ import numpy as np @_keepstate def get_scores(*args, **kwargs): scores = [] with read(*args, **kwargs) as f: for i, psm in enumerate(f): row = [] for func in (keyf, isdecoy): if callable(func): row.append(func(psm)) elif isinstance(func, (str, bytes)): row.append(psm[func]) else: row.append(func[i]) row.append(None) if full: row.append(psm) scores.append(tuple(row)) return scores peps = kwargs.get('pep', None) if peps is not None: x = {'is_decoy', 'remove_decoy', 'formula', 'ratio', 'correction'}.intersection(kwargs) if x: raise PyteomicsError( "Can't use these parameters with `pep`: " + ', '.join(x)) keyf = kwargs.pop('key', key) reverse = kwargs.get('reverse', False) if keyf is None: keyf = peps if reverse: raise PyteomicsError( 'reverse = True when using PEPs for sorting') if not callable(keyf) and not isinstance(keyf, (Sized, Container)): keyf = np.array(list(keyf)) if peps is None: if 'is_decoy' not in kwargs: if 'decoy_suffix' in kwargs: isdecoy = lambda x: is_decoy_suffix(x, kwargs['decoy_suffix']) elif 'decoy_prefix' in kwargs: isdecoy = lambda x: is_decoy_prefix(x, kwargs['decoy_prefix']) else: isdecoy = is_decoy_prefix else: isdecoy = kwargs['is_decoy'] else: isdecoy = peps if not callable(isdecoy) and not isinstance(isdecoy, (Sized, Container)): isdecoy = np.array(list(isdecoy)) remove_decoy = kwargs.get('remove_decoy', False) decoy_or_pep_label = _decoy_or_pep_label(**kwargs) score_label = kwargs.setdefault('score_label', 'score') q_label = kwargs.setdefault('q_label', 'q') dtype = _construct_dtype(*args, **kwargs) full = kwargs.get('full_output', False) arr_flag = False psms = None # time to check arg type if pd is not None and all(isinstance(arg, pd.DataFrame) for arg in args): psms = pd.concat(args) return _qvalues_df(psms, keyf, isdecoy, **kwargs) if not all(isinstance(arg, np.ndarray) for arg in args): if isinstance(keyf, (str, bytes)): keyf = op.itemgetter(keyf) if isinstance(isdecoy, (str, bytes)): isdecoy = op.itemgetter(isdecoy) if isinstance(peps, (str, bytes)): peps = op.itemgetter(peps) if callable(keyf) or callable(isdecoy): kwargs.pop('full_output', None) scores = np.array(get_scores(*args, **kwargs), dtype=dtype) else: if all(isinstance(arg, np.ndarray) for arg in args): psms = np.concatenate(args) if not isinstance(keyf, (str, bytes)): keyf = np.array(keyf) arr_flag = True if not isinstance(isdecoy, (str, bytes)): isdecoy = np.array(isdecoy) arr_flag = True if arr_flag: scores = np.empty(keyf.size if hasattr( keyf, 'size') else isdecoy.size, dtype=dtype) for func, label in zip((keyf, isdecoy), (score_label, decoy_or_pep_label)): if not isinstance(func, (str, bytes)): scores[label] = func else: scores[label] = psms[func] else: scores = np.empty(psms.shape[0], dtype=dtype) scores[score_label] = psms[keyf] scores[decoy_or_pep_label] = psms[isdecoy] if not scores.size: if full and psms is not None: return psms return scores if not reverse: keys = scores[decoy_or_pep_label], scores[score_label] else: keys = scores[decoy_or_pep_label], -scores[score_label] lexsort = np.lexsort(keys) scores = scores[lexsort] if psms is not None: psms = psms[lexsort] scores[q_label] = _calculate_qvalues(scores[score_label], scores[ decoy_or_pep_label], peps is not None, **kwargs) if remove_decoy: if psms is not None: psms = psms[~scores[decoy_or_pep_label]] scores = scores[~scores[decoy_or_pep_label]] if full and psms is not None: if isinstance(psms, np.ndarray): fields = sorted(psms.dtype.fields, key=lambda x: psms.dtype.fields[x][1]) extra = [] for func, label in zip((keyf, isdecoy), ('score', decoy_or_pep_label)): if not (isinstance(func, (str, bytes)) or label in psms.dtype.fields): extra.append(label) elif label in psms.dtype.fields: psms[label] = scores[label] newdt = [(name, psms.dtype.fields[name][0]) for name in fields] + [ (name, np.float64) for name in extra] + [(q_label, np.float64)] psms_ = psms psms = np.empty_like(psms_, dtype=newdt) for f in fields: psms[f] = psms_[f] for f in extra: psms[f] = scores[f] else: for func, label in zip((keyf, isdecoy), ('score', decoy_or_pep_label)): if not isinstance(label, (str, bytes)): psms[label] = scores[label] psms[q_label] = scores[q_label] return psms return scores _fix_docstring(qvalues, is_decoy=is_decoy_prefix, key=key) if read is _iter: qvalues.__doc__ = qvalues.__doc__.replace("""positional args : file or str Files to read PSMs from. All positional arguments are treated as files.""", """positional args : iterables Iterables to read PSMs from. All positional arguments are chained.""" ).replace("""\n .. warning:: The default function may not work with your files, because format flavours are diverse. decoy_prefix : str, optional If the default `is_decoy` function works for you, this parameter specifies which protein name prefix to use to detect decoy matches. If you provide your own `is_decoy`, or if you specify `decoy_suffix`, this parameter has no effect. Default is `"DECOY_"`. decoy_suffix : str, optional If the default `is_decoy` function works for you, this parameter specifies which protein name suffix to use to detect decoy matches. If you provide your own `is_decoy`, this parameter has no effect. Mutually exclusive with `decoy_prefix`.\n""", "") return qvalues def _make_filter(read, is_decoy_prefix, is_decoy_suffix, key, qvalues): """Create a function that reads PSMs from a file and filters them to the desired FDR level (estimated by TDA), returning the top PSMs sorted by `key`. """ def filter(*args, **kwargs): try: fdr = kwargs.pop('fdr') except KeyError: raise PyteomicsError('Keyword argument required: fdr') args = [list(arg) if not isinstance( arg, (Container, Sized)) else arg for arg in args] peps = kwargs.get('pep') if peps is None: remove_decoy = kwargs.pop('remove_decoy', True) scores = qvalues(*args, remove_decoy=remove_decoy, **kwargs) else: scores = qvalues(*args, **kwargs) keyf = kwargs.pop('key', key) if keyf is None: keyf = peps reverse = kwargs.pop('reverse', False) better = [op.lt, op.gt][bool(reverse)] if 'is_decoy' not in kwargs: if 'decoy_suffix' in kwargs: isdecoy = lambda x: is_decoy_suffix(x, kwargs['decoy_suffix']) elif 'decoy_prefix' in kwargs: isdecoy = lambda x: is_decoy_prefix(x, kwargs['decoy_prefix']) else: isdecoy = is_decoy_prefix else: isdecoy = kwargs['is_decoy'] kwargs.pop('formula', None) decoy_or_pep_label = _decoy_or_pep_label(**kwargs) score_label = kwargs.setdefault('score_label', 'score') q_label = kwargs.get('q_label', 'q') try: i = scores[q_label].searchsorted(fdr, side='right') if isinstance(i, Sized): i = i[0] except AttributeError: i = bisect_right(scores['q'], fdr) if kwargs.pop('full_output', False): if pd is not None and isinstance(scores, pd.DataFrame): return scores.iloc[:i] elif callable(keyf) or callable(isdecoy): return scores['psm'][:i] else: return scores[:i] elif not scores.size: return (_ for _ in ()) if peps is None: label = score_label else: label = decoy_or_pep_label cutoff = scores[label][i] if i < scores.size else ( scores[label][-1] + (1, -1)[bool(reverse)]) def out(): with read(*args, **kwargs) as f: for p, s in zip(f, scores): if peps is not None or not remove_decoy or not s[decoy_or_pep_label]: if better(s[label], cutoff): yield p return out() def _filter(*args, **kwargs): """Read `args` and yield only the PSMs that form a set with estimated false discovery rate (FDR) not exceeding `fdr`. Requires :py:mod:`numpy` and, optionally, :py:mod:`pandas`. Parameters ---------- positional args : file or str Files to read PSMs from. All positional arguments are treated as files. The rest of the arguments must be named. fdr : float, keyword only, 0 <= fdr <= 1 Desired FDR level. key : callable / array-like / iterable / str, keyword only A function used for sorting of PSMs. Should accept exactly one argument (PSM) and return a number (the smaller the better). The default is a function that tries to extract e-value from the PSM. .. warning:: The default function may not work with your files, because format flavours are diverse. reverse : bool, keyword only, optional If :py:const:`True`, then PSMs are sorted in descending order, i.e. the value of the key function is higher for better PSMs. Default is :py:const:`False`. is_decoy : callable / array-like / iterable / str, keyword only A function used to determine if the PSM is decoy or not. Should accept exactly one argument (PSM) and return a truthy value if the PSM should be considered decoy. .. warning:: The default function may not work with your files, because format flavours are diverse. decoy_prefix : str, optional If the default `is_decoy` function works for you, this parameter specifies which protein name prefix to use to detect decoy matches. If you provide your own `is_decoy`, or if you specify `decoy_suffix`, this parameter has no effect. Default is `"DECOY_"`. decoy_suffix : str, optional If the default `is_decoy` function works for you, this parameter specifies which protein name suffix to use to detect decoy matches. If you provide your own `is_decoy`, this parameter has no effect. Mutually exclusive with `decoy_prefix`. remove_decoy : bool, keyword only, optional Defines whether decoy matches should be removed from the output. Default is :py:const:`True`. .. note:: If set to :py:const:`False`, then by default the decoy PSMs will be taken into account when estimating FDR. Refer to the documentation of :py:func:`fdr` for math; basically, if `remove_decoy` is :py:const:`True`, then formula 1 is used to control output FDR, otherwise it's formula 2. This can be changed by overriding the `formula` argument. formula : int, keyword only, optional Can be either 1 or 2, defines which formula should be used for FDR estimation. Default is 1 if `remove_decoy` is :py:const:`True`, else 2 (see :py:func:`fdr` for definitions). ratio : float, keyword only, optional The size ratio between the decoy and target databases. Default is 1. In theory, the "size" of the database is the number of theoretical peptides eligible for assignment to spectra that are produced by *in silico* cleavage of that database. correction : int or float, keyword only, optional Possible values are 0, 1 and 2, or floating point numbers between 0 and 1. 0 (default): no correction; 1: enable "+1" correction. This accounts for the probability that a false positive scores better than the first excluded decoy PSM; 2: this also corrects that probability for finite size of the sample, so the correction will be slightly less than "+1". If a floating point number is given, then instead of the expectation value for the number of false PSMs, the confidence value is used. The value of `correction` is then interpreted as desired confidence level. E.g., if correction=0.95, then the calculated q-values do not exceed the "real" q-values with 95% probability. See `this paper <http://dx.doi.org/10.1021/acs.jproteome.6b00144>`_ for further explanation. pep : callable / array-like / iterable / str, keyword only, optional If callable, a function used to determine the posterior error probability (PEP). Should accept exactly one argument (PSM) and return a float. If array-like, should contain float values for all given PSMs. If string, it is used as a field name (PSMs must be in a record array or a :py:class:`DataFrame`). .. note:: If this parameter is given, then PEP values will be used to calculate q-values. Otherwise, decoy PSMs will be used instead. This option conflicts with: `is_decoy`, `remove_decoy`, `formula`, `ratio`, `correction`. `key` can still be provided. Without `key`, PSMs will be sorted by PEP. full_output : bool, keyword only, optional If :py:const:`True`, then an array of PSM objects is returned. Otherwise, an iterator / context manager object is returned, and the files are parsed twice. This saves some RAM, but is ~2x slower. Default is :py:const:`True`. .. note:: The name for the parameter comes from the fact that it is internally passed to :py:func:`qvalues`. q_label : str, optional Field name for q-value in the output. Default is ``'q'``. score_label : str, optional Field name for score in the output. Default is ``'score'``. decoy_label : str, optional Field name for the decoy flag in the output. Default is ``'is decoy'``. pep_label : str, optional Field name for PEP in the output. Default is ``'PEP'``. **kwargs : passed to the :py:func:`chain` function. Returns ------- out : iterator or :py:class:`numpy.ndarray` or :py:class:`pandas.DataFrame` """ if kwargs.pop('full_output', True): return filter(*args, full_output=True, **kwargs) return IteratorContextManager(*args, parser_func=filter, **kwargs) _fix_docstring(_filter, is_decoy=is_decoy_prefix, key=key) if read is _iter: _filter.__doc__ = _filter.__doc__.replace("""positional args : file or str Files to read PSMs from. All positional arguments are treated as files.""", """positional args : iterables Iterables to read PSMs from. All positional arguments are chained.""").replace( """\n .. warning:: The default function may not work with your files, because format flavours are diverse. decoy_prefix : str, optional If the default `is_decoy` function works for you, this parameter specifies which protein name prefix to use to detect decoy matches. If you provide your own `is_decoy`, or if you specify `decoy_suffix`, this parameter has no effect. Default is `"DECOY_"`. decoy_suffix : str, optional If the default `is_decoy` function works for you, this parameter specifies which protein name suffix to use to detect decoy matches. If you provide your own `is_decoy`, this parameter has no effect. Mutually exclusive with `decoy_prefix`.\n""", "") return _filter @contextmanager def _itercontext(x, **kw): try: yield (row for i, row in x.iterrows()) except AttributeError: yield x # _iter = _make_chain(_itercontext, 'iter') _iter = ChainBase._make_chain(_itercontext) qvalues = _make_qvalues(_iter, None, None, None) filter = _make_filter(_iter, None, None, None, qvalues) filter.chain = _make_chain(filter, 'filter', True) # filter.chain = TableJoiner._make_chain(filter) try: import numpy as np _precalc_fact = np.log([math.factorial(n) for n in range(20)]) def log_factorial(x): x = np.array(x) pf = _precalc_fact m = (x >= pf.size) out = np.empty(x.shape) out[~m] = pf[x[~m].astype(int)] x = x[m] out[m] = x * np.log(x) - x + 0.5 * np.log(2 * np.pi * x) return out def _expectation(d, T, p=0.5): if T is None: return d + 1 T = np.array(T, dtype=int) m = np.arange(T.max() + 1, dtype=int) pi = np.exp(_log_pi(d, m, p)) return ((m * pi).cumsum() / pi.cumsum())[T] def _confidence_value(conf, d, T, p=0.5): if T is not None: T = np.array(T, dtype=int) m = np.arange(T.max() + 1, dtype=int) else: m = np.arange(max(50 * d, 10000)) log_pi = _log_pi(d, m, p) pics = np.exp(log_pi).cumsum() return np.searchsorted(pics, conf * (pics[T] if T is not None else 1)) except ImportError: def log_factorial(n): if n > 10: return n * math.log(n) - n + 0.5 * math.log(2 * math.pi * n) else: return math.log(math.factorial(n)) def _expectation(*a, **k): raise NotImplementedError('NumPy required') def _confidence_value(*a, **k): raise NotImplementedError('NumPy required') def _log_pi_r(d, k, p=0.5): return k * math.log(p) + log_factorial(k + d) - log_factorial(k) - log_factorial(d) def _log_pi(d, k, p=0.5): return _log_pi_r(d, k, p) + (d + 1) * math.log(1 - p) def _count_psms(psms, is_decoy, pep, decoy_prefix, decoy_suffix, is_decoy_prefix, is_decoy_suffix): total, decoy = 0, 0 if pep is not None: is_decoy = pep elif is_decoy is None: if decoy_suffix is not None: is_decoy = lambda x: is_decoy_suffix(x, decoy_suffix) else:
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
true
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/auxiliary/patch.py
pyteomics/auxiliary/patch.py
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/auxiliary/constants.py
pyteomics/auxiliary/constants.py
### Bulky constants for other modules are defined below. _nist_mass = {'Ac': {0: (227, 1.0), 206: (206.0145, 0.0), 207: (207.01195, 0.0), 208: (208.01155, 0.0), 209: (209.00949, 0.0), 210: (210.00944, 0.0), 211: (211.00773, 0.0), 212: (212.00781, 0.0), 213: (213.00661, 0.0), 214: (214.006902, 0.0), 215: (215.006454, 0.0), 216: (216.00872, 0.0), 217: (217.009347, 0.0), 218: (218.01164, 0.0), 219: (219.01242, 0.0), 220: (220.014763, 0.0), 221: (221.01559, 0.0), 222: (222.017844, 0.0), 223: (223.019137, 0.0), 224: (224.021723, 0.0), 225: (225.02323, 0.0), 226: (226.026098, 0.0), 227: (227.0277521, 0.0), 228: (228.0310211, 0.0), 229: (229.03302, 0.0), 230: (230.03629, 0.0), 231: (231.03856, 0.0), 232: (232.04203, 0.0), 233: (233.04455, 0.0), 234: (234.04842, 0.0), 235: (235.05123, 0.0), 236: (236.0553, 0.0)}, 'Ag': {0: (106.905097, 1.0), 93: (92.94978, 0.0), 94: (93.94278, 0.0), 95: (94.93548, 0.0), 96: (95.93068, 0.0), 97: (96.92397, 0.0), 98: (97.92157, 0.0), 99: (98.9176, 0.0), 100: (99.9161, 0.0), 101: (100.9128, 0.0), 102: (101.91169, 0.0), 103: (102.908973, 0.0), 104: (103.908629, 0.0), 105: (104.906529, 0.0), 106: (105.906669, 0.0), 107: (106.905097, 0.51839), 108: (107.905956, 0.0), 109: (108.904752, 0.48161), 110: (109.906107, 0.0), 111: (110.905291, 0.0), 112: (111.907005, 0.0), 113: (112.906567, 0.0), 114: (113.908804, 0.0), 115: (114.90876, 0.0), 116: (115.91136, 0.0), 117: (116.91168, 0.0), 118: (117.91458, 0.0), 119: (118.91567, 0.0), 120: (119.91879, 0.0), 121: (120.91985, 0.0), 122: (121.92353, 0.0), 123: (122.9249, 0.0), 124: (123.92864, 0.0), 125: (124.93043, 0.0), 126: (125.9345, 0.0), 127: (126.93677, 0.0), 128: (127.94117, 0.0), 129: (128.94369, 0.0), 130: (129.95045, 0.0)}, 'Al': {0: (26.98153863, 1.0), 21: (21.02804, 0.0), 22: (22.01952, 0.0), 23: (23.007267, 0.0), 24: (23.9999389, 0.0), 25: (24.9904281, 0.0), 26: (25.98689169, 0.0), 27: (26.98153863, 1.0), 28: (27.98191031, 0.0), 29: (28.980445, 0.0), 30: (29.98296, 0.0), 31: (30.983947, 0.0), 32: (31.98812, 0.0), 33: (32.99084, 0.0), 34: (33.99685, 0.0), 35: (34.99986, 0.0), 36: (36.00621, 0.0), 37: (37.01068, 0.0), 38: (38.01723, 0.0), 39: (39.02297, 0.0), 40: (40.03145, 0.0), 41: (41.03833, 0.0), 42: (42.04689, 0.0)}, 'Am': {0: (243, 1.0), 231: (231.04556, 0.0), 232: (232.04659, 0.0), 233: (233.04635, 0.0), 234: (234.04781, 0.0), 235: (235.04795, 0.0), 236: (236.04958, 0.0), 237: (237.05, 0.0), 238: (238.05198, 0.0), 239: (239.0530245, 0.0), 240: (240.0553, 0.0), 241: (241.0568291, 0.0), 242: (242.0595492, 0.0), 243: (243.0613811, 0.0), 244: (244.0642848, 0.0), 245: (245.066452, 0.0), 246: (246.069775, 0.0), 247: (247.07209, 0.0), 248: (248.07575, 0.0), 249: (249.07848, 0.0)}, 'Ar': {0: (39.9623831225, 1.0), 30: (30.02156, 0.0), 31: (31.01212, 0.0), 32: (31.997638, 0.0), 33: (32.9899257, 0.0), 34: (33.9802712, 0.0), 35: (34.9752576, 0.0), 36: (35.967545106, 0.003365), 37: (36.96677632, 0.0), 38: (37.9627324, 0.000632), 39: (38.964313, 0.0), 40: (39.9623831225, 0.996003), 41: (40.9645006, 0.0), 42: (41.963046, 0.0), 43: (42.965636, 0.0), 44: (43.964924, 0.0), 45: (44.96804, 0.0), 46: (45.96809, 0.0), 47: (46.97219, 0.0), 48: (47.97454, 0.0), 49: (48.98052, 0.0), 50: (49.98443, 0.0), 51: (50.99163, 0.0), 52: (51.99678, 0.0), 53: (53.00494, 0.0)}, 'As': {0: (74.9215965, 1.0), 60: (59.99313, 0.0), 61: (60.98062, 0.0), 62: (61.9732, 0.0), 63: (62.96369, 0.0), 64: (63.95757, 0.0), 65: (64.94956, 0.0), 66: (65.94471, 0.0), 67: (66.93919, 0.0), 68: (67.93677, 0.0), 69: (68.93227, 0.0), 70: (69.93092, 0.0), 71: (70.927112, 0.0), 72: (71.926752, 0.0), 73: (72.923825, 0.0), 74: (73.9239287, 0.0), 75: (74.9215965, 1.0), 76: (75.922394, 0.0), 77: (76.9206473, 0.0), 78: (77.921827, 0.0), 79: (78.920948, 0.0), 80: (79.922534, 0.0), 81: (80.922132, 0.0), 82: (81.9245, 0.0), 83: (82.92498, 0.0), 84: (83.92906, 0.0), 85: (84.93202, 0.0), 86: (85.9365, 0.0), 87: (86.9399, 0.0), 88: (87.94494, 0.0), 89: (88.94939, 0.0), 90: (89.9555, 0.0), 91: (90.96043, 0.0), 92: (91.9668, 0.0)}, 'At': {0: (210, 1.0), 193: (192.99984, 0.0), 194: (193.99873, 0.0), 195: (194.996268, 0.0), 196: (195.99579, 0.0), 197: (196.99319, 0.0), 198: (197.99284, 0.0), 199: (198.99053, 0.0), 200: (199.990351, 0.0), 201: (200.988417, 0.0), 202: (201.98863, 0.0), 203: (202.986942, 0.0), 204: (203.987251, 0.0), 205: (204.986074, 0.0), 206: (205.986667, 0.0), 207: (206.985784, 0.0), 208: (207.98659, 0.0), 209: (208.986173, 0.0), 210: (209.987148, 0.0), 211: (210.9874963, 0.0), 212: (211.990745, 0.0), 213: (212.992937, 0.0), 214: (213.996372, 0.0), 215: (214.998653, 0.0), 216: (216.002423, 0.0), 217: (217.004719, 0.0), 218: (218.008694, 0.0), 219: (219.011162, 0.0), 220: (220.01541, 0.0), 221: (221.01805, 0.0), 222: (222.02233, 0.0), 223: (223.02519, 0.0)}, 'Au': {0: (196.9665687, 1.0), 169: (168.99808, 0.0), 170: (169.99612, 0.0), 171: (170.991879, 0.0), 172: (171.99004, 0.0), 173: (172.986237, 0.0), 174: (173.98476, 0.0), 175: (174.98127, 0.0), 176: (175.9801, 0.0), 177: (176.976865, 0.0), 178: (177.97603, 0.0), 179: (178.973213, 0.0), 180: (179.972521, 0.0), 181: (180.970079, 0.0), 182: (181.969618, 0.0), 183: (182.967593, 0.0), 184: (183.967452, 0.0), 185: (184.965789, 0.0), 186: (185.965953, 0.0), 187: (186.964568, 0.0), 188: (187.965324, 0.0), 189: (188.963948, 0.0), 190: (189.9647, 0.0), 191: (190.9637, 0.0), 192: (191.964813, 0.0), 193: (192.96415, 0.0), 194: (193.965365, 0.0), 195: (194.9650346, 0.0), 196: (195.96657, 0.0), 197: (196.9665687, 1.0), 198: (197.9682423, 0.0), 199: (198.9687652, 0.0), 200: (199.97073, 0.0), 201: (200.971657, 0.0), 202: (201.97381, 0.0), 203: (202.975155, 0.0), 204: (203.97772, 0.0), 205: (204.97987, 0.0)}, 'B': {0: (11.0093054, 1.0), 6: (6.04681, 0.0), 7: (7.02992, 0.0), 8: (8.0246072, 0.0), 9: (9.0133288, 0.0), 10: (10.012937, 0.199), 11: (11.0093054, 0.801), 12: (12.0143521, 0.0), 13: (13.0177802, 0.0), 14: (14.025404, 0.0), 15: (15.031103, 0.0), 16: (16.03981, 0.0), 17: (17.04699, 0.0), 18: (18.05617, 0.0), 19: (19.06373, 0.0)}, 'Ba': {0: (137.9052472, 1.0), 114: (113.95068, 0.0), 115: (114.94737, 0.0), 116: (115.94138, 0.0), 117: (116.9385, 0.0), 118: (117.93304, 0.0), 119: (118.93066, 0.0), 120: (119.92604, 0.0), 121: (120.92405, 0.0), 122: (121.9199, 0.0), 123: (122.918781, 0.0), 124: (123.915094, 0.0), 125: (124.914473, 0.0), 126: (125.91125, 0.0), 127: (126.911094, 0.0), 128: (127.908318, 0.0), 129: (128.908679, 0.0), 130: (129.9063208, 0.00106), 131: (130.906941, 0.0), 132: (131.9050613, 0.00101), 133: (132.9060075, 0.0), 134: (133.9045084, 0.02417), 135: (134.9056886, 0.06592), 136: (135.9045759, 0.07854), 137: (136.9058274, 0.11232), 138: (137.9052472, 0.71698), 139: (138.9088413, 0.0), 140: (139.910605, 0.0), 141: (140.914411, 0.0), 142: (141.916453, 0.0), 143: (142.920627, 0.0), 144: (143.922953, 0.0), 145: (144.92763, 0.0), 146: (145.93022, 0.0), 147: (146.93495, 0.0), 148: (147.93772, 0.0), 149: (148.94258, 0.0), 150: (149.94568, 0.0), 151: (150.95081, 0.0), 152: (151.95427, 0.0), 153: (152.95961, 0.0)}, 'Be': {0: (9.0121822, 1.0), 5: (5.04079, 0.0), 6: (6.019726, 0.0), 7: (7.01692983, 0.0), 8: (8.0053051, 0.0), 9: (9.0121822, 1.0), 10: (10.0135338, 0.0), 11: (11.021658, 0.0), 12: (12.026921, 0.0), 13: (13.03569, 0.0), 14: (14.04289, 0.0), 15: (15.05346, 0.0), 16: (16.06192, 0.0)}, 'Bh': {0: (272, 1.0), 260: (260.12197, 0.0), 261: (261.12166, 0.0), 262: (262.12289, 0.0), 263: (263.12304, 0.0), 264: (264.1246, 0.0), 265: (265.12515, 0.0), 266: (266.12694, 0.0), 267: (267.12765, 0.0), 268: (268.12976, 0.0), 269: (269.13069, 0.0), 270: (270.13362, 0.0), 271: (271.13518, 0.0), 272: (272.13803, 0.0), 273: (273.13962, 0.0), 274: (274.14244, 0.0), 275: (275.14425, 0.0)}, 'Bi': {0: (208.9803987, 1.0), 184: (184.00112, 0.0), 185: (184.99763, 0.0), 186: (185.9966, 0.0), 187: (186.993158, 0.0), 188: (187.99227, 0.0), 189: (188.9892, 0.0), 190: (189.9883, 0.0), 191: (190.985786, 0.0), 192: (191.98546, 0.0), 193: (192.98296, 0.0), 194: (193.98283, 0.0), 195: (194.980651, 0.0), 196: (195.980667, 0.0), 197: (196.978864, 0.0), 198: (197.97921, 0.0), 199: (198.977672, 0.0), 200: (199.978132, 0.0), 201: (200.977009, 0.0), 202: (201.977742, 0.0), 203: (202.976876, 0.0), 204: (203.977813, 0.0), 205: (204.977389, 0.0), 206: (205.978499, 0.0), 207: (206.9784707, 0.0), 208: (207.9797422, 0.0), 209: (208.9803987, 1.0), 210: (209.9841204, 0.0), 211: (210.987269, 0.0), 212: (211.9912857, 0.0), 213: (212.994385, 0.0), 214: (213.998712, 0.0), 215: (215.00177, 0.0), 216: (216.006306, 0.0), 217: (217.00947, 0.0), 218: (218.01432, 0.0)}, 'Bk': {0: (247, 1.0), 235: (235.05658, 0.0), 236: (236.05733, 0.0), 237: (237.057, 0.0), 238: (238.05828, 0.0), 239: (239.05828, 0.0), 240: (240.05976, 0.0), 241: (241.06023, 0.0), 242: (242.06198, 0.0), 243: (243.063008, 0.0), 244: (244.065181, 0.0), 245: (245.0663616, 0.0), 246: (246.06867, 0.0), 247: (247.070307, 0.0), 248: (248.07309, 0.0), 249: (249.0749867, 0.0), 250: (250.078317, 0.0), 251: (251.08076, 0.0), 252: (252.08431, 0.0), 253: (253.08688, 0.0), 254: (254.0906, 0.0)}, 'Br': {0: (78.9183371, 1.0), 67: (66.96479, 0.0), 68: (67.95852, 0.0), 69: (68.95011, 0.0), 70: (69.94479, 0.0), 71: (70.93874, 0.0), 72: (71.93664, 0.0), 73: (72.93169, 0.0), 74: (73.929891, 0.0), 75: (74.925776, 0.0), 76: (75.924541, 0.0), 77: (76.921379, 0.0), 78: (77.921146, 0.0), 79: (78.9183371, 0.5069), 80: (79.9185293, 0.0), 81: (80.9162906, 0.4931), 82: (81.9168041, 0.0), 83: (82.91518, 0.0), 84: (83.916479, 0.0), 85: (84.915608, 0.0), 86: (85.918798, 0.0), 87: (86.920711, 0.0), 88: (87.92407, 0.0), 89: (88.92639, 0.0), 90: (89.93063, 0.0), 91: (90.93397, 0.0), 92: (91.93926, 0.0), 93: (92.94305, 0.0), 94: (93.94868, 0.0), 95: (94.95287, 0.0), 96: (95.95853, 0.0), 97: (96.9628, 0.0)}, 'C': {0: (12.0, 1.0), 8: (8.037675, 0.0), 9: (9.0310367, 0.0), 10: (10.0168532, 0.0), 11: (11.0114336, 0.0), 12: (12.0, 0.9893), 13: (13.0033548378, 0.0107), 14: (14.003241989, 0.0), 15: (15.0105993, 0.0), 16: (16.014701, 0.0), 17: (17.022586, 0.0), 18: (18.02676, 0.0), 19: (19.03481, 0.0), 20: (20.04032, 0.0), 21: (21.04934, 0.0), 22: (22.0572, 0.0)}, 'Ca': {0: (39.96259098, 1.0), 34: (34.01412, 0.0), 35: (35.00494, 0.0), 36: (35.99309, 0.0), 37: (36.98587, 0.0), 38: (37.976318, 0.0), 39: (38.9707197, 0.0), 40: (39.96259098, 0.96941), 41: (40.96227806, 0.0), 42: (41.95861801, 0.00647), 43: (42.9587666, 0.00135), 44: (43.9554818, 0.02086), 45: (44.9561866, 0.0), 46: (45.9536926, 4e-05), 47: (46.954546, 0.0), 48: (47.952534, 0.00187), 49: (48.955674, 0.0), 50: (49.957519, 0.0), 51: (50.9615, 0.0), 52: (51.9651, 0.0), 53: (52.97005, 0.0), 54: (53.97435, 0.0), 55: (54.98055, 0.0), 56: (55.98557, 0.0), 57: (56.99236, 0.0)}, 'Cd': {0: (113.9033585, 1.0), 95: (94.94987, 0.0), 96: (95.93977, 0.0), 97: (96.93494, 0.0), 98: (97.9274, 0.0), 99: (98.92501, 0.0), 100: (99.92029, 0.0), 101: (100.91868, 0.0), 102: (101.91446, 0.0), 103: (102.913419, 0.0), 104: (103.909849, 0.0), 105: (104.909468, 0.0), 106: (105.906459, 0.0125), 107: (106.906618, 0.0), 108: (107.904184, 0.0089), 109: (108.904982, 0.0), 110: (109.9030021, 0.1249), 111: (110.9041781, 0.128), 112: (111.9027578, 0.2413), 113: (112.9044017, 0.1222), 114: (113.9033585, 0.2873), 115: (114.905431, 0.0), 116: (115.904756, 0.0749), 117: (116.907219, 0.0), 118: (117.906915, 0.0), 119: (118.90992, 0.0), 120: (119.90985, 0.0), 121: (120.91298, 0.0), 122: (121.91333, 0.0), 123: (122.917, 0.0), 124: (123.91765, 0.0), 125: (124.92125, 0.0), 126: (125.92235, 0.0), 127: (126.92644, 0.0), 128: (127.92776, 0.0), 129: (128.93215, 0.0), 130: (129.9339, 0.0), 131: (130.94067, 0.0), 132: (131.94555, 0.0)}, 'Ce': {0: (139.9054387, 1.0), 119: (118.95276, 0.0), 120: (119.94664, 0.0), 121: (120.94342, 0.0), 122: (121.93791, 0.0), 123: (122.9354, 0.0), 124: (123.93041, 0.0), 125: (124.92844, 0.0), 126: (125.92397, 0.0), 127: (126.92273, 0.0), 128: (127.91891, 0.0), 129: (128.9181, 0.0), 130: (129.91474, 0.0), 131: (130.91442, 0.0), 132: (131.91146, 0.0), 133: (132.911515, 0.0), 134: (133.908925, 0.0), 135: (134.909151, 0.0), 136: (135.907172, 0.00185), 137: (136.907806, 0.0), 138: (137.905991, 0.00251), 139: (138.906653, 0.0), 140: (139.9054387, 0.8845), 141: (140.9082763, 0.0), 142: (141.909244, 0.11114), 143: (142.912386, 0.0), 144: (143.913647, 0.0), 145: (144.91723, 0.0), 146: (145.91876, 0.0), 147: (146.92267, 0.0), 148: (147.92443, 0.0), 149: (148.9284, 0.0), 150: (149.93041, 0.0), 151: (150.93398, 0.0), 152: (151.93654, 0.0), 153: (152.94058, 0.0), 154: (153.94342, 0.0), 155: (154.94804, 0.0), 156: (155.95126, 0.0), 157: (156.95634, 0.0)}, 'Cf': {0: (251, 1.0), 237: (237.06207, 0.0), 238: (238.06141, 0.0), 239: (239.06242, 0.0), 240: (240.0623, 0.0), 241: (241.06373, 0.0), 242: (242.0637, 0.0), 243: (243.06543, 0.0), 244: (244.066001, 0.0), 245: (245.068049, 0.0), 246: (246.0688053, 0.0), 247: (247.071001, 0.0), 248: (248.072185, 0.0), 249: (249.0748535, 0.0), 250: (250.0764061, 0.0), 251: (251.079587, 0.0), 252: (252.081626, 0.0), 253: (253.085133, 0.0), 254: (254.087323, 0.0), 255: (255.09105, 0.0), 256: (256.09344, 0.0)}, 'Cl': {0: (34.96885268, 1.0), 28: (28.02851, 0.0), 29: (29.01411, 0.0), 30: (30.00477, 0.0), 31: (30.99241, 0.0), 32: (31.98569, 0.0), 33: (32.9774519, 0.0), 34: (33.97376282, 0.0), 35: (34.96885268, 0.7576), 36: (35.96830698, 0.0), 37: (36.96590259, 0.2424), 38: (37.96801043, 0.0), 39: (38.9680082, 0.0), 40: (39.97042, 0.0), 41: (40.97068, 0.0), 42: (41.97325, 0.0), 43: (42.97405, 0.0), 44: (43.97828, 0.0), 45: (44.98029, 0.0), 46: (45.98421, 0.0), 47: (46.98871, 0.0), 48: (47.99495, 0.0), 49: (49.00032, 0.0), 50: (50.00784, 0.0), 51: (51.01449, 0.0)}, 'Cm': {0: (247, 1.0), 233: (233.05077, 0.0), 234: (234.05016, 0.0), 235: (235.05143, 0.0), 236: (236.05141, 0.0), 237: (237.0529, 0.0), 238: (238.05303, 0.0), 239: (239.05496, 0.0), 240: (240.0555295, 0.0), 241: (241.057653, 0.0), 242: (242.0588358, 0.0), 243: (243.0613891, 0.0), 244: (244.0627526, 0.0), 245: (245.0654912, 0.0), 246: (246.0672237, 0.0), 247: (247.070354, 0.0), 248: (248.072349, 0.0), 249: (249.075953, 0.0), 250: (250.078357, 0.0), 251: (251.082285, 0.0), 252: (252.08487, 0.0)}, 'Cn': {0: (285, 1.0), 277: (277.16394, 0.0), 278: (278.16431, 0.0), 279: (279.16655, 0.0), 280: (280.16704, 0.0), 281: (281.16929, 0.0), 282: (282.16977, 0.0), 283: (283.17179, 0.0), 284: (284.17238, 0.0), 285: (285.17411, 0.0)}, 'Co': {0: (58.933195, 1.0), 47: (47.01149, 0.0), 48: (48.00176, 0.0), 49: (48.98972, 0.0), 50: (49.98154, 0.0), 51: (50.97072, 0.0), 52: (51.96359, 0.0), 53: (52.954219, 0.0), 54: (53.9484596, 0.0), 55: (54.941999, 0.0), 56: (55.9398393, 0.0), 57: (56.9362914, 0.0), 58: (57.9357528, 0.0), 59: (58.933195, 1.0), 60: (59.9338171, 0.0), 61: (60.9324758, 0.0), 62: (61.934051, 0.0), 63: (62.933612, 0.0), 64: (63.93581, 0.0), 65: (64.936478, 0.0), 66: (65.93976, 0.0), 67: (66.94089, 0.0), 68: (67.94487, 0.0), 69: (68.94632, 0.0), 70: (69.951, 0.0), 71: (70.9529, 0.0), 72: (71.95781, 0.0), 73: (72.96024, 0.0), 74: (73.96538, 0.0), 75: (74.96833, 0.0)}, 'Cr': {0: (51.9405075, 1.0), 42: (42.00643, 0.0), 43: (42.99771, 0.0), 44: (43.98555, 0.0), 45: (44.97964, 0.0), 46: (45.968359, 0.0), 47: (46.9629, 0.0), 48: (47.954032, 0.0), 49: (48.9513357, 0.0), 50: (49.9460442, 0.04345), 51: (50.9447674, 0.0), 52: (51.9405075, 0.83789), 53: (52.9406494, 0.09501), 54: (53.9388804, 0.02365), 55: (54.9408397, 0.0), 56: (55.9406531, 0.0), 57: (56.943613, 0.0), 58: (57.94435, 0.0), 59: (58.94859, 0.0), 60: (59.95008, 0.0), 61: (60.95472, 0.0), 62: (61.95661, 0.0), 63: (62.96186, 0.0), 64: (63.96441, 0.0), 65: (64.97016, 0.0), 66: (65.97338, 0.0), 67: (66.97955, 0.0)}, 'Cs': {0: (132.905451933, 1.0), 112: (111.9503, 0.0), 113: (112.94449, 0.0), 114: (113.94145, 0.0), 115: (114.93591, 0.0), 116: (115.93337, 0.0), 117: (116.92867, 0.0), 118: (117.926559, 0.0), 119: (118.922377, 0.0), 120: (119.920677, 0.0), 121: (120.917229, 0.0), 122: (121.91611, 0.0), 123: (122.912996, 0.0), 124: (123.912258, 0.0), 125: (124.909728, 0.0), 126: (125.909452, 0.0), 127: (126.907418, 0.0), 128: (127.907749, 0.0), 129: (128.906064, 0.0), 130: (129.906709, 0.0), 131: (130.905464, 0.0), 132: (131.9064343, 0.0), 133: (132.905451933, 1.0), 134: (133.906718475, 0.0), 135: (134.905977, 0.0), 136: (135.9073116, 0.0), 137: (136.9070895, 0.0), 138: (137.911017, 0.0), 139: (138.913364, 0.0), 140: (139.917282, 0.0), 141: (140.920046, 0.0), 142: (141.924299, 0.0), 143: (142.927352, 0.0), 144: (143.932077, 0.0), 145: (144.935526, 0.0), 146: (145.94029, 0.0), 147: (146.94416, 0.0), 148: (147.94922, 0.0), 149: (148.95293, 0.0), 150: (149.95817, 0.0), 151: (150.96219, 0.0)}, 'Cu': {0: (62.9295975, 1.0), 52: (51.99718, 0.0), 53: (52.98555, 0.0), 54: (53.97671, 0.0), 55: (54.96605, 0.0), 56: (55.95856, 0.0), 57: (56.949211, 0.0), 58: (57.9445385, 0.0), 59: (58.939498, 0.0), 60: (59.937365, 0.0), 61: (60.9334578, 0.0), 62: (61.932584, 0.0), 63: (62.9295975, 0.6915), 64: (63.9297642, 0.0), 65: (64.9277895, 0.3085), 66: (65.9288688, 0.0), 67: (66.9277303, 0.0), 68: (67.9296109, 0.0), 69: (68.9294293, 0.0), 70: (69.9323923, 0.0), 71: (70.9326768, 0.0), 72: (71.9358203, 0.0), 73: (72.936675, 0.0), 74: (73.939875, 0.0), 75: (74.9419, 0.0), 76: (75.945275, 0.0), 77: (76.94785, 0.0), 78: (77.95196, 0.0), 79: (78.95456, 0.0), 80: (79.96087, 0.0)}, 'Db': {0: (268, 1.0), 255: (255.1074, 0.0), 256: (256.10813, 0.0), 257: (257.10772, 0.0), 258: (258.10923, 0.0), 259: (259.10961, 0.0), 260: (260.1113, 0.0), 261: (261.11206, 0.0), 262: (262.11408, 0.0), 263: (263.11499, 0.0), 264: (264.1174, 0.0), 265: (265.1186, 0.0), 266: (266.12103, 0.0), 267: (267.12238, 0.0), 268: (268.12545, 0.0), 269: (269.12746, 0.0), 270: (270.13071, 0.0)}, 'Ds': {0: (281, 1.0), 267: (267.14434, 0.0), 268: (268.1438, 0.0), 269: (269.14512, 0.0), 270: (270.14472, 0.0), 271: (271.14606, 0.0), 272: (272.14632, 0.0), 273: (273.14886, 0.0), 274: (274.14949, 0.0), 275: (275.15218, 0.0), 276: (276.15303, 0.0), 277: (277.15565, 0.0), 278: (278.15647, 0.0), 279: (279.15886, 0.0), 280: (280.1598, 0.0), 281: (281.16206, 0.0)}, 'Dy': {0: (163.9291748, 1.0), 138: (137.96249, 0.0), 139: (138.95954, 0.0), 140: (139.95401, 0.0), 141: (140.95135, 0.0), 142: (141.94637, 0.0), 143: (142.94383, 0.0), 144: (143.93925, 0.0), 145: (144.93743, 0.0), 146: (145.932845, 0.0), 147: (146.931092, 0.0), 148: (147.92715, 0.0), 149: (148.927305, 0.0), 150: (149.925585, 0.0), 151: (150.926185, 0.0), 152: (151.924718, 0.0), 153: (152.925765, 0.0), 154: (153.924424, 0.0), 155: (154.925754, 0.0), 156: (155.924283, 0.00056), 157: (156.925466, 0.0), 158: (157.924409, 0.00095), 159: (158.9257392, 0.0), 160: (159.9251975, 0.02329), 161: (160.9269334, 0.18889), 162: (161.9267984, 0.25475), 163: (162.9287312, 0.24896), 164: (163.9291748, 0.2826), 165: (164.9317033, 0.0), 166: (165.9328067, 0.0), 167: (166.93566, 0.0), 168: (167.93713, 0.0), 169: (168.94031, 0.0), 170: (169.94239, 0.0), 171: (170.9462, 0.0), 172: (171.94876, 0.0), 173: (172.953, 0.0)}, 'Er': {0: (165.9302931, 1.0), 143: (142.96634, 0.0), 144: (143.96038, 0.0), 145: (144.95739, 0.0), 146: (145.952, 0.0), 147: (146.94949, 0.0), 148: (147.94455, 0.0), 149: (148.94231, 0.0), 150: (149.937914, 0.0), 151: (150.937449, 0.0), 152: (151.93505, 0.0), 153: (152.935063, 0.0), 154: (153.932783, 0.0), 155: (154.933209, 0.0), 156: (155.931065, 0.0), 157: (156.93192, 0.0), 158: (157.929893, 0.0), 159: (158.930684, 0.0), 160: (159.929083, 0.0), 161: (160.929995, 0.0), 162: (161.928778, 0.00139), 163: (162.930033, 0.0), 164: (163.9292, 0.01601), 165: (164.930726, 0.0), 166: (165.9302931, 0.33503), 167: (166.9320482, 0.22869), 168: (167.9323702, 0.26978), 169: (168.9345904, 0.0), 170: (169.9354643, 0.1491), 171: (170.9380298, 0.0), 172: (171.939356, 0.0), 173: (172.9424, 0.0), 174: (173.94423, 0.0), 175: (174.94777, 0.0), 176: (175.95008, 0.0), 177: (176.95405, 0.0)}, 'Es': {0: (252, 1.0), 240: (240.06892, 0.0), 241: (241.06854, 0.0), 242: (242.06975, 0.0), 243: (243.06955, 0.0), 244: (244.07088, 0.0), 245: (245.07132, 0.0), 246: (246.0729, 0.0), 247: (247.07366, 0.0), 248: (248.07547, 0.0), 249: (249.07641, 0.0), 250: (250.07861, 0.0), 251: (251.079992, 0.0), 252: (252.08298, 0.0), 253: (253.0848247, 0.0), 254: (254.088022, 0.0), 255: (255.090273, 0.0), 256: (256.0936, 0.0), 257: (257.09598, 0.0), 258: (258.09952, 0.0)}, 'Eu': {0: (152.9212303, 1.0), 130: (129.96357, 0.0), 131: (130.95775, 0.0), 132: (131.95437, 0.0), 133: (132.94924, 0.0), 134: (133.94651, 0.0), 135: (134.94182, 0.0), 136: (135.9396, 0.0), 137: (136.93557, 0.0), 138: (137.93371, 0.0), 139: (138.929792, 0.0), 140: (139.92809, 0.0), 141: (140.924931, 0.0), 142: (141.92343, 0.0), 143: (142.920298, 0.0), 144: (143.918817, 0.0), 145: (144.916265, 0.0), 146: (145.917206, 0.0), 147: (146.916746, 0.0), 148: (147.918086, 0.0), 149: (148.917931, 0.0), 150: (149.919702, 0.0), 151: (150.9198502, 0.4781), 152: (151.9217445, 0.0), 153: (152.9212303, 0.5219), 154: (153.9229792, 0.0), 155: (154.9228933, 0.0), 156: (155.924752, 0.0), 157: (156.925424, 0.0), 158: (157.92785, 0.0), 159: (158.929089, 0.0), 160: (159.93197, 0.0), 161: (160.93368, 0.0), 162: (161.93704, 0.0), 163: (162.93921, 0.0), 164: (163.94299, 0.0), 165: (164.94572, 0.0), 166: (165.94997, 0.0), 167: (166.95321, 0.0)}, 'F': {0: (18.99840322, 1.0), 14: (14.03506, 0.0), 15: (15.01801, 0.0), 16: (16.011466, 0.0), 17: (17.00209524, 0.0), 18: (18.000938, 0.0), 19: (18.99840322, 1.0), 20: (19.99998132, 0.0), 21: (20.999949, 0.0), 22: (22.002999, 0.0), 23: (23.00357, 0.0), 24: (24.00812, 0.0), 25: (25.0121, 0.0), 26: (26.01962, 0.0), 27: (27.02676, 0.0), 28: (28.03567, 0.0), 29: (29.04326, 0.0), 30: (30.0525, 0.0), 31: (31.06043, 0.0)}, 'Fe': {0: (55.9349375, 1.0), 45: (45.01458, 0.0), 46: (46.00081, 0.0), 47: (46.99289, 0.0), 48: (47.9805, 0.0), 49: (48.97361, 0.0), 50: (49.96299, 0.0), 51: (50.95682, 0.0), 52: (51.948114, 0.0), 53: (52.9453079, 0.0), 54: (53.9396105, 0.05845), 55: (54.9382934, 0.0), 56: (55.9349375, 0.91754), 57: (56.935394, 0.02119), 58: (57.9332756, 0.00282), 59: (58.9348755, 0.0), 60: (59.934072, 0.0), 61: (60.936745, 0.0), 62: (61.936767, 0.0), 63: (62.94037, 0.0), 64: (63.9412, 0.0), 65: (64.94538, 0.0), 66: (65.94678, 0.0), 67: (66.95095, 0.0), 68: (67.9537, 0.0), 69: (68.95878, 0.0), 70: (69.96146, 0.0), 71: (70.96672, 0.0), 72: (71.96962, 0.0)}, 'Fm': {0: (257, 1.0), 242: (242.07343, 0.0), 243: (243.07435, 0.0), 244: (244.07408, 0.0), 245: (245.07539, 0.0), 246: (246.0753, 0.0), 247: (247.07685, 0.0), 248: (248.077195, 0.0), 249: (249.07903, 0.0), 250: (250.079521, 0.0), 251: (251.081575, 0.0), 252: (252.082467, 0.0), 253: (253.085185, 0.0), 254: (254.0868542, 0.0), 255: (255.089962, 0.0), 256: (256.091773, 0.0), 257: (257.095105, 0.0), 258: (258.09708, 0.0), 259: (259.1006, 0.0), 260: (260.10268, 0.0)}, 'Fr': {0: (223, 1.0), 199: (199.00726, 0.0), 200: (200.00657, 0.0), 201: (201.00386, 0.0), 202: (202.00337, 0.0), 203: (203.000925, 0.0), 204: (204.000653, 0.0), 205: (204.998594, 0.0), 206: (205.99867, 0.0), 207: (206.99695, 0.0), 208: (207.99714, 0.0), 209: (208.995954, 0.0), 210: (209.996408, 0.0), 211: (210.995537, 0.0), 212: (211.996202, 0.0), 213: (212.996189, 0.0), 214: (213.998971, 0.0), 215: (215.000341, 0.0), 216: (216.003198, 0.0), 217: (217.004632, 0.0), 218: (218.007578, 0.0), 219: (219.009252, 0.0), 220: (220.012327, 0.0), 221: (221.014255, 0.0), 222: (222.017552, 0.0), 223: (223.0197359, 0.0), 224: (224.02325, 0.0), 225: (225.02557, 0.0), 226: (226.02939, 0.0), 227: (227.03184, 0.0), 228: (228.03573, 0.0), 229: (229.03845, 0.0), 230: (230.04251, 0.0), 231: (231.04544, 0.0), 232: (232.04977, 0.0)}, 'Ga': {0: (68.9255736, 1.0), 56: (55.99491, 0.0), 57: (56.98293, 0.0), 58: (57.97425, 0.0), 59: (58.96337, 0.0), 60: (59.95706, 0.0), 61: (60.94945, 0.0), 62: (61.944175, 0.0), 63: (62.9392942, 0.0), 64: (63.9368387, 0.0), 65: (64.9327348, 0.0), 66: (65.931589, 0.0), 67: (66.9282017, 0.0), 68: (67.9279801, 0.0), 69: (68.9255736, 0.60108), 70: (69.926022, 0.0), 71: (70.9247013, 0.39892), 72: (71.9263663, 0.0), 73: (72.9251747, 0.0), 74: (73.926946, 0.0), 75: (74.9265002, 0.0), 76: (75.9288276, 0.0), 77: (76.9291543, 0.0), 78: (77.9316082, 0.0), 79: (78.93289, 0.0), 80: (79.93652, 0.0), 81: (80.93775, 0.0), 82: (81.94299, 0.0), 83: (82.94698, 0.0), 84: (83.95265, 0.0), 85: (84.957, 0.0), 86: (85.96312, 0.0)}, 'Gd': {0: (157.9241039, 1.0), 134: (133.95537, 0.0), 135: (134.95257, 0.0), 136: (135.94734, 0.0), 137: (136.94502, 0.0), 138: (137.94012, 0.0), 139: (138.93824, 0.0), 140: (139.93367, 0.0), 141: (140.932126, 0.0), 142: (141.92812, 0.0), 143: (142.92675, 0.0), 144: (143.92296, 0.0), 145: (144.921709, 0.0), 146: (145.918311, 0.0), 147: (146.919094, 0.0), 148: (147.918115, 0.0), 149: (148.919341, 0.0), 150: (149.918659, 0.0), 151: (150.920348, 0.0), 152: (151.919791, 0.002), 153: (152.9217495, 0.0), 154: (153.9208656, 0.0218), 155: (154.922622, 0.148), 156: (155.9221227, 0.2047), 157: (156.9239601, 0.1565), 158: (157.9241039, 0.2484), 159: (158.9263887, 0.0), 160: (159.9270541, 0.2186), 161: (160.9296692, 0.0), 162: (161.930985, 0.0), 163: (162.93399, 0.0), 164: (163.93586, 0.0), 165: (164.93938, 0.0), 166: (165.9416, 0.0), 167: (166.94557, 0.0), 168: (167.94836, 0.0), 169: (168.95287, 0.0)}, 'Ge': {0: (73.9211778, 1.0), 58: (57.99101, 0.0), 59: (58.98175, 0.0), 60: (59.97019, 0.0), 61: (60.96379, 0.0), 62: (61.95465, 0.0), 63: (62.94964, 0.0), 64: (63.94165, 0.0), 65: (64.93944, 0.0), 66: (65.93384, 0.0), 67: (66.932734, 0.0), 68: (67.928094, 0.0), 69: (68.9279645, 0.0), 70: (69.9242474, 0.2038), 71: (70.924951, 0.0), 72: (71.9220758, 0.2731), 73: (72.9234589, 0.0776), 74: (73.9211778, 0.3672), 75: (74.9228589, 0.0), 76: (75.9214026, 0.0783), 77: (76.9235486, 0.0), 78: (77.922853, 0.0), 79: (78.9254, 0.0), 80: (79.92537, 0.0), 81: (80.92882, 0.0), 82: (81.92955, 0.0), 83: (82.93462, 0.0), 84: (83.93747, 0.0), 85: (84.94303, 0.0), 86: (85.94649, 0.0), 87: (86.95251, 0.0), 88: (87.95691, 0.0), 89: (88.96383, 0.0)}, 'H': {0: (1.00782503207, 1.0), 1: (1.00782503207, 0.999885), 2: (2.0141017778, 0.000115), 3: (3.0160492777, 0.0), 4: (4.02781, 0.0), 5: (5.03531, 0.0), 6: (6.04494, 0.0), 7: (7.05275, 0.0)}, 'H+': {0: (1.00727646677, 1.0), 1: (1.00727646677, 1.0)}, 'He': {0: (4.00260325415, 1.0), 3: (3.0160293191, 1.34e-06), 4: (4.00260325415, 0.99999866), 5: (5.01222, 0.0), 6: (6.0188891, 0.0), 7: (7.028021, 0.0), 8: (8.033922, 0.0), 9: (9.04395, 0.0), 10: (10.0524, 0.0)}, 'Hf': {0: (179.94655, 1.0), 153: (152.97069, 0.0), 154: (153.96486, 0.0), 155: (154.96339, 0.0), 156: (155.95936, 0.0), 157: (156.9584, 0.0), 158: (157.954799, 0.0), 159: (158.953995, 0.0), 160: (159.950684, 0.0), 161: (160.950275, 0.0), 162: (161.94721, 0.0), 163: (162.94709, 0.0), 164: (163.944367, 0.0), 165: (164.94457, 0.0), 166: (165.94218, 0.0), 167: (166.9426, 0.0), 168: (167.94057, 0.0), 169: (168.94126, 0.0), 170: (169.93961, 0.0), 171: (170.94049, 0.0), 172: (171.939448, 0.0), 173: (172.94051, 0.0), 174: (173.940046, 0.0016), 175: (174.941509, 0.0), 176: (175.9414086, 0.0526), 177: (176.9432207, 0.186), 178: (177.9436988, 0.2728), 179: (178.9458161, 0.1362), 180: (179.94655, 0.3508), 181: (180.9491012, 0.0), 182: (181.950554, 0.0), 183: (182.95353, 0.0), 184: (183.95545, 0.0), 185: (184.95882, 0.0), 186: (185.96089, 0.0), 187: (186.96459, 0.0), 188: (187.96685, 0.0)}, 'Hg': {0: (201.970643, 1.0), 171: (171.00376, 0.0), 172: (171.99883, 0.0), 173: (172.99724, 0.0), 174: (173.992864, 0.0), 175: (174.99142, 0.0), 176: (175.987355, 0.0), 177: (176.98628, 0.0), 178: (177.982483, 0.0), 179: (178.981834, 0.0), 180: (179.978266, 0.0), 181: (180.977819, 0.0), 182: (181.97469, 0.0), 183: (182.97445, 0.0), 184: (183.971713, 0.0), 185: (184.971899, 0.0), 186: (185.969362, 0.0), 187: (186.969814, 0.0), 188: (187.967577, 0.0), 189: (188.96819, 0.0), 190: (189.966322, 0.0), 191: (190.967157, 0.0), 192: (191.965634, 0.0), 193: (192.966665, 0.0), 194: (193.965439, 0.0), 195: (194.96672, 0.0), 196: (195.965833, 0.0015), 197: (196.967213, 0.0), 198: (197.966769, 0.0997), 199: (198.9682799, 0.1687), 200: (199.968326, 0.231), 201: (200.9703023, 0.1318), 202: (201.970643, 0.2986), 203: (202.9728725, 0.0), 204: (203.9734939, 0.0687), 205: (204.976073, 0.0), 206: (205.977514, 0.0), 207: (206.98259, 0.0), 208: (207.98594, 0.0), 209: (208.99104, 0.0), 210: (209.99451, 0.0)}, 'Ho': {0: (164.9303221, 1.0), 140: (139.96854, 0.0), 141: (140.9631, 0.0), 142: (141.95977, 0.0), 143: (142.95461, 0.0), 144: (143.95148, 0.0), 145: (144.9472, 0.0), 146: (145.94464, 0.0), 147: (146.94006, 0.0), 148: (147.93772, 0.0), 149: (148.933775, 0.0), 150: (149.933496, 0.0), 151: (150.931688, 0.0), 152: (151.931714, 0.0), 153: (152.930199, 0.0), 154: (153.930602, 0.0), 155: (154.929103, 0.0), 156: (155.92984, 0.0), 157: (156.928256, 0.0), 158: (157.928941, 0.0), 159: (158.927712, 0.0), 160: (159.928729, 0.0), 161: (160.927855, 0.0), 162: (161.929096, 0.0), 163: (162.9287339, 0.0), 164: (163.9302335, 0.0), 165: (164.9303221, 1.0), 166: (165.9322842, 0.0), 167: (166.933133, 0.0), 168: (167.93552, 0.0), 169: (168.936872, 0.0), 170: (169.93962, 0.0), 171: (170.94147, 0.0), 172: (171.94482, 0.0), 173: (172.94729, 0.0), 174: (173.95115, 0.0), 175: (174.95405, 0.0)}, 'Hs': {0: (270, 1.0), 263: (263.12856, 0.0), 264: (264.12839, 0.0), 265: (265.13009, 0.0), 266: (266.1301, 0.0), 267: (267.13179, 0.0), 268: (268.13216, 0.0), 269: (269.13406, 0.0), 270: (270.13465, 0.0), 271: (271.13766, 0.0), 272: (272.13905, 0.0), 273: (273.14199, 0.0), 274: (274.14313, 0.0), 275: (275.14595, 0.0), 276: (276.14721, 0.0), 277: (277.14984, 0.0)}, 'I': {0: (126.904473, 1.0), 108: (107.94348, 0.0), 109: (108.93815, 0.0), 110: (109.93524, 0.0), 111: (110.93028, 0.0), 112: (111.92797, 0.0), 113: (112.92364, 0.0), 114: (113.92185, 0.0), 115: (114.91805, 0.0), 116: (115.91681, 0.0), 117: (116.91365, 0.0), 118: (117.913074, 0.0), 119: (118.91007, 0.0), 120: (119.910048, 0.0), 121: (120.907367, 0.0), 122: (121.907589, 0.0), 123: (122.905589, 0.0), 124: (123.9062099, 0.0), 125: (124.9046302, 0.0), 126: (125.905624, 0.0), 127: (126.904473, 1.0), 128: (127.905809, 0.0), 129: (128.904988, 0.0), 130: (129.906674, 0.0), 131: (130.9061246, 0.0), 132: (131.907997, 0.0), 133: (132.907797, 0.0), 134: (133.909744, 0.0), 135: (134.910048, 0.0),
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
true
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/auxiliary/utils.py
pyteomics/auxiliary/utils.py
import base64 import zlib from functools import wraps from collections import namedtuple from urllib.parse import urlparse import pathlib try: import numpy as np except ImportError: np = None try: import pynumpress except ImportError: pynumpress = None from .structures import PyteomicsError def print_tree(d, indent_str=' -> ', indent_count=1): """Read a nested dict (with strings as keys) and print its structure. """ def structure(d): out = {} for k, v in d.items(): if isinstance(v, dict): out[k] = structure(v) elif isinstance(v, list) and v and isinstance(v[0], dict): out['{} [list]'.format(k)] = structure(v[0]) else: out[k] = None return out def _print(d, level=0): for k, v in d.items(): print('{}{}'.format(indent_str * indent_count * level, k)) if v is not None: _print(v, level + 1) _print(structure(d)) def memoize(maxsize=1000): """Make a memoization decorator. A negative value of `maxsize` means no size limit.""" def deco(f): """Memoization decorator. Items of `kwargs` must be hashable.""" memo = {} @wraps(f) def func(*args, **kwargs): key = (args, frozenset(kwargs.items())) if key not in memo: if len(memo) == maxsize: memo.popitem() memo[key] = f(*args, **kwargs) return memo[key] return func return deco def _decode_base64_data_array(source, dtype, is_compressed): """Read a base64-encoded binary array. Parameters ---------- source : str A binary array encoded with base64. dtype : dtype The type of the array in numpy dtype notation. is_compressed : bool If True then the array will be decompressed with zlib. Returns ------- out : numpy.array """ decoded_source = base64.b64decode(source.encode('ascii')) if is_compressed: decoded_source = zlib.decompress(decoded_source) output = np.frombuffer(bytearray(decoded_source), dtype=dtype) return output _default_compression_map = { 'no compression': lambda x: x, 'zlib compression': zlib.decompress, } def _pynumpressDecompress(decoder): def decode(data): return decoder(np.frombuffer(data, dtype=np.uint8)) return decode def _zlibNumpress(decoder): def decode(data): return decoder(np.frombuffer(zlib.decompress(data), dtype=np.uint8)) return decode if pynumpress: _default_compression_map.update( { 'MS-Numpress short logged float compression': _pynumpressDecompress(pynumpress.decode_slof), 'MS-Numpress positive integer compression': _pynumpressDecompress(pynumpress.decode_pic), 'MS-Numpress linear prediction compression': _pynumpressDecompress(pynumpress.decode_linear), 'MS-Numpress short logged float compression followed by zlib compression': _zlibNumpress(pynumpress.decode_slof), 'MS-Numpress positive integer compression followed by zlib compression': _zlibNumpress(pynumpress.decode_pic), 'MS-Numpress linear prediction compression followed by zlib compression': _zlibNumpress(pynumpress.decode_linear), }) class ArrayConversionMixin(object): _dtype_dict = {} _array_keys = ['m/z array', 'intensity array'] def __init__(self, *args, **kwargs): self._dtype_dict = {None: None} dtype = kwargs.pop('dtype', None) if isinstance(dtype, dict): self._dtype_dict.update(dtype) elif dtype: self._dtype_dict = {k: dtype for k in self._array_keys} self._dtype_dict[None] = dtype self._convert_arrays = kwargs.pop('convert_arrays', 1) if self._convert_arrays and np is None: raise PyteomicsError('numpy is required for array conversion') super(ArrayConversionMixin, self).__init__(*args, **kwargs) def __getstate__(self): state = super(ArrayConversionMixin, self).__getstate__() state['_dtype_dict'] = self._dtype_dict state['_convert_arrays'] = self._convert_arrays state['_array_keys'] = self._array_keys return state def __setstate__(self, state): super(ArrayConversionMixin, self).__setstate__(state) self._dtype_dict = state['_dtype_dict'] self._convert_arrays = state['_convert_arrays'] self._array_keys = state['_array_keys'] def _build_array(self, k, data): dtype = self._dtype_dict.get(k) return np.array(data, dtype=dtype) def _convert_array(self, k, array): dtype = self._dtype_dict.get(k) if dtype is not None: return array.astype(dtype) return array def _build_all_arrays(self, info): if self._convert_arrays: for k in self._array_keys: if k in info: info[k] = self._build_array(k, info[k]) class MaskedArrayConversionMixin(ArrayConversionMixin): _masked_array_keys = ['charge array'] _mask_value = 0 def __init__(self, *args, **kwargs): self._convert_arrays = kwargs.pop('convert_arrays', 2) kwargs['convert_arrays'] = self._convert_arrays super(MaskedArrayConversionMixin, self).__init__(*args, **kwargs) def __getstate__(self): state = super(MaskedArrayConversionMixin, self).__getstate__() state['_masked_array_keys'] = self._masked_array_keys state['_mask_value'] = self._mask_value return state def __setstate__(self, state): super(MaskedArrayConversionMixin, self).__setstate__(state) self._masked_array_keys = state['_masked_array_keys'] self._mask_value = state['_mask_value'] def _build_masked_array(self, k, data): array = self._build_array(k, data) return self._convert_masked_array(k, array) def _convert_masked_array(self, k, array): return np.ma.masked_equal(array, self._mask_value) def _ensure_masked_array(self, k, data): if isinstance(data, np.ndarray): return self._convert_masked_array(k, data) return self._build_masked_array(self, k, data) def _build_all_arrays(self, info): super(MaskedArrayConversionMixin, self)._build_all_arrays(info) if self._convert_arrays == 2: for k in self._masked_array_keys: if k in info: info[k] = self._ensure_masked_array(k, info[k]) if np is not None: class BinaryDataArrayTransformer(object): """A base class that provides methods for reading base64-encoded binary arrays. Attributes ---------- compression_type_map : dict Maps compressor type name to decompression function """ compression_type_map = _default_compression_map class binary_array_record(namedtuple( "binary_array_record", ("data", "compression", "dtype", "source", "key"))): """Hold all of the information about a base64 encoded array needed to decode the array. """ def decode(self): """Decode :attr:`data` into a numerical array Returns ------- np.ndarray """ return self.source._decode_record(self) def _make_record(self, data, compression, dtype, key=None): return self.binary_array_record(data, compression, dtype, self, key) def _decode_record(self, record): array = self.decode_data_array( record.data, record.compression, record.dtype) return self._finalize_record_conversion(array, record) def _finalize_record_conversion(self, array, record): return array def _base64_decode(self, source): decoded_source = base64.b64decode(source.encode('ascii')) return decoded_source def _decompress(self, source, compression_type=None): if compression_type is None: return source decompressor = self.compression_type_map.get(compression_type) decompressed_source = decompressor(source) return decompressed_source def _transform_buffer(self, binary, dtype): if isinstance(binary, np.ndarray): return binary.astype(dtype, copy=False) return np.frombuffer(binary, dtype=dtype) def decode_data_array(self, source, compression_type=None, dtype=np.float64): """Decode a base64-encoded, compressed bytestring into a numerical array. Parameters ---------- source : bytes A base64 string encoding a potentially compressed numerical array. compression_type : str, optional The name of the compression method used before encoding the array into base64. dtype : type, optional The data type to use to decode the binary array from the decompressed bytes. Returns ------- np.ndarray """ binary = self._base64_decode(source) binary = self._decompress(binary, compression_type) if isinstance(binary, bytes): binary = bytearray(binary) array = self._transform_buffer(binary, dtype) return array class BinaryArrayConversionMixin(ArrayConversionMixin, BinaryDataArrayTransformer): def _finalize_record_conversion(self, array, record): key = record.key return self._convert_array(key, array) else: BinaryDataArrayTransformer = None BinaryArrayConversionMixin = None def add_metaclass(metaclass): """Class decorator for creating a class with a metaclass.""" def wrapper(cls): orig_vars = cls.__dict__.copy() slots = orig_vars.get('__slots__') if slots is not None: if isinstance(slots, str): slots = [slots] for slots_var in slots: orig_vars.pop(slots_var) orig_vars.pop('__dict__', None) orig_vars.pop('__weakref__', None) if hasattr(cls, '__qualname__'): orig_vars['__qualname__'] = cls.__qualname__ return metaclass(cls.__name__, cls.__bases__, orig_vars) return wrapper _supported_url_schemes = {'file', 'ftp', 'http', 'https'} def ensure_url_prefix(path_or_url): """ Ensure that a string path has a URL schema prefix, add "file://" if it is missing. Only expects a subset of URL schemes that can be urlopen-ed for reading. Parameters ---------- path_or_url : str or Path Returns ------- out : str """ if isinstance(path_or_url, pathlib.Path): path = path_or_url else: parsed = urlparse(path_or_url) if parsed.scheme in _supported_url_schemes: return path_or_url if parsed.params or parsed.query or parsed.fragment: return path_or_url # for all other cases assume it's a local path path = pathlib.Path(path_or_url) return path.absolute().as_uri()
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/auxiliary/__init__.py
pyteomics/auxiliary/__init__.py
from .structures import ( PyteomicsError, Charge, ChargeList, _parse_charge, _parse_ion, BasicComposition, unitfloat, unitint, unitstr, cvstr, cvquery) from .constants import _nist_mass from .file_helpers import ( _file_obj, _keepstate, _keepstate_method, IteratorContextManager, FileReader, IndexedTextReader, IndexedReaderMixin, TimeOrderedIndexedReaderMixin, IndexSavingMixin, OffsetIndex, HierarchicalOffsetIndex, IndexSavingTextReader, _file_reader, _file_writer, _make_chain, _check_use_index, FileReadingProcess, TaskMappingMixin, set_start_method, serializer, ChainBase, TableJoiner) from .math import ( linear_regression, linear_regression_perpendicular, linear_regression_vertical) from .target_decoy import ( _calculate_qvalues, _qvalues_df, _decoy_or_pep_label, _construct_dtype, _make_qvalues, _make_filter, _itercontext, _iter, qvalues, filter, log_factorial, _expectation, _confidence_value, _log_pi_r, _log_pi, _make_fdr, fdr, sigma_T, sigma_fdr) from .utils import ( print_tree, memoize, BinaryDataArrayTransformer, ArrayConversionMixin, BinaryArrayConversionMixin, MaskedArrayConversionMixin, _decode_base64_data_array, ensure_url_prefix)
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/auxiliary/psims_util.py
pyteomics/auxiliary/psims_util.py
from functools import partial try: from psims.controlled_vocabulary.controlled_vocabulary import (load_psimod, load_xlmod, load_gno, obo_cache, load_unimod, load_psims) from psims.controlled_vocabulary.relationship import HasValueTypeRelationship _has_psims = True except ImportError: def _needs_psims(name): raise ImportError("Loading %s requires the `psims` library. To access it, please install `psims`" % name) load_psimod = partial(_needs_psims, 'PSIMOD') load_xlmod = partial(_needs_psims, 'XLMOD') load_gno = partial(_needs_psims, 'GNO') load_unimod = partial(_needs_psims, 'UNIMOD') load_psims = partial(_needs_psims, 'PSI-MS') obo_cache = None HasValueTypeRelationship = None _has_psims = False
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/auxiliary/structures.py
pyteomics/auxiliary/structures.py
import re from collections import defaultdict, Counter import warnings _UNIT_CV_INTERN_TABLE = dict() def clear_unit_cv_table(): """Clear the module-level unit name and controlled vocabulary accession table. """ _UNIT_CV_INTERN_TABLE.clear() def _intern_unit_or_cv(unit_or_cv): """Intern `unit_or_cv` in :const:`~._UNIT_CV_INTERN_TABLE`, potentially keeping a reference to the object stored for the duration of the program. Parameters ---------- unit_or_cv : object The value to intern Returns ------- object: The object which `unit_or_cv` hash-equals in :const:`~._UNIT_CV_INTERN_TABLE`. """ if unit_or_cv is None: return None try: return _UNIT_CV_INTERN_TABLE[unit_or_cv] except KeyError: _UNIT_CV_INTERN_TABLE[unit_or_cv] = unit_or_cv return _UNIT_CV_INTERN_TABLE[unit_or_cv] class PyteomicsError(Exception): """Exception raised for errors in Pyteomics library. Attributes ---------- message : str Error message. """ def __init__(self, msg, *values): self.message = msg self.values = values def __str__(self): if not self.values: return "Pyteomics error, message: %s" % (repr(self.message),) else: return "Pyteomics error, message: %s %r" % (repr(self.message), self.values) class Charge(int): """A subclass of :py:class:`int`. Can be constructed from strings in "N+" or "N-" format, and the string representation of a :py:class:`Charge` is also in that format. """ def __new__(cls, *args, **kwargs): exc = None try: return super(Charge, cls).__new__(cls, *args) except ValueError as e: exc = e if isinstance(args[0], (str, bytes)): match = re.match(r'^(\d+)(\+|-)$', args[0]) if match: num, sign = match.groups() try: return super(Charge, cls).__new__(cls, sign + num, *args[1:], **kwargs) except ValueError as e: exc = e else: # try to parse as a float try: float_val = float(args[0]) if not float_val.is_integer(): raise PyteomicsError( f"Cannot convert non-integer float string to Charge: {args[0]}") int_val = int(float_val) return super(Charge, cls).__new__(cls, int_val, *args[1:], **kwargs) except ValueError as e: exc = e raise PyteomicsError(f"Cannot convert {args[0]!r} to Charge") from exc def __str__(self): return str(abs(self)) + '+-'[self < 0] class Ion(str): """Represents an Ion, right now just a subclass of String. """ _pattern = r'([abcxyz]\d+(\-H2O|\-NH3)?)([\+|-]\d+)' # "y2-H2O+1" def __init__(self, *args, **kwargs): if args and isinstance(args[0], (str, bytes)): try: self.ion_type, self.neutral_loss, self.charge = re.match(self._pattern, args[0]).groups() except Exception: raise PyteomicsError("Malformed ion string, must match the regex {!r}".format(self._pattern)) class ChargeList(list): """Just a list of :py:class:`Charge`s. When printed, looks like an enumeration of the list contents. Can also be constructed from such strings (e.g. "2+, 3+ and 4+"). """ def __init__(self, *args, **kwargs): if args and isinstance(args[0], (str, bytes)): delim = r'(?:,\s*)|(?:\s*and\s*)' self.extend(map(Charge, re.split(delim, args[0]))) else: try: super(ChargeList, self).__init__( sorted(set(args[0])), *args[1:], **kwargs) except Exception: super(ChargeList, self).__init__(*args, **kwargs) self[:] = map(Charge, self) def __str__(self): if len(self) > 1: return ', '.join(map(str, self[:-1])) + ' and {}'.format(self[-1]) elif self: return str(self[0]) return super(ChargeList, self).__str__() def _parse_charge(s, list_only=False): if not list_only: try: return Charge(s) except PyteomicsError: pass return ChargeList(s) def _parse_ion(ion_text): try: return Ion(ion_text) except Exception as e: warnings.warn('Could not parse ion string: {} ({})'.format(ion_text, e.args[0])) class BasicComposition(defaultdict, Counter): """A generic dictionary for compositions. Keys should be strings, values should be integers. Allows simple arithmetics.""" def __init__(self, *args, **kwargs): defaultdict.__init__(self, int) Counter.__init__(self, *args, **kwargs) for k, v in list(self.items()): if not v: del self[k] def __str__(self): return '{}({})'.format(type(self).__name__, dict.__repr__(self)) def __repr__(self): return str(self) def _repr_pretty_(self, p, cycle): if cycle: # should never happen p.text('{} object with a cyclic reference'.format(type(self).__name__)) p.text(str(self)) def __add__(self, other): result = self.copy() for elem, cnt in other.items(): result[elem] += cnt return result def __iadd__(self, other): for elem, cnt in other.items(): self[elem] += cnt return self def __radd__(self, other): return self + other def __sub__(self, other): result = self.copy() for elem, cnt in other.items(): result[elem] -= cnt return result def __isub__(self, other): for elem, cnt in other.items(): self[elem] -= cnt return self def __rsub__(self, other): return (self - other) * (-1) def __mul__(self, other): if not isinstance(other, int): raise PyteomicsError('Cannot multiply Composition by non-integer', other) return type(self)({k: v * other for k, v in self.items()}) def __imul__(self, other): if not isinstance(other, int): raise PyteomicsError('Cannot multiply Composition by non-integer', other) for elem in self: self[elem] *= other return self def __rmul__(self, other): return self * other def __eq__(self, other): if not isinstance(other, dict): return False self_items = {i for i in self.items() if i[1]} other_items = {i for i in other.items() if i[1]} return self_items == other_items # override default behavior: # we don't want to add 0's to the dictionary def __missing__(self, key): return 0 def __setitem__(self, key, value): if isinstance(value, float): value = int(round(value)) elif not isinstance(value, int): raise PyteomicsError('Only integers allowed as values in ' 'Composition, got {}.'.format(type(value).__name__)) if value: # reject 0's super(BasicComposition, self).__setitem__(key, value) elif key in self: del self[key] def copy(self): return type(self)(self) def __reduce__(self): class_, args, state, list_iterator, dict_iterator = super( BasicComposition, self).__reduce__() # Override the reduce of defaultdict so we do not provide the # `int` type as the first argument # which prevents from correctly unpickling the object args = () return class_, args, state, list_iterator, dict_iterator class _MappingOverAttributeProxy(object): '''A replacement for __dict__ for unpickling an object which once has __slots__ now but did not before.''' def __init__(self, obj): self.obj = obj def __getitem__(self, key): return getattr(self.obj, key) def __setitem__(self, key, value): setattr(self.obj, key, value) def __contains__(self, key): return hasattr(self.obj, key) def __repr__(self): return "{self.__class__.__name__}({self.obj})".format(self=self) class unitint(int): '''Represents an integer value with a unit name. Behaves identically to a built-in :class:`int` type. Attributes ---------- unit_info : :class:`str` The name of the unit this value posseses. ''' def __new__(cls, value, unit_info=None): inst = int.__new__(cls, value) inst.unit_info = unit_info return inst def __reduce__(self): return self.__class__, (int(self), self.unit_info) def _repr_pretty_(self, p, cycle): base = super(unitint, self).__repr__() if self.unit_info: string = "%s %s" % (base, self.unit_info) else: string = base p.text(string) class unitfloat(float): '''Represents an float value with a unit name. Behaves identically to a built-in :class:`float` type. Attributes ---------- unit_info : :class:`str` The name of the unit this value posseses. ''' __slots__ = ('unit_info', ) def __new__(cls, value, unit_info=None): inst = float.__new__(cls, value) inst.unit_info = unit_info return inst @property def __dict__(self): return _MappingOverAttributeProxy(self) def __reduce__(self): return self.__class__, (float(self), self.unit_info) def _repr_pretty_(self, p, cycle): base = super(unitfloat, self).__repr__() if self.unit_info: string = "%s %s" % (base, self.unit_info) else: string = base p.text(string) class unitstr(str): '''Represents an string value with a unit name. Behaves identically to a built-in :class:`str` type. Attributes ---------- unit_info : :class:`str` The name of the unit this value posseses. ''' __slots__ = ("unit_info", ) def __new__(cls, value, unit_info=None): inst = str.__new__(cls, value) inst.unit_info = unit_info return inst @property def __dict__(self): return _MappingOverAttributeProxy(self) def __reduce__(self): return self.__class__, (str(self), self.unit_info) def _repr_pretty_(self, p, cycle): base = super(unitstr, self).__repr__() if self.unit_info: string = "%s %s" % (base, self.unit_info) else: string = base p.text(string) class cvstr(str): '''A helper class to associate a controlled vocabullary accession number with an otherwise plain :class:`str` object Attributes ---------- accession : str The accession number for this parameter, e.g. MS:1000040 unit_accession : str The accession number for the unit of the value, if any ''' __slots__ = ('accession', 'unit_accession') _cache = {} def __new__(cls, value, accession=None, unit_accession=None): try: inst = cls._cache[value] if inst.accession == accession and inst.unit_accession == unit_accession: return inst except KeyError: pass inst = str.__new__(cls, value) inst.accession = _intern_unit_or_cv(accession) inst.unit_accession = _intern_unit_or_cv(unit_accession) cls._cache[value] = inst return inst @property def __dict__(self): return _MappingOverAttributeProxy(self) def __reduce__(self): return self.__class__, (str(self), self.accession, self.unit_accession) class CVQueryEngine(object): '''Traverse an arbitrarily nested dictionary looking for keys which are :class:`cvstr` instances, or objects with an attribute called ``accession``. ''' def _accession(self, key): return getattr(key, 'accession', None) def _query_dict(self, data, accession): for key, value in data.items(): if self._accession(key) == accession: if not isinstance(value, str) or value != '': return value else: return key elif isinstance(value, dict): inner = self._query_dict(value, accession) if inner is not None: return inner elif isinstance(value, (list, tuple)): inner = self._query_sequence(value, accession) if inner is not None: return inner elif self._accession(value) == accession: return value def _query_sequence(self, data, accession): for value in data: if isinstance(value, dict): inner = self._query_dict(value, accession) if inner is not None: return inner elif isinstance(value, (list, tuple)): inner = self._query_sequence(value, accession) if inner is not None: return inner elif self._accession(value) == accession: return value def query(self, data, accession): '''Search ``data`` for a key with the accession number ``accession``. Returns :const:`None` if not found. ''' if accession is None: raise TypeError("`accession` cannot be None") return self._query_dict(data, accession) def _is_empty(self, value): if isinstance(value, (str, bytes)): return value == '' return False def _walk_dict(self, data, index): for key, value in data.items(): accession = self._accession(key) if accession: if not self._is_empty(value): index[accession] = value else: index[accession] = key elif isinstance(value, dict): self._walk_dict(value, index) elif isinstance(value, (list, tuple)): self._walk_sequence(value, index) accession = self._accession(value) if accession: index[accession] = value return index def _walk_sequence(self, data, index): for value in data: if isinstance(value, dict): self._walk_dict(value, index) elif isinstance(value, (list, tuple)): self._walk_sequence(value, index) else: accession = self._accession(value) if accession: index[accession] = value def index(self, data): '''Construct a flat :class:`dict` whose keys are the accession numbers for all qualified keys in ``data`` and whose values are the mapped values from ``data``. ''' index = self._walk_dict(data, {}) return index def __call__(self, data, accession=None): '''If ``accession`` is :const:`None`, calls :meth:`index` on ``data``, otherwise calls :meth:`query` with ``data`` and ``accession``. ''' if accession is None: return self.index(data) else: return self.query(data, accession) '''A ready-to-use instance of :class:`~.CVQueryEngine`''' cvquery = CVQueryEngine()
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/pyteomics/auxiliary/math.py
pyteomics/auxiliary/math.py
from .structures import PyteomicsError def linear_regression_vertical(x, y=None, a=None, b=None): """Calculate coefficients of a linear regression y = a * x + b. The fit minimizes *vertical* distances between the points and the line. Requires :py:mod:`numpy`. Parameters ---------- x, y : array_like of float 1-D arrays of floats. If `y` is omitted, `x` must be a 2-D array of shape (N, 2). a : float, optional If specified then the slope coefficient is fixed and equals a. b : float, optional If specified then the free term is fixed and equals b. Returns ------- out : 4-tuple of float The structure is (a, b, r, stderr), where a -- slope coefficient, b -- free term, r -- Peason correlation coefficient, stderr -- standard deviation. """ import numpy as np x = np.asarray(x) if y is not None: y = np.asarray(y) else: if len(x.shape) != 2 or x.shape[-1] != 2: raise PyteomicsError( 'If `y` is not given, x.shape should be (N, 2), given: {}'.format(x.shape)) y = x[:, 1] x = x[:, 0] if (a is not None and b is None): b = (y - a * x).mean() elif (a is not None and b is not None): pass else: a, b = np.polyfit(x, y, 1) r = np.corrcoef(x, y)[0, 1] stderr = (y - a * x - b).std() return a, b, r, stderr def linear_regression(x, y=None, a=None, b=None): """Alias of :py:func:`linear_regression_vertical`.""" return linear_regression_vertical(x, y, a, b) def linear_regression_perpendicular(x, y=None): """Calculate coefficients of a linear regression y = a * x + b. The fit minimizes *perpendicular* distances between the points and the line. Requires :py:mod:`numpy`. Parameters ---------- x, y : array_like of float 1-D arrays of floats. If `y` is omitted, `x` must be a 2-D array of shape (N, 2). Returns ------- out : 4-tuple of float The structure is (a, b, r, stderr), where a -- slope coefficient, b -- free term, r -- Peason correlation coefficient, stderr -- standard deviation. """ import numpy as np x = np.asarray(x) if y is not None: y = np.asarray(y) data = np.hstack((x.reshape((-1, 1)), y.reshape((-1, 1)))) else: if len(x.shape) != 2 or x.shape[-1] != 2: raise PyteomicsError( 'If `y` is not given, x.shape should be (N, 2), given: {}'.format(x.shape)) data = x mu = data.mean(axis=0) eigenvectors, eigenvalues, V = np.linalg.svd((data - mu).T, full_matrices=False) a = eigenvectors[0][1] / eigenvectors[0][0] xm, ym = data.mean(axis=0) b = ym - a * xm r = np.corrcoef(data[:, 0], data[:, 1])[0, 1] stderr = ((data[:, 1] - a * data[:, 0] - b) / np.sqrt(a**2 + 1)).std() return a, b, r, stderr
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/doc/source/charge_vs_ph.py
doc/source/charge_vs_ph.py
import pylab import numpy as np from pyteomics import electrochem pHs = np.arange(1, 14, 0.5) # list of values of pH charges = electrochem.charge('PEPTIDE', pHs) # charge function accepts lists of pHs pylab.figure() pylab.plot(pHs, charges) pylab.title("Charge of peptide 'PEPTIDE' vs pH") pylab.xlabel('pH') pylab.ylabel('Charge') pylab.show()
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/doc/source/conf.py
doc/source/conf.py
# -*- coding: utf-8 -*- # # Pyteomics tutorial build configuration file, created by # sphinx-quickstart on Sat Dec 17 23:03:56 2011. # # This file is execfile()d with the current directory set to its containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. import sys, os # http://stackoverflow.com/q/10861463/1258041 from sphinx.ext.autodoc import ModuleLevelDocumenter, DataDocumenter def add_directive_header(self, sig): ModuleLevelDocumenter.add_directive_header(self, sig) # Rest of original method ignored DataDocumenter.add_directive_header = add_directive_header #sys.path.insert(0, os.path.abspath('exts')) pyteomics_path = os.path.abspath('../../') sys.path.insert(0, pyteomics_path) # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. #sys.path.insert(0, os.path.abspath('.')) # -- General configuration ----------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. #needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. extensions = [ 'sphinx.ext.napoleon', 'sphinx.ext.autodoc', 'sphinx.ext.autosummary', 'sphinx.ext.viewcode', 'sphinx.ext.doctest', 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.mathjax', 'matplotlib.sphinxext.plot_directive', # 'matplotlib.sphinxext.only_directives', 'matplotlib.sphinxext.mathmpl', 'sphinxcontrib.googleanalytics', 'sphinx_sitemap', ] toc_object_entries = False site_url = 'https://pyteomics.readthedocs.io/en/latest/' # Enable Google Analytics via a script from # http://pypi.python.org/pypi/sphinxcontrib-googleanalytics googleanalytics_enabled = True googleanalytics_id = 'G-0P9DXBVNL8' # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix of source filenames. source_suffix = '.rst' # The encoding of source files. #source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' # General information about the project. project = u'Pyteomics documentation' copyright = u'2011-2020, Lev Levitsky, Anton Goloborodko, Mikhail Gorshkov' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. from pyteomics import version as ver version = ver.__version__ # The full version, including alpha/beta/rc tags. release = version # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. #language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: #today = '' # Else, today_fmt is used as the format for a strftime call. #today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = [] # The reST default role (used for this markup: `text`) to use for all documents. #default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. #add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] # -- Options for HTML output --------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. html_theme = 'haiku-pyteomics' html_theme_path = ["./"] # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. #html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. #html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # "<project> v<release> documentation". html_title = "%s v%s" % (project, release) # A shorter title for the navigation bar. Default is the same as html_title. #html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. #html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. #html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. #html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. #html_domain_indices = True # If false, no index is generated. #html_use_index = True # If true, the index is split into individual pages for each letter. #html_split_index = False # If true, links to the reST sources are added to the pages. #html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. #html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. #html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a <link> tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. #html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = None # Output file base name for HTML help builder. htmlhelp_basename = 'Pyteomicstutorialdoc' # -- Options for LaTeX output -------------------------------------------------- latex_elements = { # The paper size ('letterpaper' or 'a4paper'). #'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). #'pointsize': '10pt', # Additional stuff for the LaTeX preamble. #'preamble': '', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ ('index', 'Pyteomicstutorial.tex', u'Pyteomics tutorial', u'Lev Levitsky, Anton Goloborodko', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. #latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. #latex_use_parts = False # If true, show page references after internal links. #latex_show_pagerefs = False # If true, show URL addresses after external links. #latex_show_urls = False # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. #latex_domain_indices = True # -- Options for manual page output -------------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ ('index', 'pyteomicstutorial', u'Pyteomics tutorial', [u'Lev Levitsky, Anton Goloborodko'], 1) ] # If true, show URL addresses after external links. #man_show_urls = False # -- Options for Texinfo output ------------------------------------------------ # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ ('index', 'Pyteomicstutorial', u'Pyteomics tutorial', u'Lev Levitsky, Anton Goloborodko', 'Pyteomicstutorial', 'A tutorial for effective use of Pyteomics package', 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. #texinfo_appendices = [] # If false, no module index is generated. #texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. #texinfo_show_urls = 'footnote' # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = {'python': ('https://docs.python.org/3', None)} def skip(app, what, name, obj, skip, options): if name == '__init__': return False return skip autodoc_default_options = {'members': None, 'inherited-members': None, 'show-inheritance': None} def setup(app): app.connect('autodoc-skip-member', skip)
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/doc/source/_static/filtering.py
doc/source/_static/filtering.py
# place this file in the same directory as example files import pylab from pyteomics import tandem, pepxml, mzid, auxiliary as aux, pylab_aux as pa import pandas as pd import numpy as np pylab.figure() with tandem.read('example.t.xml') as tf: h = pylab.hist([psm['rt'] / 60 for psm in tf], bins=25, label='All IDs') with tandem.filter('example.t.xml', fdr=0.01, full_output=False) as ftf: pylab.hist([psm['rt'] / 60 for psm in ftf], bins=h[1], label='1% FDR') pylab.xlabel('RT, min') pylab.legend() q1 = pepxml.qvalues('example.pep.xml', read_schema=False, key=lambda x: x['search_hit'][0]['search_score']['Morpheus Score'], reverse=True) q2 = tandem.qvalues('example.t.xml') pylab.figure() pa.plot_qvalue_curve(q1['q'], label='Morpheus') pa.plot_qvalue_curve(q2['q'], label='X!Tandem') pylab.legend() msgf = mzid.filter('example.mzid', retrieve_refs=True, key=lambda x: x['SpectrumIdentificationItem'][0]['MS-GF:EValue'], fdr=0.01) pylab.figure() pylab.hist([psm['SpectrumIdentificationItem'][0]['chargeState'] for psm in msgf], bins=np.arange(5), align='left') pylab.xticks(np.linspace(0, 4, 5)) pylab.xlabel('charge state') morpheus = pd.read_table('example.PSMs.tsv') amanda = pd.read_table('example_output.csv', skiprows=1) morph_filt = aux.filter(morpheus, fdr=0.01, key='Morpheus Score', reverse=True, is_decoy='Decoy?') morph_filt.plot(x='Retention Time (minutes)' , y='Precursor Mass (Da)', kind='scatter') amanda['isDecoy'] = [all(s.startswith('DECOY') for s in prot.split(';')) for prot in amanda['Protein Accessions']] amanda_filt = aux.filter(amanda[amanda['Rank'] == 1], key='Weighted Probability', is_decoy='isDecoy', fdr=0.01) amanda_pep = amanda_filt.sort_values('Weighted Probability').groupby('Sequence').first() morph_pep = morph_filt.sort_values('Q-Value (%)').groupby('Base Peptide Sequence').first() inter = amanda_pep.join(morph_pep, how='inner', lsuffix='[amanda]', rsuffix='[morpheus]') inter.plot('Amanda Score', 'Morpheus Score', kind='hexbin', gridsize=10) pylab.show()
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/doc/source/_static/example_fasta.py
doc/source/_static/example_fasta.py
import os from urllib.request import urlretrieve import gzip import matplotlib.pyplot as plt import numpy as np from pyteomics import fasta, parser, mass, achrom, electrochem, auxiliary if not os.path.isfile('yeast.fasta.gz'): print('Downloading the FASTA file for Saccharomyces cerevisiae...') urlretrieve( 'ftp://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/' 'reference_proteomes/Eukaryota/UP000002311_559292.fasta.gz', 'yeast.fasta.gz') print('Done!') print('Cleaving the proteins with trypsin...') unique_peptides = set() with gzip.open('yeast.fasta.gz', mode='rt') as gzfile: for description, sequence in fasta.FASTA(gzfile): new_peptides = parser.cleave(sequence, 'trypsin') unique_peptides.update(new_peptides) print('Done, {0} sequences obtained!'.format(len(unique_peptides))) peptides = [{'sequence': i} for i in unique_peptides] print('Parsing peptide sequences...') for peptide in peptides: peptide['parsed_sequence'] = parser.parse( peptide['sequence'], show_unmodified_termini=True) peptide['length'] = parser.length(peptide['parsed_sequence']) print('Done!') peptides = [peptide for peptide in peptides if peptide['length'] <= 100] print('Calculating the mass, charge and m/z...') for peptide in peptides: peptide['charge'] = int(round( electrochem.charge(peptide['parsed_sequence'], pH=2.0))) peptide['mass'] = mass.calculate_mass(peptide['parsed_sequence']) peptide['m/z'] = mass.calculate_mass(peptide['parsed_sequence'], charge=peptide['charge']) print('Done!') print('Calculating the retention time...') for peptide in peptides: peptide['RT_RP'] = achrom.calculate_RT( peptide['parsed_sequence'], achrom.RCs_zubarev) peptide['RT_normal'] = achrom.calculate_RT( peptide['parsed_sequence'], achrom.RCs_yoshida_lc) print('Done!') plt.figure() plt.hist([peptide['m/z'] for peptide in peptides], bins = 2000, range=(0,4000)) plt.xlabel('m/z, Th') plt.ylabel('# of peptides within 2 Th bin') plt.figure() plt.hist([peptide['charge'] for peptide in peptides], bins = 20, range=(0,10)) plt.xlabel('charge, e') plt.ylabel('# of peptides') x = [peptide['RT_RP'] for peptide in peptides] y = [peptide['RT_normal'] for peptide in peptides] heatmap, xbins, ybins = np.histogram2d(x, y, bins=100) heatmap[heatmap == 0] = np.nan a, b, r, stderr = auxiliary.linear_regression(x,y) plt.figure() plt.imshow(heatmap) plt.xlabel('RT on RP, min') plt.ylabel('RT on normal phase, min') plt.title('All tryptic peptides, RT correlation = {0}'.format(r)) x = [peptide['m/z'] for peptide in peptides] y = [peptide['RT_RP'] for peptide in peptides] heatmap, xbins, ybins = np.histogram2d(x, y, bins=[150, 2000], range=[[0, 4000], [0, 150]]) heatmap[heatmap == 0] = np.nan a, b, r, stderr = auxiliary.linear_regression(x,y) plt.figure() plt.imshow(heatmap, aspect='auto', origin='lower') plt.xlabel('m/z, Th') plt.ylabel('RT on RP, min') plt.title('All tryptic peptides, correlation = {0}'.format(r)) close_mass_peptides = [peptide for peptide in peptides if 700.0 <= peptide['m/z'] <= 701.0] x = [peptide['RT_RP'] for peptide in close_mass_peptides] y = [peptide['RT_normal'] for peptide in close_mass_peptides] a, b, r, stderr = auxiliary.linear_regression(x, y) plt.figure() plt.scatter(x, y) plt.xlabel('RT on RP, min') plt.ylabel('RT on normal phase, min') plt.title('Tryptic peptides with m/z=700-701 Th\nRT correlation = {0}'.format(r)) plt.show()
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/doc/source/_static/example_annotation.py
doc/source/_static/example_annotation.py
from pyteomics import pylab_aux as pa, usi import matplotlib.pyplot as plt spectrum = usi.proxi( 'mzspec:PXD004732:01650b_BC2-TUM_first_pool_53_01_01-3xHCD-1h-R2:scan:41840', 'massive') peptide = 'WNQLQAFWGTGK' pa.annotate_spectrum(spectrum, peptide, precursor_charge=2, backend='spectrum_utils', ion_types='aby', title=peptide) plt.show() from pyteomics import mass peptide = 'DLTDYLoxMK' # oxidized methionine aa_mass = mass.std_aa_mass.copy() aa_mass['ox'] = 15.9949 # define the mass of the label usi_top = 'mzspec:MSV000079960:DY_HS_Exp7-Ad1:scan:30372' usi_bottom = 'mzspec:MSV000080679:j11962_C1orf144:scan:10671' spectrum_top = usi.proxi(usi_top, 'massive') spectrum_bottom = usi.proxi(usi_bottom, 'massive') pa.mirror(spectrum_top, spectrum_bottom, peptide=peptide, precursor_charge=2, aa_mass=aa_mass, ion_types='aby', ftol=0.5, scaling='root', remove_precursor_peak=True, backend='spectrum_utils.iplot' ).properties(width=640, height=400).save('mirror.html')
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
levitsky/pyteomics
https://github.com/levitsky/pyteomics/blob/17e7f6bad7d273846de0a398a3870afe10c74be6/doc/source/_static/example_msms.py
doc/source/_static/example_msms.py
from pyteomics import mgf, pepxml, mass, pylab_aux import os from urllib.request import urlopen, Request import pylab # get the files for fname in ('mgf', 'pep.xml'): if not os.path.isfile('example.' + fname): headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'} url = 'http://pyteomics.readthedocs.io/en/latest/_static/example.' + fname request = Request(url, None, headers) target_name = 'example.' + fname with urlopen(request) as response, open(target_name, 'wb') as fout: print('Downloading ' + target_name + '...') fout.write(response.read()) with mgf.read('example.mgf') as spectra, pepxml.read('example.pep.xml') as psms: spectrum = next(spectra) psm = next(psms) pylab.figure() pylab_aux.plot_spectrum(spectrum, title="Experimental spectrum " + spectrum['params']['title']) pylab.figure() pylab_aux.annotate_spectrum(spectrum, psm['search_hit'][0]['peptide'], title='Annotated spectrum ' + psm['search_hit'][0]['peptide'], maxcharge=psm['assumed_charge']) def fragments(peptide, types=('b', 'y'), maxcharge=1): """ The function generates all possible m/z for fragments of types `types` and of charges from 1 to `maxharge`. """ for i in range(1, len(peptide)): for ion_type in types: for charge in range(1, maxcharge+1): if ion_type[0] in 'abc': yield mass.fast_mass( peptide[:i], ion_type=ion_type, charge=charge) else: yield mass.fast_mass( peptide[i:], ion_type=ion_type, charge=charge) fragment_list = list(fragments(psm['search_hit'][0]['peptide'], maxcharge=psm['assumed_charge'])) theor_spectrum = {'m/z array': fragment_list, 'intensity array': [spectrum['intensity array'].max()] * len(fragment_list)} pylab.figure() pylab.title('Theoretical and experimental spectra for ' + psm['search_hit'][0]['peptide']) pylab_aux.plot_spectrum(spectrum, width=0.1, linewidth=2, edgecolor='black') pylab_aux.plot_spectrum(theor_spectrum, width=0.1, edgecolor='red', alpha=0.7) pylab.show()
python
Apache-2.0
17e7f6bad7d273846de0a398a3870afe10c74be6
2026-01-05T07:13:10.794267Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/gateways/envoy-ai-gateway/rate-limiting/client.py
blueprints/gateways/envoy-ai-gateway/rate-limiting/client.py
#!/usr/bin/env python3 """ Test script for token-based rate limiting with GPT and Bedrock Claude """ import requests import json import time import subprocess def get_gateway_url(): """Auto-detect the Gateway URL using kubectl""" try: result = subprocess.run([ 'kubectl', 'get', 'gateway', 'ai-gateway', '-o', 'jsonpath={.status.addresses[0].value}' ], capture_output=True, text=True, check=True) if result.stdout.strip(): return f"http://{result.stdout.strip()}" else: print("Gateway address not found.") return None except subprocess.CalledProcessError as e: print(f"Error getting gateway URL: {e}") return None def test_gpt_rate_limiting(gateway_url): """Test rate limiting with GPT model""" print("🔹 Testing GPT Model Rate Limiting") print("-" * 40) model_name = "openai/gpt-oss-20b" user_id = "test-user-gpt" headers = { "Content-Type": "application/json", "x-ai-eg-model": model_name, "x-user-id": user_id } payload = { "model": model_name, "prompt": "Hello! This is a rate limiting test.", "max_tokens": 5, "temperature": 0.7 } successful_requests = 0 rate_limited_requests = 0 # Send multiple requests to trigger rate limiting for i in range(1, 6): # Test with 5 requests try: response = requests.post( f"{gateway_url}/v1/completions", headers=headers, json=payload, timeout=10 ) print(f"Request {i}: Status {response.status_code}", end="") if response.status_code == 200: successful_requests += 1 result = response.json() tokens = result.get('usage', {}).get('total_tokens', 'N/A') print(f" ✅ SUCCESS - Tokens: {tokens}") elif response.status_code == 429: rate_limited_requests += 1 print(f" 🚫 RATE LIMITED") else: print(f" ❌ ERROR") except requests.exceptions.RequestException as e: print(f"Request {i}: ❌ Request failed") time.sleep(0.5) return successful_requests, rate_limited_requests def test_bedrock_rate_limiting(gateway_url): """Test rate limiting with Bedrock Claude""" print("\n🔹 Testing Bedrock Claude Rate Limiting") print("-" * 40) model_name = "anthropic.claude-3-haiku-20240307-v1:0" user_id = "test-user-bedrock" headers = { "Content-Type": "application/json", "x-ai-eg-model": model_name, "x-user-id": user_id } payload = { "model": model_name, "messages": [ { "role": "user", "content": "Hello! This is a rate limiting test." } ], "max_tokens": 5, "anthropic_version": "bedrock-2023-05-31" } successful_requests = 0 rate_limited_requests = 0 # Send multiple requests to trigger rate limiting for i in range(1, 6): # Test with 5 requests try: response = requests.post( f"{gateway_url}/anthropic/v1/messages", headers=headers, json=payload, timeout=10 ) print(f"Request {i}: Status {response.status_code}", end="") if response.status_code == 200: successful_requests += 1 result = response.json() usage = result.get('usage', {}) input_tokens = usage.get('input_tokens', 0) output_tokens = usage.get('output_tokens', 0) total_tokens = input_tokens + output_tokens print(f" ✅ SUCCESS - Tokens: {total_tokens}") elif response.status_code == 429: rate_limited_requests += 1 print(f" 🚫 RATE LIMITED") else: print(f" ❌ ERROR") except requests.exceptions.RequestException as e: print(f"Request {i}: ❌ Request failed") time.sleep(0.5) return successful_requests, rate_limited_requests def main(): """Main test function""" gateway_url = get_gateway_url() if not gateway_url: print("❌ Could not determine Gateway URL. Exiting.") return print("🚀 Testing Token-Based Rate Limiting") print("=" * 50) print(f"Gateway URL: {gateway_url}") # Test GPT rate limiting gpt_success, gpt_limited = test_gpt_rate_limiting(gateway_url) # Test Bedrock rate limiting bedrock_success, bedrock_limited = test_bedrock_rate_limiting(gateway_url) # Summary print("\n" + "=" * 50) print("FINAL SUMMARY:") print(f"GPT Model: {gpt_success} successful, {gpt_limited} rate limited") print(f"Bedrock Claude: {bedrock_success} successful, {bedrock_limited} rate limited") if gpt_limited > 0 or bedrock_limited > 0: print("✅ Rate limiting is working!") elif gpt_success > 0 or bedrock_success > 0: print("⚠️ Rate limiting may not be active yet") else: print("❌ No successful requests - check configuration") if __name__ == "__main__": main()
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/gateways/envoy-ai-gateway/multi-model-routing/client.py
blueprints/gateways/envoy-ai-gateway/multi-model-routing/client.py
#!/usr/bin/env python3 """ Multi-Model AI Gateway Test Client Tests self-hosted models (Qwen3, GPT-OSS) and AWS Bedrock Claude """ import requests import json import subprocess import sys def get_gateway_url(): """Auto-detect AI Gateway URL""" try: result = subprocess.run([ 'kubectl', 'get', 'gateway', 'ai-gateway', '-o', 'jsonpath={.status.addresses[0].value}' ], capture_output=True, text=True, check=True) if result.stdout.strip(): return f"http://{result.stdout.strip()}" else: print("Gateway address not found. Make sure the Gateway is deployed and has an address.") return None except subprocess.CalledProcessError as e: print(f"Error getting gateway URL: {e}") return "http://localhost:8080" # fallback def test_qwen3_model(gateway_url): """Test Qwen3 model via /v1/chat/completions""" print("=== Testing Qwen3 1.7B ===") try: response = requests.post( f"{gateway_url}/v1/chat/completions", headers={ 'Content-Type': 'application/json', 'x-ai-eg-model': 'Qwen/Qwen3-1.7B' }, json={ 'model': 'Qwen/Qwen3-1.7B', 'max_tokens': 50, 'messages': [{'role': 'user', 'content': 'Hello from Qwen3!'}] }, timeout=30 ) print(f"Status Code: {response.status_code}") if response.status_code == 200: data = response.json() content = data.get('choices', [{}])[0].get('message', {}).get('content', 'No content') print(f"✅ SUCCESS: Qwen3 - {content[:100]}...") return True else: print(f"❌ ERROR: Qwen3 - {response.text}") return False except requests.exceptions.RequestException as e: print(f"❌ ERROR: Qwen3 - {e}") return False def test_gpt_model(gateway_url): """Test GPT model via /v1/chat/completions""" print("\n=== Testing Self-hosted GPT ===") try: response = requests.post( f"{gateway_url}/v1/chat/completions", headers={ 'Content-Type': 'application/json', 'x-ai-eg-model': 'openai/gpt-oss-20b' }, json={ 'model': 'openai/gpt-oss-20b', 'max_tokens': 50, 'messages': [{'role': 'user', 'content': 'Hello from GPT!'}] }, timeout=30 ) print(f"Status Code: {response.status_code}") if response.status_code == 200: data = response.json() content = data.get('choices', [{}])[0].get('message', {}).get('content', 'No content') print(f"✅ SUCCESS: GPT - {content[:100]}...") return True else: print(f"❌ ERROR: GPT - {response.text}") return False except requests.exceptions.RequestException as e: print(f"❌ ERROR: GPT - {e}") return False def test_bedrock_claude(gateway_url): """Test Bedrock Claude via /anthropic/v1/messages""" print("\n=== Testing Bedrock Claude ===") try: response = requests.post( f"{gateway_url}/anthropic/v1/messages", headers={ 'Content-Type': 'application/json', 'x-ai-eg-model': 'anthropic.claude-3-haiku-20240307-v1:0', 'anthropic-version': 'bedrock-2023-05-31' }, json={ 'model': 'anthropic.claude-3-haiku-20240307-v1:0', 'max_tokens': 50, 'messages': [{'role': 'user', 'content': 'Hello from Bedrock!'}] }, timeout=30 ) print(f"Status Code: {response.status_code}") if response.status_code == 200: data = response.json() content = data.get('content', [{}])[0].get('text', 'No content') print(f"✅ SUCCESS: Bedrock Claude - {content[:100]}...") return True else: print(f"❌ ERROR: Bedrock Claude - {response.text}") return False except requests.exceptions.RequestException as e: print(f"❌ ERROR: Bedrock Claude - {e}") return False def main(): print("🚀 AI Gateway Multi-Model Routing Test") print("=" * 60) gateway_url = get_gateway_url() if not gateway_url: print("❌ Could not determine Gateway URL. Exiting.") sys.exit(1) print(f"Gateway URL: {gateway_url}") results = [] results.append(test_qwen3_model(gateway_url)) results.append(test_gpt_model(gateway_url)) results.append(test_bedrock_claude(gateway_url)) print("\n" + "=" * 60) print("🎯 Final Results:") print(f"• Qwen3 1.7B: {'✅ PASS' if results[0] else '❌ FAIL'}") print(f"• GPT OSS 20B: {'✅ PASS' if results[1] else '❌ FAIL'}") print(f"• Bedrock Claude: {'✅ PASS' if results[2] else '❌ FAIL'}") passed = sum(results) print(f"\n📊 Summary: {passed}/3 models working") print("📋 Routing: Header-based using 'x-ai-eg-model'") print("🔗 All models accessible through single Gateway endpoint") if passed > 0: print(f"\n🎉 SUCCESS! {passed} model(s) working through AI Gateway!") sys.exit(0) else: print(f"\n❌ All tests failed") sys.exit(1) if __name__ == "__main__": main()
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/training/llama-lora-finetuning-trn1/assets/consolidate_adapter_shards_and_merge_model.py
blueprints/training/llama-lora-finetuning-trn1/assets/consolidate_adapter_shards_and_merge_model.py
from optimum.neuron.distributed.checkpointing import ( consolidate_model_parallel_checkpoints_to_unified_checkpoint, ) from transformers import AutoModel from argparse import ArgumentParser from shutil import copyfile import os import peft parser = ArgumentParser() parser.add_argument( "-i", "--input_dir", help="source checkpoint directory containing sharded adapter checkpoint files", required=True, ) parser.add_argument( "-o", "--output_dir", help="destination directory for final merged model (adapters merged into base model)", required=True, ) args = parser.parse_args() consolidated_ckpt_dir = os.path.join(args.input_dir, "consolidated") print("Consolidating the adapter shards into a PEFT-compatible checkpoint") consolidate_model_parallel_checkpoints_to_unified_checkpoint( args.input_dir, consolidated_ckpt_dir ) copyfile( os.path.join(args.input_dir, "adapter_config.json"), os.path.join(consolidated_ckpt_dir, "adapter_config.json"), ) # Load AutoPeftModel using the consolidated PEFT checkpoint peft_model = peft.AutoPeftModelForCausalLM.from_pretrained(consolidated_ckpt_dir) print("Meging adapter weights into base model and saving new pretrained model") merged_model = peft_model.merge_and_unload() merged_model.save_pretrained(args.output_dir) print("Loading the pretrained model and printing model") model = AutoModel.from_pretrained(args.output_dir) print(model)
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/training/llama-lora-finetuning-trn1/assets/test_model.py
blueprints/training/llama-lora-finetuning-trn1/assets/test_model.py
from datasets import load_dataset from transformers import AutoModelForCausalLM, AutoTokenizer import argparse # Set these as constants TOKENIZER_PATH = "meta-llama/Meta-Llama-3-8B-Instruct" BASE_MODEL = "meta-llama/Meta-Llama-3-8B" SAMPLE_INDICES = [94, 99, 123] def main(): parser = argparse.ArgumentParser(description='Evaluate SQL translation models') parser.add_argument('--tuned-model', required=True, help='Path to tuned model') args = parser.parse_args() # Initialize components tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_PATH) tokenizer.pad_token = tokenizer.eos_token base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL) tuned_model = AutoModelForCausalLM.from_pretrained(args.tuned_model) # Prepare dataset dataset = load_dataset("b-mc2/sql-create-context", split="train").shuffle(seed=23) eval_dataset = dataset.select(range(50000, 50500)).map( create_conversation, remove_columns=dataset.features, batched=False ) # Run evaluation evaluate_models(eval_dataset, tokenizer, base_model, tuned_model, SAMPLE_INDICES) def create_conversation(sample): system_msg = "You are a text-to-SQL translator. Generate SQL queries based on:\nSCHEMA:\n{schema}" return { "messages": [ {"role": "system", "content": system_msg.format(schema=sample["context"])}, {"role": "user", "content": sample["question"]}, {"role": "assistant", "content": sample["answer"]}, ] } def evaluate_models(dataset, tokenizer, base_model, tuned_model, indices): for idx in indices: messages = dataset[idx]['messages'][:-1] # Tokenize input example = tokenizer.apply_chat_template( messages, tokenize=False, add_generation_prompt=True ) tokenized = tokenizer.apply_chat_template( messages, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True ) prompt_len = tokenized["input_ids"].size(1) print(f"\nEvaluating sample {idx}") print(f"PROMPT:\n{example}\n") print("Generating output for the prompt using the base model and the new fine-tuned model. Please wait...\n\n") # Generate outputs base_output = base_model.generate(**tokenized, max_new_tokens=50, pad_token_id=128001) tuned_output = tuned_model.generate(**tokenized, max_new_tokens=50, pad_token_id=128001) # Display results print("BASE MODEL:\n", tokenizer.decode(base_output[0][prompt_len:]), "\n") print("FINE-TUNED MODEL:\n", tokenizer.decode(tuned_output[0][prompt_len:]), "\n\n") if __name__ == "__main__": main()
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/training/llama-lora-finetuning-trn1/assets/finetune_llama.py
blueprints/training/llama-lora-finetuning-trn1/assets/finetune_llama.py
from dataclasses import dataclass, field from datasets import load_dataset from peft import LoraConfig from transformers import ( AutoModelForCausalLM, AutoTokenizer, set_seed, ) from optimum.neuron import NeuronHfArgumentParser as HfArgumentParser from optimum.neuron import NeuronSFTConfig, NeuronSFTTrainer, NeuronTrainingArguments from optimum.neuron.distributed import lazy_load_for_parallelism def training_function(script_args, training_args): dataset = load_dataset("b-mc2/sql-create-context", split="train") dataset = dataset.shuffle(seed=23) # split dataset into train and eval train_dataset = dataset.select(range(50000)) eval_dataset = dataset.select(range(50000, 50500)) def create_conversation(sample): system_message = ( "You are a text to SQL query translator. Users will ask you questions in English and you will generate a " "SQL query based on the provided SCHEMA.\nSCHEMA:\n{schema}" ) return { "messages": [ {"role": "system", "content": system_message.format(schema=sample["context"])}, {"role": "user", "content": sample["question"]}, {"role": "assistant", "content": sample["answer"]}, ] } train_dataset = train_dataset.map( create_conversation, remove_columns=train_dataset.features, batched=False ) eval_dataset = eval_dataset.map( create_conversation, remove_columns=eval_dataset.features, batched=False ) tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct") tokenizer.pad_token = tokenizer.eos_token tokenizer.eos_token_id = 128001 with lazy_load_for_parallelism(tensor_parallel_size=training_args.tensor_parallel_size): model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B") config = LoraConfig( r=8, lora_alpha=64, lora_dropout=0.05, target_modules=[ "q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj", ], bias="none", task_type="CAUSAL_LM", use_rslora=True, ) args = training_args.to_dict() sft_config = NeuronSFTConfig( max_seq_length=1024, packing=True, **args, dataset_kwargs={ "add_special_tokens": False, "append_concat_token": True, }, ) trainer = NeuronSFTTrainer( args=sft_config, model=model, peft_config=config, tokenizer=tokenizer, train_dataset=train_dataset, eval_dataset=eval_dataset, ) print(trainer.model) trainer.model.print_trainable_parameters() # Start training trainer.train() @dataclass class ScriptArguments: model_id: str = field( default="meta-llama/Meta-Llama-3-8B", metadata={ "help": "The model that you want to train from the Hugging Face hub." }, ) if __name__ == "__main__": parser = HfArgumentParser([ScriptArguments, NeuronTrainingArguments]) script_args, training_args = parser.parse_args_into_dataclasses() set_seed(training_args.seed) training_function(script_args, training_args)
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/training/raytrain-llama2-pretrain-trn1/llama2_pretrain/tp_zero1_llama2_7b_hf_pretrain.py
blueprints/training/raytrain-llama2-pretrain-trn1/llama2_pretrain/tp_zero1_llama2_7b_hf_pretrain.py
# coding=utf-8 # Copyright (c) 2019 NVIDIA CORPORATION. All rights reserved. # Copyright 2018 The Google AI Language Team Authors and The HugginFace Inc. team. # Modifications Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import math import torch import sys import time import argparse import json import queue from typing import Any, Dict, List from datetime import datetime, timezone from collections import namedtuple import torch_xla import torch_xla.core.xla_model as xm from torch.utils.data.dataloader import DataLoader from torch.utils.data import DistributedSampler import torch_xla.distributed.parallel_loader as pl import torch.distributed as dist import torch_xla.distributed.xla_multiprocessing as xmp import torch_xla.distributed.xla_backend import numpy as np from transformers import ( AdamW, default_data_collator, set_seed, LlamaConfig, ) from transformers.optimization import get_linear_schedule_with_warmup import copy from torch.utils.tensorboard import SummaryWriter import inspect import requests import neuronx_distributed as nxd from neuronx_distributed.parallel_layers import parallel_state, layers, grads, checkpointing from neuronx_distributed.utils.model_utils import move_model_to_device from neuronx_distributed.parallel_layers.grads import bucket_allreduce_gradients from neuronx_distributed.parallel_layers.utils import is_pjrt_device import datasets from neuronx_distributed.optimizer import NeuronZero1Optimizer from neuronx_distributed.utils.adamw_fp32_optim_params import AdamW_FP32OptimParams from modeling_llama_nxd import CoreAttention,LlamaForCausalLM from logger import Logger # For PT autocast. torch.cuda.is_bf16_supported = lambda: True # Workaround for NaNs seen with transformers version >= 4.21.0 # https://github.com/aws-neuron/aws-neuron-sdk/issues/593 import transformers.modeling_utils as modeling_utils if os.environ.get("XLA_USE_BF16") or os.environ.get("XLA_DOWNCAST_BF16"): modeling_utils.get_parameter_dtype = lambda x: torch.bfloat16 datetime_str = str(datetime.now()) results = { "inference_success": 1 } Metric = namedtuple("Metric", ["name", "value", "units", "additional_data"]) class TrainingMetrics: """ This class is used for logging metrics to a json file. One can provide a dictionary of metrics that needs to be stored, and it wpuld get written to the file. Arguments: json_file: File used for logging. If no file exists, new file would be created. """ def __init__(self, json_file): self.json_file = json_file def read_modify_write_file(self, data, key: str = "metrics") -> None: """ data (dict of training parameters or list of metrics): Data to update in the file. key (str): the dictionary key under which data is to be recorded """ result_dict = {} print(f"Writing data to the provided results file: {self.json_file}") if os.path.exists(self.json_file): with open(self.json_file) as json_file: result_dict = json.loads(json_file.read()) or result_dict print(f"Updating with {key} data: {data}") if result_dict: try: # handle internal named entity if present results = result_dict[next(iter(result_dict))] except Exception: results = result_dict current = results.get(key) if not current: results[key] = data else: if isinstance(current, list): current.extend(data) elif isinstance(current, dict): current.update(data) else: result_dict["results"] = {key: data} with open(self.json_file, "w") as json_file: json.dump(result_dict, json_file) def store_metrics(self, metrics: List[Metric]) -> None: """ Writes collected metrics to the file. """ data = [ { "MetricName": metric.name, "MeasuredValue": metric.value, "Units": metric.units, "Timestamp": datetime.now(timezone.utc).isoformat(), "AdditionalData": metric.additional_data, } for metric in metrics ] self.update(data=data, key="metrics") def store_parameters(self, parameters: Dict[str, Any]) -> None: """ Writes specified model and configuration parameters to the file. """ self.update(data=parameters, key="parameters") def update(self, **kwargs: Any) -> None: """ Write specified data to the output file. """ self.read_modify_write_file(**kwargs) class Throughput: """ Used to calculate the throughput over a moving window. It records the step time between two calls and uses that time to calculate the throughput. """ def __init__( self, batch_size, world_size, grad_accum_usteps, moving_avg_window_size=10, logging_interval=1 ): self.seqs_per_iteration = batch_size * world_size * grad_accum_usteps*logging_interval self.moving_avg_window_size = math.ceil(moving_avg_window_size/logging_interval) self.moving_avg_window = queue.Queue() self.window_time = 0 self.start_time = time.time() def get_throughput(self): step_time = time.time() - self.start_time self.start_time += step_time self.window_time += step_time self.moving_avg_window.put(step_time) window_size = self.moving_avg_window.qsize() if window_size > self.moving_avg_window_size: self.window_time -= self.moving_avg_window.get() window_size -= 1 throughput = window_size * self.seqs_per_iteration / self.window_time return throughput # Workaround because python functions are not picklable class WorkerInitObj(object): def __init__(self, seed): self.seed = seed def __call__(self, id): set_seed(self.seed) def create_pretraining_dataset( data_dir, mini_batch_size, worker_init ): train_data = datasets.load_from_disk(os.path.expanduser(data_dir)) train_sampler = DistributedSampler( train_data, num_replicas=parallel_state.get_data_parallel_size(), rank=parallel_state.get_data_parallel_rank(), shuffle=False, drop_last=True, ) train_dataloader = DataLoader( train_data, collate_fn=default_data_collator, sampler=train_sampler, batch_size=mini_batch_size, num_workers=0, worker_init_fn=worker_init, drop_last=True, pin_memory=True, ) return train_dataloader def get_model(flags): model_path, seq_len = flags.model_path, flags.seq_len config = LlamaConfig.from_pretrained(model_path) config.use_cache = False config.kv_shared_group_size = flags.kv_replicator config.qkv_linear = flags.qkv_linear config.max_position_embeddings = max(config.max_position_embeddings, seq_len) if flags.num_layers > 0: config.num_hidden_layers = flags.num_layers if flags.sequence_parallel_enabled: config.sequence_parallel_enabled = True if flags.selective_checkpoint_enabled: config.selective_checkpoint_enabled = True xm.master_print(config) model = LlamaForCausalLM(config) def get_sin_cos_matrix(config): head_dim = config.hidden_size // config.num_attention_heads base = 10000 inv_freq = 1.0 / (base ** (torch.arange(0, head_dim, 2).float() / head_dim)) t = torch.arange(config.max_position_embeddings, dtype=inv_freq.dtype) freqs = torch.einsum("i,j->ij", t, inv_freq) # Different from paper, but it uses a different permutation in order to obtain the same calculation emb = torch.cat((freqs, freqs), dim=-1) return emb.cos()[None, None, :, :].to(torch.float32), emb.sin()[None, None, :, :].to(torch.float32) # Here we make sure we use the same sine and cosine matrices for all layers. # Making use of same tensors would make the CSE algorithm eliminate the lookup call # from layers, keeping only lookup from first layer. with torch.no_grad(): cos, sin = get_sin_cos_matrix(config) for layer in model.model.layers: layer.self_attn.rotary_emb.cos_cached = cos layer.self_attn.rotary_emb.sin_cached = sin xm.master_print(model) return model def get_dtype(model) -> str: """ Reference: https://pytorch.org/xla/release/1.12/index.html#xla-tensors-and-bfloat16 """ if "XLA_USE_BF16" in os.environ: return "torch.bfloat16" if "XLA_DOWNCAST_BF16" in os.environ: if "torch.float" in str(model.dtype): return "torch.bfloat16" if "torch.double" in str(model.dtype): return "torch.float32" return str(model.dtype) def allreduce_sequence_parallel_gradients(optimizer): """ All-reduce layernorm parameters across model parallel nodes when sequence parallelism is used. Modified from megatron-lm: https://gitlab-master.nvidia.com/ADLR/megatron-lm/-/blob/3f91f09bb2ab32f9904b47f46f19d2fc3f518ed8/megatron/training.py#L425 """ from neuronx_distributed.parallel_layers.mappings import reduce_from_tensor_model_parallel_region grads = [] for param_group in optimizer.__getstate__()['param_groups']: for group, params in param_group.items(): if group == 'params': for p in params: if isinstance(p, torch.Tensor) and p.grad is not None: sequence_parallel_param = getattr(p, 'sequence_parallel_enabled', False) if sequence_parallel_param: grads.append(p.grad.data) for grad in grads: # sum v.s. average: sum reduce_from_tensor_model_parallel_region(grad) def train_llama(flags): set_seed(flags.seed) if flags.use_meta_device_init: model_init_config = { "meta_device_init": True, "param_init_fn": init_weights, } else: model_init_config = None # Setting up NxD config nxd_config = nxd.neuronx_distributed_config( tensor_parallel_size=flags.tensor_parallel_size, optimizer_config={"zero_one_enabled": flags.use_zero_1, "grad_clipping": True, "max_grad_norm": 1.0}, sequence_parallel=flags.sequence_parallel_enabled, activation_checkpoint_config=CoreAttention if flags.selective_checkpoint_enabled else "full", model_init_config=model_init_config, ) # Creating NxD model model = nxd.initialize_parallel_model(nxd_config, get_model, flags) world_size = parallel_state.get_data_parallel_size() is_root = xm.is_master_ordinal(local=False) extract_graphs_only = os.environ.get("NEURON_EXTRACT_GRAPHS_ONLY", None) worker_init = WorkerInitObj(flags.seed) device = xm.xla_device() model_dtype = get_dtype(model) running_loss = torch.zeros(1, dtype=torch.double).to(device) param_optimizer = list(model.named_parameters()) no_decay = ["bias", "LayerNorm"] # gamma/beta are in LayerNorm.weight optimizer_grouped_parameters = [ { "params": [ p for n, p in param_optimizer if not any(nd in n for nd in no_decay) ], "weight_decay": 0.01, }, { "params": [ p for n, p in param_optimizer if any(nd in n for nd in no_decay) ], "weight_decay": 0.0, }, ] if flags.use_mix_precision: optimizer_cls = AdamW_FP32OptimParams else: optimizer_cls = AdamW # Creating NxD Optimizer optimizer = nxd.initialize_parallel_optimizer(nxd_config, optimizer_cls, optimizer_grouped_parameters, lr=flags.lr) optimizer.zero_grad() if is_root: if not os.path.exists(flags.output_dir): os.makedirs(flags.output_dir, exist_ok=True) if not extract_graphs_only: logger = Logger(flags, world_size, model_dtype) metric_writer = TrainingMetrics(flags.metrics_file) throughput = Throughput( flags.batch_size, world_size, flags.grad_accum_usteps, logging_interval=flags.logging_interval ) print("--------TRAINING CONFIG----------") print(flags) print("--------MODEL CONFIG----------") print(model.config) print("---------------------------------") metric_writer.store_parameters( { "Model": model.config.model_type, "Model configuration": str(model.config), "World size": xm.xrt_world_size(), "Data parallel degree": world_size, "Batch size": flags.batch_size, "Total steps": flags.steps_this_run, "Seed": flags.seed, "Optimizer": str(optimizer), "Data type": model_dtype, "Gradient accumulation microsteps": flags.grad_accum_usteps, "Warmup steps": flags.warmup_steps, "Dataset": os.path.basename(os.path.normpath(flags.data_dir)), "Environment variables": { variable: value for variable, value in os.environ.items() if variable.startswith("NEURON") or variable.startswith("XLA") }, } ) def train_loop_fn( model, optimizer, train_loader, epoch, global_step, training_ustep, running_loss, use_zero_1 ): for _, data in enumerate(train_loader): training_ustep += 1 input_ids = data["input_ids"] attention_mask = data["attention_mask"] labels = data["labels"] outputs = model( input_ids=input_ids, attention_mask=attention_mask, labels=labels, ) loss = outputs.loss / flags.grad_accum_usteps loss.backward() running_loss += loss.detach() if training_ustep % flags.grad_accum_usteps == 0: xm.mark_step() # loss averaging running_loss_div = running_loss / world_size # Collecting loss across all data-parallel ranks running_loss_reduced = xm.all_reduce( xm.REDUCE_SUM, running_loss_div, groups=parallel_state.get_data_parallel_group(as_list=True), ) running_loss_reduced_detached = running_loss_reduced.detach() running_loss.zero_() optimizer.step() total_norm = optimizer.grad_norm # Global norm before clipping optimizer.zero_grad() scheduler.step() global_step += 1 def _print_logs(running_loss_reduced_detached, total_norm): if is_root and not extract_graphs_only: total_norm_cpu = None if flags.print_grad_norm: total_norm_cpu = total_norm.cpu().item() # NOTE: The running_loss is the loss of the global_step logger.log( epoch, global_step, running_loss_reduced_detached.cpu().item(), optimizer.param_groups[0]["lr"], throughput.get_throughput(), total_norm_cpu, ) if global_step % flags.logging_interval == 0: # Printing the loss inside the step closure. This won't block # the tracing for next step. Also, we are logging every N steps. # This is done to reduce the overhead of copying tensors to CPU. # Tensor copy is expensive since it prevents the next step to start. xm.add_step_closure( _print_logs, (running_loss_reduced_detached, total_norm.detach()) ) # Save checkpoint using checkpoint API if (flags.checkpoint_freq > 0) and (global_step % flags.checkpoint_freq == 0): xm.add_step_closure( nxd.save_checkpoint, ( flags.checkpoint_dir, # checkpoint directory f"step_{global_step}", # tag model, # model optimizer, # optimizer scheduler, # scheduler {"epoch": epoch, "global_step": global_step, "cli_args": flags.__dict__}, # user content 8, # num_workers True, # use_xser flags.num_kept_checkpoint, #num_kept_ckpts ) ) if global_step >= flags.steps_this_run: # NOTE: Prevent runtime "Call to recv failed : Broken pipe" issue xm.mark_step() break return ( global_step, training_ustep, running_loss, running_loss_reduced_detached.cpu().item(), ) train_start = time.time() training_ustep = 0 scheduler = get_linear_schedule_with_warmup( optimizer, num_warmup_steps=flags.warmup_steps, num_training_steps=flags.max_steps, last_epoch=-1, ) if flags.resume_ckpt: user_content = nxd.load_checkpoint( flags.checkpoint_dir, tag=f"step_{flags.loading_step}", model=model, optimizer=optimizer, scheduler=scheduler, ) if user_content is not None: epoch = user_content["epoch"] global_step = user_content["global_step"] else: global_step = 0 epoch = 0 assert os.path.exists( os.path.expanduser(flags.data_dir) ), "ERROR: Data directory {} doesn't exist!".format(flags.data_dir) mini_batch_size = flags.batch_size train_dataloader = create_pretraining_dataset( flags.data_dir, mini_batch_size, worker_init ) # We wrap the dataloader with MpDeviceLoader. This dataloader should take # care of copying the tensors to device and also inserting the mark_step at # iteration end. train_device_loader = pl.MpDeviceLoader(train_dataloader, device) while True: xm.master_print( "Epoch {} begin {}".format(epoch, time.asctime()), flush=True, ) global_step, training_ustep, running_loss, final_loss = train_loop_fn( model, optimizer, train_device_loader, epoch, global_step, training_ustep, running_loss, flags.use_zero_1, ) if is_root and not extract_graphs_only: final_time = time.time() time_diff = final_time - train_start print( "Epoch {} step {} end {} loss {} perf {} seq/sec (at train microstep {} time {} from beginning time {})".format( epoch, global_step, time.asctime(), final_loss, logger.throughputs[-1], training_ustep, final_time, train_start, ), flush=True, ) additional_data = { "Epoch": epoch, "Global step": global_step, "Microstep": training_ustep, } metric_data = [ Metric("Loss", final_loss, "", additional_data), Metric( "Throughput", logger.throughputs[-1], "seq/s", additional_data ), ] metric_writer.store_metrics(metric_data) if global_step >= flags.steps_this_run: if is_root and not extract_graphs_only: # record aggregate & final statistics in the metrics file additional_data = { "Epoch": epoch, "Global step": global_step, "Microstep": training_ustep, } average_throughput = round( sum(logger.throughputs) / len(logger.throughputs), 4 ) metric_data = [ Metric("Final loss", final_loss, "", additional_data), Metric( "Time to train", round(time_diff / 60, 4), "minutes", additional_data, ), Metric( "Average throughput", average_throughput, "seq/s", additional_data, ), Metric( "Peak throughput", max(logger.throughputs), "seq/s", additional_data, ), ] metric_writer.store_metrics(metric_data) return epoch += 1 def _mp_fn(index, flags): torch.set_default_tensor_type("torch.FloatTensor") train_llama(flags) xm.rendezvous("_mp_fn finished") if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--model_path", type=str, help="Model weight and config path.", ) parser.add_argument( "--data_dir", type=str, help="Pre-tokenized dataset directory.", ) parser.add_argument( "--output_dir", type=str, default="./output", help="Directory for checkpoints and logs.", ) parser.add_argument( "--metrics_file", type=str, default="results.json", help="training metrics results file", ) parser.add_argument("--batch_size", type=int, default=8, help="Worker batch size.") parser.add_argument( "--max_steps", type=int, help="Maximum total accumulation-steps to run.", ) parser.add_argument( "--steps_this_run", type=int, help="Exit early at <value> steps and not go to max_steps. -1 to mean no early exit.", ) parser.add_argument( "--seed", type=int, default=12349, help="Random seed. Worker seed is this value + worker rank.", ) parser.add_argument("--lr", type=float, default=4e-4, help="Learning rate.") parser.add_argument( "--warmup_steps", type=int, default=2000, help="Number of warmup accumulation-steps for learning rate .", ) parser.add_argument( "--grad_accum_usteps", type=int, default=64, help="Gradient accumulation micro-steps (an accumulation-step has <value> micro-steps.", ) parser.add_argument( "--print_grad_norm", default=False, action="store_true", help="Whether to print grad norm", ) parser.add_argument( "--resume_ckpt", action="store_true", help="Resume from checkpoint at resume_step." ) parser.add_argument( "--tensor_parallel_size", default=2, type=int, help="Tensor parallel size" ) parser.add_argument( "--seq_len", default=2048, type=int, help="Sequence length" ) parser.add_argument( "--use_mix_precision", action="store_true", help="Use mix precision." ) parser.add_argument( "--use_zero_1", action="store_true", help="Use ZeRO-1." ) parser.add_argument( "--num_layers", type=int, default=-1, help="Override number of layers for this LLaMA model", ) parser.add_argument( "--sequence_parallel_enabled", default=False, action="store_true", help="Enable sequence parallel", ) parser.add_argument( "--selective_checkpoint_enabled", default=False, action="store_true", help="Enable selective checkpoint", ) parser.add_argument( "--use_meta_device_init", default=False, action="store_true", help="use meta device initialization", ) parser.add_argument( "--logging_interval", default=1, type=int, help="logging every N steps", ) parser.add_argument( "--qkv_linear", default=False, action="store_true", help="Whether to use the QKV Module", ) parser.add_argument( "--kv_replicator", default=1, type=int, help="KV replication number", ) #Checkpointing parser.add_argument("--checkpoint_freq", type=int, default=100000, help="save checkpoint freq") parser.add_argument("--checkpoint_dir", type=str, default=None) parser.add_argument("--loading_step", type=int, default=-1, help="load from step, -1 means no load") parser.add_argument("--num_kept_checkpoint", type=int, default=-1, help="number of checkpoints kept, old checkpoint will get deleted") args = parser.parse_args(sys.argv[1:]) if args.steps_this_run < 0: args.steps_this_run = args.max_steps os.environ["NEURON_RT_STOCHASTIC_ROUNDING_EN"] = "1" if args.use_mix_precision: os.environ["XLA_DOWNCAST_BF16"]="1" else: os.environ["XLA_USE_BF16"]="1" # WORLD_SIZE is set by torchrun if os.environ.get("WORLD_SIZE"): if is_pjrt_device(): import torch_xla.experimental.pjrt_backend dist.init_process_group("xla", init_method="pjrt://") else: dist.init_process_group("xla") _mp_fn(0, args) else: xmp.spawn(_mp_fn, args=(args,))
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/training/raytrain-llama2-pretrain-trn1/llama2_pretrain/modeling_llama_nxd.py
blueprints/training/raytrain-llama2-pretrain-trn1/llama2_pretrain/modeling_llama_nxd.py
# coding=utf-8 # Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. # # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX # and OPT implementations in this library. It has been modified from its # original forms to accommodate minor architectural differences compared # to GPT-NeoX and OPT used by the Meta AI team that trained the model. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ PyTorch LLaMA model.""" import os import math from packaging import version from typing import List, Optional, Tuple, Union import torch import torch.nn.functional as F import torch.utils.checkpoint from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from transformers.activations import ACT2FN from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast from transformers.modeling_utils import PreTrainedModel from transformers.utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings from transformers.models.llama.configuration_llama import LlamaConfig from neuronx_distributed.parallel_layers.layers import ParallelEmbedding, ColumnParallelLinear, RowParallelLinear from neuronx_distributed.parallel_layers.loss_functions import parallel_cross_entropy from neuronx_distributed.parallel_layers.parallel_state import get_tensor_model_parallel_size, get_tensor_model_parallel_rank import neuronx_distributed.parallel_layers.utils as neuronx_dist_utils from neuronx_distributed.utils.model_utils import move_model_to_device from neuronx_distributed.parallel_layers import mappings from neuronx_distributed.modules.qkv_linear import GQAQKVColumnParallelLinear import torch_xla.core.xla_model as xm from transformers.models.llama.modeling_llama import LlamaForCausalLM as LlamaForCausalLMHF from transformers.models.llama.modeling_llama import LlamaRMSNorm as LlamaRMSNormHF from transformers.models.llama.modeling_llama import LlamaDecoderLayer as LlamaDecoderLayerHF from transformers.models.llama.modeling_llama import LlamaMLP as LlamaMLPHF from transformers.models.llama.modeling_llama import LlamaAttention as LlamaAttentionHF from transformers.models.llama.modeling_llama import LlamaModel as LlamaModelHF from transformers.models.llama.modeling_llama import ( LlamaRotaryEmbedding, LlamaLinearScalingRotaryEmbedding, LlamaPreTrainedModel, LlamaForSequenceClassification, rotate_half, apply_rotary_pos_emb, repeat_kv, LLAMA_START_DOCSTRING, LLAMA_INPUTS_DOCSTRING, ) from functools import partial def _init_normal(std, w): return nn.init.normal_(w, mean=0.0, std=std) if version.parse(torch.__version__) >= version.parse("2.1"): from torch_xla.utils.checkpoint import checkpoint checkpoint_method = checkpoint else: checkpoint_method = torch.utils.checkpoint.checkpoint logger = logging.get_logger(__name__) _CONFIG_FOR_DOC = "LlamaConfig" # Copied from transformers.models.bart.modeling_bart._make_causal_mask def _make_causal_mask( input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0 ): """ Make causal mask used for bi-directional self-attention. """ bsz, tgt_len = input_ids_shape mask = torch.full((tgt_len, tgt_len), torch.finfo(dtype).min, device=device) mask_cond = torch.arange(mask.size(-1), device=device) mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0) mask = mask.to(dtype) if past_key_values_length > 0: mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1) return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length) # Copied from transformers.models.bart.modeling_bart._expand_mask def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None): """ Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`. """ bsz, src_len = mask.size() tgt_len = tgt_len if tgt_len is not None else src_len expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype) inverted_mask = 1.0 - expanded_mask return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min) class LlamaRMSNorm(LlamaRMSNormHF): def __init__(self, hidden_size, eps=1e-6, sequence_parallel_enabled=False): """ LlamaRMSNorm is equivalent to T5LayerNorm """ super().__init__(hidden_size, eps=eps) setattr(self.weight, "sequence_parallel_enabled", sequence_parallel_enabled) def forward(self, hidden_states): input_dtype = hidden_states.dtype hidden_states = hidden_states.to(torch.double) variance = hidden_states.pow(2).mean(-1, keepdim=True) hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) return self.weight * hidden_states.to(input_dtype) class LlamaMLP(LlamaMLPHF): def __init__(self, config): nn.Module.__init__(self) self.config = config self.pretraining_tp = config.pretraining_tp self.hidden_size = config.hidden_size self.intermediate_size = config.intermediate_size self.act_fn = ACT2FN[config.hidden_act] init_method = partial(_init_normal, config.initializer_range) self.gate_up_proj = ColumnParallelLinear( self.hidden_size, 2 * self.intermediate_size, stride=2, bias=False, gather_output=False, init_method=init_method, sequence_parallel_enabled=self.config.sequence_parallel_enabled, ) self.down_proj = RowParallelLinear( self.intermediate_size, self.hidden_size, bias=False, input_is_parallel=True, init_method=init_method, sequence_parallel_enabled=self.config.sequence_parallel_enabled, ) self.split_size = self.intermediate_size // get_tensor_model_parallel_size() if config.move_model_to_device: move_model_to_device(self, xm.xla_device()) def forward(self, x): if self.pretraining_tp > 1: slice = self.intermediate_size // self.pretraining_tp gate_proj_slices = self.gate_proj.weight.split(slice, dim=0) up_proj_slices = self.up_proj.weight.split(slice, dim=0) down_proj_slices = self.down_proj.weight.split(slice, dim=1) gate_proj = torch.cat([F.linear(x, gate_proj_slices[i]) for i in range(self.pretraining_tp)], dim=-1) up_proj = torch.cat([F.linear(x, up_proj_slices[i]) for i in range(self.pretraining_tp)], dim=-1) intermediate_states = (self.act_fn(gate_proj) * up_proj).split(slice, dim=2) down_proj = [F.linear(intermediate_states[i], down_proj_slices[i]) for i in range(self.pretraining_tp)] down_proj = sum(down_proj) else: gate_proj, up_proj = self.gate_up_proj(x).split(self.split_size, dim=2) def activation_mlp(gate_proj, up_proj): activation_output = self.act_fn(gate_proj) return (activation_output * up_proj) # We checkpoint the MLP compute too, since we see extra data movement which is more # expensive than the recompute in this case. if self.config.selective_checkpoint_enabled: intermediate_states = checkpoint_method(activation_mlp, gate_proj, up_proj) else: intermediate_states = (self.act_fn(gate_proj) * up_proj) down_proj = self.down_proj(intermediate_states) return down_proj class CoreAttention(nn.Module): def __init__(self): super().__init__() def forward(self, query_states, key_states, value_states): bsz, num_heads, q_len, head_dim = query_states.shape kv_seq_len = key_states.shape[-2] attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(head_dim) if attn_weights.size() != (bsz, num_heads, q_len, kv_seq_len): raise ValueError( f"Attention weights should be of size {(bsz, num_heads, q_len, kv_seq_len)}, but is" f" {attn_weights.size()}" ) causal_mask = torch.triu(torch.ones((1, 1, q_len, kv_seq_len), device='xla'), diagonal=1).bool() attn_weights = attn_weights.masked_fill_(causal_mask, -10000.0) attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.double).to(query_states.dtype) attn_output = torch.matmul(attn_weights, value_states) return attn_output class LlamaAttention(LlamaAttentionHF): """Multi-headed attention from 'Attention Is All You Need' paper""" def __init__(self, config: LlamaConfig): nn.Module.__init__(self) self.config = config self.hidden_size = config.hidden_size self.num_heads = config.num_attention_heads self.head_dim = self.hidden_size // self.num_heads self.num_key_value_heads = config.num_key_value_heads self.num_key_value_groups = self.num_heads // self.num_key_value_heads self.pretraining_tp = config.pretraining_tp self.max_position_embeddings = config.max_position_embeddings if not hasattr(config, "kv_shared_group_size"): config.kv_shared_group_size = 1 if not hasattr(config, "qkv_linear"): config.qkv_linear = False if (self.head_dim * self.num_heads) != self.hidden_size: raise ValueError( f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}" f" and `num_heads`: {self.num_heads})." ) self._init_rope() init_method = partial(_init_normal, config.initializer_range) if self.num_heads == self.num_key_value_heads: self.qkv_proj = ColumnParallelLinear( self.hidden_size, 3 * self.num_heads * self.head_dim, stride=3, bias=False, gather_output=False, init_method=init_method, sequence_parallel_enabled=self.config.sequence_parallel_enabled, ) self.split_size = self.num_heads * self.head_dim // get_tensor_model_parallel_size() elif self.config.qkv_linear: self.qkv_proj = GQAQKVColumnParallelLinear( self.hidden_size, [self.num_heads * self.head_dim, self.num_key_value_heads * self.head_dim], bias=False, gather_output=False, init_method=init_method, sequence_parallel_enabled= self.config.sequence_parallel_enabled, kv_size_multiplier=self.config.kv_shared_group_size, ) else: self.q_proj = ColumnParallelLinear( self.hidden_size, self.num_heads * self.head_dim, bias=False, gather_output=False, init_method=init_method, sequence_parallel_enabled=self.config.sequence_parallel_enabled, ) self.k_proj = ColumnParallelLinear( self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False, gather_output=False, init_method=init_method, sequence_parallel_enabled=self.config.sequence_parallel_enabled, ) self.v_proj = ColumnParallelLinear( self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False, gather_output=False, init_method=init_method, sequence_parallel_enabled=self.config.sequence_parallel_enabled, ) self.o_proj = RowParallelLinear( self.num_heads * self.head_dim, self.hidden_size, bias=False, input_is_parallel=True, init_method=init_method, sequence_parallel_enabled=self.config.sequence_parallel_enabled, ) self.num_heads = neuronx_dist_utils.divide(config.num_attention_heads, get_tensor_model_parallel_size()) self.num_key_value_heads = neuronx_dist_utils.divide(config.num_key_value_heads*self.config.kv_shared_group_size, get_tensor_model_parallel_size()) self.num_key_value_groups = self.num_heads // self.num_key_value_heads self.core_attn = CoreAttention() if config.move_model_to_device: move_model_to_device(self, xm.xla_device()) def forward( self, hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Tuple[torch.Tensor]] = None, output_attentions: bool = False, use_cache: bool = False, ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() if self.config.sequence_parallel_enabled: q_len, bsz, _ = hidden_states.size() q_len = q_len * get_tensor_model_parallel_size() if self.pretraining_tp > 1: key_value_slicing = (self.num_key_value_heads * self.head_dim) // self.pretraining_tp query_slices = self.q_proj.weight.split((self.num_heads * self.head_dim) // self.pretraining_tp, dim=0) key_slices = self.k_proj.weight.split(key_value_slicing, dim=0) value_slices = self.v_proj.weight.split(key_value_slicing, dim=0) query_states = [F.linear(hidden_states, query_slices[i]) for i in range(self.pretraining_tp)] query_states = torch.cat(query_states, dim=-1) key_states = [F.linear(hidden_states, key_slices[i]) for i in range(self.pretraining_tp)] key_states = torch.cat(key_states, dim=-1) value_states = [F.linear(hidden_states, value_slices[i]) for i in range(self.pretraining_tp)] value_states = torch.cat(value_states, dim=-1) else: if self.num_heads == self.num_key_value_heads and self.config.kv_shared_group_size == 1: qkv_states = self.qkv_proj(hidden_states) query_states, key_states, value_states = qkv_states.split(self.split_size, dim=2) elif self.config.qkv_linear: query_states, key_states, value_states = self.qkv_proj(hidden_states) else: query_states = self.q_proj(hidden_states) key_states = self.k_proj(hidden_states) value_states = self.v_proj(hidden_states) if self.config.sequence_parallel_enabled: query_states = query_states.view(q_len, bsz, self.num_heads, self.head_dim).permute(1, 2, 0, 3) key_states = key_states.view(q_len, bsz, self.num_key_value_heads, self.head_dim).permute(1, 2, 0, 3) value_states = value_states.view(q_len, bsz, self.num_key_value_heads, self.head_dim).permute(1, 2, 0, 3) else: query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) kv_seq_len = key_states.shape[-2] if past_key_value is not None: kv_seq_len += past_key_value[0].shape[-2] cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len) query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids) if past_key_value is not None: # reuse k, v, self_attention key_states = torch.cat([past_key_value[0], key_states], dim=2) value_states = torch.cat([past_key_value[1], value_states], dim=2) past_key_value = (key_states, value_states) if use_cache else None # repeat k/v heads if n_kv_heads < n_heads key_states = repeat_kv(key_states, self.num_key_value_groups) value_states = repeat_kv(value_states, self.num_key_value_groups) attn_output = self.core_attn(query_states, key_states, value_states) if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim): raise ValueError( f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is" f" {attn_output.size()}" ) if self.config.sequence_parallel_enabled: attn_output = attn_output.permute(2, 0, 1, 3) attn_output = attn_output.reshape(q_len, bsz, self.hidden_size // get_tensor_model_parallel_size()) else: attn_output = attn_output.transpose(1, 2).contiguous() attn_output = attn_output.reshape(bsz, q_len, self.hidden_size // get_tensor_model_parallel_size()) if self.pretraining_tp > 1: attn_output = attn_output.split(self.hidden_size // self.pretraining_tp, dim=2) o_proj_slices = self.o_proj.weight.split(self.hidden_size // self.pretraining_tp, dim=1) attn_output = sum([F.linear(attn_output[i], o_proj_slices[i]) for i in range(self.pretraining_tp)]) else: attn_output = self.o_proj(attn_output) if not output_attentions: attn_weights = None return attn_output, attn_weights, past_key_value class LlamaDecoderLayer(LlamaDecoderLayerHF): def __init__(self, config: LlamaConfig): nn.Module.__init__(self) self.hidden_size = config.hidden_size self.self_attn = LlamaAttention(config=config) self.mlp = LlamaMLP(config) self.input_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps, sequence_parallel_enabled=config.sequence_parallel_enabled) self.post_attention_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps, sequence_parallel_enabled=config.sequence_parallel_enabled) @add_start_docstrings( "The bare LLaMA Model outputting raw hidden-states without any specific head on top.", LLAMA_START_DOCSTRING, ) class LlamaModel(LlamaModelHF): """ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`LlamaDecoderLayer`] Args: config: LlamaConfig """ def __init__(self, config: LlamaConfig): LlamaPreTrainedModel.__init__(self, config) self.padding_idx = config.pad_token_id self.vocab_size = config.vocab_size init_method = partial(_init_normal, config.initializer_range) self.embed_tokens = ParallelEmbedding(config.vocab_size, config.hidden_size, self.padding_idx, init_method=init_method) self.layers = nn.ModuleList([LlamaDecoderLayer(config) for _ in range(config.num_hidden_layers)]) self.norm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps, sequence_parallel_enabled=config.sequence_parallel_enabled) self.gradient_checkpointing = False # Initialize weights and apply final processing self.post_init() # Copied from transformers.models.bart.modeling_bart.BartDecoder._prepare_decoder_attention_mask def _prepare_decoder_attention_mask(self, attention_mask, input_shape, inputs_embeds, past_key_values_length): # create causal mask # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len] combined_attention_mask = None if input_shape[-1] > 1: combined_attention_mask = _make_causal_mask( input_shape, inputs_embeds.dtype, device=inputs_embeds.device, past_key_values_length=past_key_values_length, ) if attention_mask is not None: pass return combined_attention_mask @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING) def forward( self, input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[List[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) use_cache = use_cache if use_cache is not None else self.config.use_cache return_dict = return_dict if return_dict is not None else self.config.use_return_dict # retrieve input_ids and inputs_embeds if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time") elif input_ids is not None: batch_size, seq_length = input_ids.shape elif inputs_embeds is not None: batch_size, seq_length, _ = inputs_embeds.shape else: raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds") seq_length_with_past = seq_length past_key_values_length = 0 if past_key_values is not None: past_key_values_length = past_key_values[0][0].shape[2] seq_length_with_past = seq_length_with_past + past_key_values_length if position_ids is None: device = input_ids.device if input_ids is not None else inputs_embeds.device position_ids = torch.arange( past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device ) position_ids = position_ids.unsqueeze(0).view(-1, seq_length) else: position_ids = position_ids.view(-1, seq_length).long() if inputs_embeds is None: inputs_embeds = self.embed_tokens(input_ids) # embed positions # if attention_mask is None: # attention_mask = torch.ones( # (batch_size, seq_length_with_past), dtype=torch.bool, device=inputs_embeds.device # ) # attention_mask = self._prepare_decoder_attention_mask( # attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length # ) hidden_states = inputs_embeds if self.gradient_checkpointing and self.training: if use_cache: logger.warning_once( "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." ) use_cache = False # decoder layers all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None next_decoder_cache = () if use_cache else None if self.config.sequence_parallel_enabled: hidden_states = hidden_states.transpose(0, 1).contiguous() hidden_states = mappings.scatter_to_sequence_parallel_region(hidden_states) for idx, decoder_layer in enumerate(self.layers): if output_hidden_states: all_hidden_states += (hidden_states,) past_key_value = past_key_values[idx] if past_key_values is not None else None if self.gradient_checkpointing and self.training: def create_custom_forward(module): def custom_forward(*inputs): # None for past_key_value return module(*inputs, output_attentions, None) return custom_forward layer_outputs = checkpoint_method( create_custom_forward(decoder_layer), hidden_states, attention_mask, position_ids, None, ) else: layer_outputs = decoder_layer( hidden_states, attention_mask=attention_mask, position_ids=position_ids, past_key_value=past_key_value, output_attentions=output_attentions, use_cache=use_cache, ) hidden_states = layer_outputs[0] if use_cache: next_decoder_cache += (layer_outputs[2 if output_attentions else 1],) if output_attentions: all_self_attns += (layer_outputs[1],) hidden_states = self.norm(hidden_states) if self.config.sequence_parallel_enabled: hidden_states = mappings.gather_from_sequence_parallel_region(hidden_states, to_model_parallel=False) hidden_states = hidden_states.transpose(0, 1).contiguous() # add hidden states from the last decoder layer if output_hidden_states: all_hidden_states += (hidden_states,) next_cache = next_decoder_cache if use_cache else None if not return_dict: return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None) return BaseModelOutputWithPast( last_hidden_state=hidden_states, past_key_values=next_cache, hidden_states=all_hidden_states, attentions=all_self_attns, ) class LlamaForCausalLM(LlamaForCausalLMHF): _tied_weights_keys = ["lm_head.weight"] def __init__(self, config): LlamaPreTrainedModel.__init__(self, config) self.model = LlamaModel(config) self.pretraining_tp = config.pretraining_tp self.vocab_size = config.vocab_size init_method = partial(_init_normal, config.initializer_range) self.lm_head = ColumnParallelLinear( config.hidden_size, config.vocab_size, bias=False, gather_output=False, init_method=init_method, ) # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC) def forward( self, input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[List[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple, CausalLMOutputWithPast]: r""" Args: labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`. Returns: Example: ```python >>> from transformers import AutoTokenizer, LlamaForCausalLM >>> model = LlamaForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS) >>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER) >>> prompt = "Hey, are you conscious? Can you talk to me?" >>> inputs = tokenizer(prompt, return_tensors="pt") >>> # Generate >>> generate_ids = model.generate(inputs.input_ids, max_length=30) >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you." ```""" output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) return_dict = return_dict if return_dict is not None else self.config.use_return_dict # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn) outputs = self.model( input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, past_key_values=past_key_values, inputs_embeds=inputs_embeds, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, ) hidden_states = outputs[0] if self.pretraining_tp > 1: lm_head_slices = self.lm_head.weight.split(self.vocab_size // self.pretraining_tp, dim=0) logits = [F.linear(hidden_states, lm_head_slices[i]) for i in range(self.pretraining_tp)] logits = torch.cat(logits, dim=-1) else: logits = self.lm_head(hidden_states) logits = logits.double() loss = None if labels is not None: # Shift so that tokens < n predict n shift_logits = logits[..., :-1, :].contiguous() shift_labels = labels[..., 1:].contiguous() # Flatten the tokens loss_fct = parallel_cross_entropy shift_logits = shift_logits.view(-1, shift_logits.size(-1)) shift_labels = shift_labels.view(-1) # Enable model parallelism shift_labels = shift_labels.to(shift_logits.device) loss = loss_fct(shift_logits, shift_labels) loss = torch.mean(loss) if not return_dict: output = (logits,) + outputs[1:] return (loss,) + output if loss is not None else output return CausalLMOutputWithPast( loss=loss, logits=logits, past_key_values=outputs.past_key_values, hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) def init_weights(module): """ Re-init weights after partition Referred from HF transformers https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/modeling_llama.py#L690 """ if isinstance(module, torch.nn.Linear): module.weight.data.normal_(mean=0.0, std=config.initializer_range) if module.bias is not None: module.bias.data.zero_() elif isinstance(module, torch.nn.Embedding): module.weight.data.normal_(mean=0.0, std=config.initializer_range) if module.padding_idx: module.weight.data[module.padding_idx].zero_() elif isinstance(module, LlamaRMSNorm): module.weight.data.fill_(1.0) elif isinstance(module, (ParallelEmbedding, RowParallelLinear, ColumnParallelLinear)): module.init_weight_cpu() if hasattr(module, "bias") and module.bias is not None: module.bias.data.zero_() elif isinstance(module, GQAQKVColumnParallelLinear): module.initialize_weight_biases()
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/training/raytrain-llama2-pretrain-trn1/llama2_pretrain/ray_train_llama2.py
blueprints/training/raytrain-llama2-pretrain-trn1/llama2_pretrain/ray_train_llama2.py
from argparse import ArgumentParser import ray from ray.train import ScalingConfig from ray.train.torch import TorchTrainer from ray.train.torch.xla import TorchXLAConfig # Use training function from NxD llama2 example # see: https://awsdocs-neuron.readthedocs-hosted.com/en/latest/libraries/neuronx-distributed/tutorials/training_llama2_7b.html#llama2-7b-tp-zero1-tutorial from tp_zero1_llama2_7b_hf_pretrain import train_llama # Collect command-line args def get_args(): parser = ArgumentParser() # Args expected by train_llama() parser.add_argument( "--model_path", type=str, default="/llama2_pretrain", help="Model weight and config path.", ) parser.add_argument( "--data_dir", type=str, default="/shared/wikicorpus_llama2_7B_tokenized_4k", help="Pre-tokenized dataset directory.", ) parser.add_argument( "--output_dir", type=str, default="/shared/llama2_output", help="Directory for checkpoints and logs.", ) parser.add_argument( "--metrics_file", type=str, default="results.json", help="training metrics results file", ) parser.add_argument("--batch_size", type=int, default=1, help="Worker batch size.") parser.add_argument( "--max_steps", type=int, default=10000, help="Maximum total accumulation-steps to run.", ) parser.add_argument( "--steps_this_run", type=int, default=10000, help="Exit early at <value> steps and not go to max_steps. -1 to mean no early exit.", ) parser.add_argument( "--seed", type=int, default=12349, help="Random seed. Worker seed is this value + worker rank.", ) parser.add_argument("--lr", type=float, default=3e-4, help="Learning rate.") parser.add_argument( "--warmup_steps", type=int, default=100, help="Number of warmup accumulation-steps for learning rate .", ) # grad_accum_usteps should be set to global_batch_sz / micro_batch_sz / DP_degree # where DP_degree = num_nodes * 32 / TP_degree parser.add_argument( "--grad_accum_usteps", type=int, default=32, help="Gradient accumulation micro-steps", ) parser.add_argument( "--print_grad_norm", default=False, action="store_true", help="Whether to print grad norm", ) parser.add_argument( "--resume_ckpt", action="store_true", help="Resume from checkpoint at resume_step.", ) parser.add_argument( "--tensor_parallel_size", default=8, type=int, help="Tensor parallel size" ) parser.add_argument("--seq_len", default=4096, type=int, help="Sequence length") parser.add_argument( "--use_mix_precision", action="store_true", help="Use mix precision." ) parser.add_argument("--use_zero_1", action="store_true", help="Use ZeRO-1.") parser.add_argument( "--num_layers", type=int, default=-1, help="Override number of layers for this LLaMA model to num_layers", ) parser.add_argument( "--sequence_parallel_enabled", type=bool, default=True, help="Enable/Disable sequence parallel", ) parser.add_argument( "--selective_checkpoint_enabled", type=bool, default=True, help="Enable selective checkpoint", ) parser.add_argument( "--use_meta_device_init", default=False, action="store_true", help="use meta device initialization", ) parser.add_argument( "--logging_interval", default=10, type=int, help="logging every N steps", ) parser.add_argument( "--qkv_linear", default=False, action="store_true", help="Whether to use the QKV Module", ) parser.add_argument( "--kv_replicator", default=1, type=int, help="KV replication number", ) parser.add_argument( "--checkpoint_freq", type=int, default=100000, help="save checkpoint freq" ) parser.add_argument("--checkpoint_dir", type=str, default=None) parser.add_argument( "--loading_step", type=int, default=-1, help="load from step, -1 means no load" ) parser.add_argument( "--num_kept_checkpoint", type=int, default=-1, help="number of checkpoints kept, old checkpoint will get deleted", ) # Additional args added for this Ray Train example parser.add_argument( "--neuron_parallel_compile", action="store_true", default=False, help="Enable Neuron parallel compilation to pre-populate the Neuron cache", ) parser.add_argument( "--num_nodes", type=int, default=2, help="Number of trn1 nodes to use for training", ) return parser.parse_args() if __name__ == "__main__": args = get_args() # Set up Neuron-specific env. variables to customize this training job env = { "NEURON_CC_FLAGS": "--model-type transformer --distribution-strategy=llm-training", "NEURON_FUSE_SOFTMAX": "1", "NEURON_RT_ASYNC_EXEC_MAX_INFLIGHT_REQUESTS": "3", "NEURON_RT_STOCHASTIC_ROUNDING_EN": "1", "MALLOC_ARENA_MAX": "64", "CCOM_SOCKET_IFNAME": "eth0", "NEURON_COMPILE_CACHE_URL": "/shared/neuron_cache", } if args.use_mix_precision: env["XLA_DOWNCAST_BF16"] = "1" else: env["XLA_USE_BF16"] = "1" # Configure runtime env to use Neuron env vars ray.init(runtime_env={"env_vars": env}) # Limit number of steps during neuron parallel compile runs if args.neuron_parallel_compile: args.steps_this_run = 10 trainer = TorchTrainer( train_loop_per_worker=train_llama, train_loop_config=args, torch_config=TorchXLAConfig( neuron_parallel_compile=args.neuron_parallel_compile ), scaling_config=ScalingConfig( num_workers=args.num_nodes * 32, resources_per_worker={"neuron_cores": 1} ), ) trainer.fit()
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/training/raytrain-llama2-pretrain-trn1/llama2_pretrain/logger.py
blueprints/training/raytrain-llama2-pretrain-trn1/llama2_pretrain/logger.py
import os import sys import time import inspect import requests import torch from torch.utils.tensorboard import SummaryWriter from tensorboard.backend.event_processing.event_accumulator import EventAccumulator import numpy as np def load_events(event_file): accumulator = EventAccumulator(event_file) accumulator.Reload() tags = accumulator.Tags() data = {} for tag in tags["scalars"]: data[tag] = accumulator.Scalars(tag) return data class Logger: def __init__(self, args, world_size, model_dtype): xla = "torch_xla" in sys.modules self.throughputs = [] dtype_short = model_dtype.replace("torch.", "") self.tb = SummaryWriter( os.path.join( args.output_dir, f"neuron_tblogs_{time.strftime('%m%d%y_%H%M')}" f"_{dtype_short}" f"_w{world_size}" f"_lr{args.lr}" f"_bs{args.batch_size}" f"_acc{args.grad_accum_usteps}" f"_warmup{args.warmup_steps}" f"_max{args.max_steps}" f"_xla{xla}" f"_{self.get_instance_type()}", ) ) self.tb.add_text( "script", "```\n" + inspect.getsource(sys.modules[__name__]) + "\n```", 0 ) def get_instance_type(self): try: token = requests.put( "http://169.254.169.254/latest/api/token", headers={"X-aws-ec2-metadata-token-ttl-seconds": "21600"}, ) data = requests.get( "http://169.254.169.254/latest/meta-data/instance-type", headers={"X-aws-ec2-metadata-token": token.text}, ) return data.text except: return os.environ.get("HOSTNAME", "unknown") def log(self, epoch, step, step_loss, learning_rate, throughput, grad_norm=None): time_now = time.asctime() grad_norm_msg = f"grad-norm : {grad_norm}" if grad_norm else "" print( f"LOG {time_now} - ({epoch}, {step}) step_loss : {step_loss:.4f} " f"learning_rate : {learning_rate:.2e} throughput : {throughput:.2f} " f"{grad_norm_msg}", flush=True, ) self.tb.add_scalar("step loss", step_loss, step) self.tb.add_scalar("learning rate", learning_rate, step) self.tb.add_scalar("throughput", throughput, step) if grad_norm: self.tb.add_scalar("grad-norm", grad_norm, step) self.throughputs.append(throughput)
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/training/raytrain-llama2-pretrain-trn1/llama2_pretrain/get_dataset.py
blueprints/training/raytrain-llama2-pretrain-trn1/llama2_pretrain/get_dataset.py
from datasets import load_dataset # Library for loading datasets from transformers import AutoTokenizer # Library for handling tokenization from itertools import chain # For efficiently iterating through multiple sequences import os # For file and directory operations # Configuration dataset_name = "wikicorpus" # Name of the dataset to load dataset_config_name = "raw_en" # Configuration of the dataset (e.g., language) save_path = "/shared/wikicorpus_llama2_7B_tokenized_4k" # Path to save tokenized data tokenizer_path = os.getcwd() # Path to the tokenizer model (assumed to be in the current directory) # Ensure Save Directory Exists save_path = os.path.expanduser(save_path) # Expand ~ to home directory tokenizer_path = os.path.expanduser(tokenizer_path) if not os.path.exists(save_path): # Create directory if it doesn't exist os.makedirs(save_path) block_size = 4096 # Size of text chunks for processing # Load Dataset raw_datasets = load_dataset(dataset_name, dataset_config_name) # Load Tokenizer tokenizer = AutoTokenizer.from_pretrained(tokenizer_path) # Determine Text Column Name column_names = raw_datasets["train"].column_names text_column_name = "text" if "text" in column_names else column_names[0] # Tokenization Function def tokenize_function(examples): return tokenizer(examples[text_column_name]) # Tokenize Dataset tokenized_datasets = raw_datasets.map( tokenize_function, # Apply tokenization function batched=True, # Process in batches for efficiency remove_columns=column_names, # Remove original columns after tokenization load_from_cache_file=True, # Load from cache if available desc="Running tokenizer on dataset", # Description for progress bar ) # Adjust Block Size if block_size > tokenizer.model_max_length: # Ensure block size doesn't exceed model limit print("block_size > tokenizer.model_max_length") block_size = min(block_size, tokenizer.model_max_length) # Main data processing function that will concatenate all texts from our dataset and generate chunks of block_size. def group_texts(examples): # Concatenate Texts concatenated_examples = {k: list(chain(*examples[k])) for k in examples.keys()} total_length = len(concatenated_examples[list(examples.keys())[0]]) # We drop the small remainder, and if the total_length < block_size we exclude this batch and return an empty dict. # We could add padding if the model supported it instead of this drop, you can customize this part to your needs. total_length = (total_length // block_size) * block_size # Split by chunks of max_len. result = { k: [t[i : i + block_size] for i in range(0, total_length, block_size)] for k, t in concatenated_examples.items() } result["labels"] = result["input_ids"].copy() # Labels are same as input for pretraining return result # Apply Grouping and Chunking lm_datasets = tokenized_datasets.map( group_texts, batched=True, load_from_cache_file=True, desc=f"Grouping texts in chunks of {block_size}", ) # Extract Training Dataset train_dataset = lm_datasets["train"] print(len(train_dataset)) # Save Tokenized Data train_dataset.save_to_disk(save_path)
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/nvidia-nim/nim-client/client.py
blueprints/inference/nvidia-nim/nim-client/client.py
import argparse import asyncio import time from os import system import openai async def generate(client, prompt, sampling_parameters): try: response = await client.chat.completions.create( model=FLAGS.model_name, messages=[ { "role": "system", "content": "Keep short answers of no more than 100 sentences.", }, {"role": "user", "content": prompt}, ], max_tokens=sampling_parameters["max_tokens"], temperature=float(sampling_parameters["temperature"]), top_p=float(sampling_parameters["top_p"]), n=1, stream=False, ) return response.choices[0].message.content except Exception as e: print(f"Error in generate function: {e}") return None async def process_prompt(client, prompt, prompt_id, sampling_parameters, results_dict): start_time = time.time() response = await generate(client, prompt.strip(), sampling_parameters) end_time = time.time() if response: results_dict[str(prompt_id)].append(response.encode("utf-8")) duration = end_time - start_time duration_ms = duration * 1000 print( f"Model {FLAGS.model_name} - Request {prompt_id}: {duration:.2f}s ({duration_ms:.2f}ms)" ) return end_time - start_time else: print(f"Error processing prompt {prompt_id}") return 0 async def main(FLAGS): start = time.time() sampling_parameters = { "temperature": "0.01", "top_p": "1.0", "top_k": 20, "max_tokens": 512, } client = openai.AsyncOpenAI( base_url=FLAGS.url, api_key="not_used_for_self_host", # To avoid report OPENAI_API_KEY missing ) with open(FLAGS.input_prompts, "r") as file: print(f"Loading inputs from `{FLAGS.input_prompts}`...") prompts = file.readlines() results_dict = {} total_time_sec = 0 for iter in range(FLAGS.iterations): tasks = [] for i, prompt in enumerate(prompts): prompt_id = FLAGS.offset + (len(prompts) * iter) + i results_dict[str(prompt_id)] = [] task = process_prompt( client, prompt, prompt_id, sampling_parameters, results_dict ) tasks.append(task) times = await asyncio.gather(*tasks) total_time_sec += sum(times) with open(FLAGS.results_file, "w") as file: for id in results_dict.keys(): for result in results_dict[id]: file.write(result.decode("utf-8")) file.write("\n") file.write("\n=========\n\n") print(f"Storing results into `{FLAGS.results_file}`...") if FLAGS.verbose: print(f"\nContents of `{FLAGS.results_file}` ===>") system(f"cat {FLAGS.results_file}") total_time_ms = total_time_sec * 1000 print( f"Accumulated time for all requests: {total_time_sec:.2f} seconds ({total_time_ms:.2f} milliseconds)" ) print("PASS: NVIDIA NIM example") end = time.time() actual_duration = end - start actual_duration_ms = end - start print( f"Actual execution time used with concurrency {len(tasks)} is: {actual_duration:.2f} seconds ({actual_duration_ms:.2f} milliseconds)" ) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "-u", "--url", type=str, required=False, default="http://localhost:8000/v1", help="Inference server URL. Default is http://localhost:8000/v1 ", ) parser.add_argument( "-v", "--verbose", action="store_true", required=False, default=False, help="Enable verbose output", ) parser.add_argument( "--offset", type=int, required=False, default=0, help="Add offset to request IDs used", ) parser.add_argument( "--input-prompts", type=str, required=False, default="prompts.txt", help="Text file with input prompts", ) parser.add_argument( "--results-file", type=str, required=False, default="results.txt", help="The file with output results", ) parser.add_argument( "--iterations", type=int, required=False, default=1, help="Number of iterations through the prompts file", ) parser.add_argument( "--model-name", type=str, required=False, default="meta/llama3-8b-instruct", help="Name of the model to test", ) FLAGS = parser.parse_args() asyncio.run(main(FLAGS))
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/vllm-rayserve-inf2/openai-client.py
blueprints/inference/vllm-rayserve-inf2/openai-client.py
# pip install openai # python3 openai-client.py import os from openai import OpenAI # Set up the client client = OpenAI( base_url="http://localhost:8000/v1", api_key="dummy-key" # The key doesn't matter for our custom server, but is required by the SDK ) def chat_completion_example(messages, model="NousResearch/Meta-Llama-3-8B-Instruct", temperature=0.7, max_tokens=100): try: response = client.chat.completions.create( model=model, messages=messages, temperature=temperature, max_tokens=max_tokens ) return response except Exception as e: print(f"An error occurred: {e}") return None def streaming_chat_completion_example(messages, model="NousResearch/Meta-Llama-3-8B-Instruct", temperature=0.7, max_tokens=100): try: stream = client.chat.completions.create( model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, stream=True ) for chunk in stream: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].delta.content, end="", flush=True) print() # New line after streaming is complete except Exception as e: print(f"An error occurred: {e}") # Example usage if __name__ == "__main__": # Example 1: Simple chat completion messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is the capital of India?"} ] print("Example 1 - Simple chat completion:") response = chat_completion_example(messages) if response: print(response.choices[0].message.content) print("\n") # Example 2: Chat completion with different parameters messages = [ {"role": "system", "content": "You are a creative writing assistant."}, {"role": "user", "content": "Write a short story about the starwars franchise"} ] print("Example 2 - Chat completion with different parameters:") response = chat_completion_example(messages, temperature=0.9, max_tokens=200) if response: print(response.choices[0].message.content) print("\n") # Example 3: Streaming chat completion messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Count from 1 to 10, with a brief pause between each number."} ] print("Example 3 - Streaming chat completion:") streaming_chat_completion_example(messages)
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/vllm-nvidia-triton-server-gpu/triton-client/triton-client.py
blueprints/inference/vllm-nvidia-triton-server-gpu/triton-client/triton-client.py
import argparse import asyncio import json import sys import time # Import the time module from os import system import numpy as np import tritonclient.grpc.aio as grpcclient from tritonclient.utils import * def count_tokens(text): return len(text.split()) def create_request(prompt, stream, request_id, sampling_parameters, model_name, send_parameters_as_tensor=True): inputs = [] prompt_data = np.array([prompt.encode("utf-8")], dtype=np.object_) try: inputs.append(grpcclient.InferInput("PROMPT", [1], "BYTES")) inputs[-1].set_data_from_numpy(prompt_data) except Exception as e: print(f"Encountered an error {e}") stream_data = np.array([stream], dtype=bool) inputs.append(grpcclient.InferInput("STREAM", [1], "BOOL")) inputs[-1].set_data_from_numpy(stream_data) if send_parameters_as_tensor: sampling_parameters_data = np.array( [json.dumps(sampling_parameters).encode("utf-8")], dtype=np.object_ ) inputs.append(grpcclient.InferInput("SAMPLING_PARAMETERS", [1], "BYTES")) inputs[-1].set_data_from_numpy(sampling_parameters_data) outputs = [] outputs.append(grpcclient.InferRequestedOutput("TEXT")) return { "model_name": model_name, "inputs": inputs, "outputs": outputs, "request_id": str(request_id), "parameters": sampling_parameters } async def main(FLAGS): sampling_parameters = {"temperature": "0.01", "top_p": "1.0", "top_k": 20, "max_tokens": 512} stream = FLAGS.streaming_mode model_name = FLAGS.model_name with open(FLAGS.input_prompts, "r") as file: print(f"Loading inputs from `{FLAGS.input_prompts}`...") prompts = file.readlines() results_dict = {} total_time_sec = 0 # Initialize total time in seconds async with grpcclient.InferenceServerClient( url=FLAGS.url, verbose=FLAGS.verbose ) as triton_client: async def async_request_iterator(): try: for iter in range(FLAGS.iterations): for i, prompt in enumerate(prompts): prompt_id = FLAGS.offset + (len(prompts) * iter) + i results_dict[str(prompt_id)] = [] SYSTEM_PROMPT = """<<SYS>>\nKeep short answers of no more than 100 sentences.\n<</SYS>>\n\n""" prompt = "<s>[INST]" + SYSTEM_PROMPT + prompt + "[/INST]" yield create_request( prompt, stream, prompt_id, sampling_parameters, model_name ) except Exception as error: print(f"caught error in request iterator: {error}") try: start_time = time.time() # Record the start time response_iterator = triton_client.stream_infer( inputs_iterator=async_request_iterator(), stream_timeout=FLAGS.stream_timeout, ) async for response in response_iterator: result, error = response end_time = time.time() # Record the end time if error: print(f"Encountered error while processing: {error}") else: output = result.as_numpy("TEXT") for i in output: debug = { "Prompt": prompts[int(result.get_response().id)], "Response Time": end_time - start_time, "Tokens": count_tokens(i.decode('utf-8')), "Response": i.decode('utf-8'), } results_dict[result.get_response().id] = debug duration = (end_time - start_time) # Calculate the duration in seconds total_time_sec += (end_time - start_time) # Add duration to total time in seconds print(f"Model {FLAGS.model_name} - Request {result.get_response().id}: {duration:.2f} seconds") except InferenceServerException as error: print(error) sys.exit(1) with open(FLAGS.results_file, "w") as file: for key, val in results_dict.items(): file.write( f"Prompt: {val['Prompt']}\nResponse Time: {val['Response Time']}\nTokens: {val['Tokens']}\nResponse: {val['Response']}") file.write("\n=========\n\n") print(f"Storing results into `{FLAGS.results_file}`...") if FLAGS.verbose: print(f"\nContents of `{FLAGS.results_file}` ===>") system(f"cat {FLAGS.results_file}") total_time_ms = total_time_sec # Convert total time to milliseconds print(f"Total time for all requests: {total_time_sec:.2f} seconds ({total_time_ms:.2f} seconds)") print("PASS: vLLM example") if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "-v", "--verbose", action="store_true", required=False, default=False, help="Enable verbose output", ) parser.add_argument( "-u", "--url", type=str, required=False, default="localhost:8001", help="Inference server URL and it gRPC port. Default is localhost:8001.", ) parser.add_argument( "-t", "--stream-timeout", type=float, required=False, default=None, help="Stream timeout in seconds. Default is None.", ) parser.add_argument( "--offset", type=int, required=False, default=0, help="Add offset to request IDs used", ) parser.add_argument( "--input-prompts", type=str, required=False, default="prompts.txt", help="Text file with input prompts", ) parser.add_argument( "--results-file", type=str, required=False, default="results.txt", help="The file with output results", ) parser.add_argument( "--iterations", type=int, required=False, default=1, help="Number of iterations through the prompts file", ) parser.add_argument( "-s", "--streaming-mode", action="store_true", required=False, default=False, help="Enable streaming mode", ) parser.add_argument( "--model-name", type=str, required=True, help="Name of the model to test", ) FLAGS = parser.parse_args() asyncio.run(main(FLAGS))
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/vllm-nvidia-triton-server-gpu/model_repository/llama3/1/model.py
blueprints/inference/vllm-nvidia-triton-server-gpu/model_repository/llama3/1/model.py
# Reference from https://github.com/triton-inference-server/vllm_backend import asyncio import json import os import threading from typing import AsyncGenerator import numpy as np import triton_python_backend_utils as pb_utils from vllm import SamplingParams from vllm.engine.arg_utils import AsyncEngineArgs from vllm.engine.async_llm_engine import AsyncLLMEngine from vllm.utils import random_uuid import huggingface_hub # Environment and configuration setup _VLLM_ENGINE_ARGS_FILENAME = "vllm_engine_args.json" huggingface_hub.login(token=os.environ.get("HUGGING_FACE_TOKEN", "")) class TritonPythonModel: # initialize method sets up the model configuration, initializes the vLLM engine with the provided parameters, # and starts an asynchronous event loop to process requests. def initialize(self, args): self.logger = pb_utils.Logger self.model_config = json.loads(args["model_config"]) # assert are in decoupled mode. Currently, Triton needs to use # decoupled policy for asynchronously forwarding requests to # vLLM engine. self.using_decoupled = pb_utils.using_decoupled_model_transaction_policy( self.model_config ) assert ( self.using_decoupled ), "vLLM Triton backend must be configured to use decoupled model transaction policy" # Load engine arguments from the model repository engine_args_filepath = os.path.join( args["model_repository"], _VLLM_ENGINE_ARGS_FILENAME ) # GPU ID gpu_id = args.get("model_instance_device_id", "2") os.environ["CUDA_VISIBLE_DEVICES"] = gpu_id vllm_engine_config = { "model": os.environ.get( "model_name", "meta-llama/Meta-Llama-3-8B-Instruct" ), "disable_log_requests": "true", "tensor_parallel_size": int(os.environ.get("tensor_parallel_size", 1)), "gpu_memory_utilization": float( os.environ.get("gpu_memory_utilization", "0.8") ), "dtype": os.environ.get("dtype", "auto"), "max_model_len": int(os.environ.get("max_model_len", 4096)), "enforce_eager": True, } # Create an AsyncLLMEngine from the config from JSON self.llm_engine = AsyncLLMEngine.from_engine_args( AsyncEngineArgs(**vllm_engine_config) ) output_config = pb_utils.get_output_config_by_name(self.model_config, "TEXT") self.output_dtype = pb_utils.triton_string_to_numpy(output_config["data_type"]) # Counter to keep track of ongoing request counts self.ongoing_request_count = 0 # Starting asyncio event loop to process the received requests asynchronously. self._loop = asyncio.get_event_loop() self._loop_thread = threading.Thread( target=self.engine_loop, args=(self._loop,) ) self._shutdown_event = asyncio.Event() self._loop_thread.start() def create_task(self, coro): """ The create_task method schedules asynchronous tasks on the event loop running in a separate thread. """ assert ( self._shutdown_event.is_set() is False ), "Cannot create tasks after shutdown has been requested" return asyncio.run_coroutine_threadsafe(coro, self._loop) def engine_loop(self, loop): """ Runs the engine's event loop on a separate thread. """ asyncio.set_event_loop(loop) self._loop.run_until_complete(self.await_shutdown()) async def await_shutdown(self): """ Primary coroutine running on the engine event loop. This coroutine is responsible for keeping the engine alive until a shutdown is requested. """ # first await the shutdown signal while self._shutdown_event.is_set() is False: await asyncio.sleep(5) # Wait for the ongoing_requests while self.ongoing_request_count > 0: self.logger.log_info( "Awaiting remaining {} requests".format(self.ongoing_request_count) ) await asyncio.sleep(5) self.logger.log_info("Shutdown complete") def get_sampling_params_dict(self, params_json): """ This functions parses the dictionary values into their expected format. """ params_dict = json.loads(params_json) # Special parsing for the supported sampling parameters bool_keys = ["ignore_eos", "skip_special_tokens", "use_beam_search"] for k in bool_keys: if k in params_dict: params_dict[k] = bool(params_dict[k]) float_keys = [ "frequency_penalty", "length_penalty", "presence_penalty", "temperature", "top_p", ] for k in float_keys: if k in params_dict: params_dict[k] = float(params_dict[k]) int_keys = ["best_of", "max_tokens", "n", "top_k"] for k in int_keys: if k in params_dict: params_dict[k] = int(params_dict[k]) return params_dict def create_response(self, vllm_output): """ Responses are created using the create_response method and sent back to Triton. """ prompt = vllm_output.prompt text_outputs = [ (prompt + output.text).encode("utf-8") for output in vllm_output.outputs ] triton_output_tensor = pb_utils.Tensor( "TEXT", np.asarray(text_outputs, dtype=self.output_dtype) ) return pb_utils.InferenceResponse(output_tensors=[triton_output_tensor]) async def generate(self, request): """ Generate method forwards the input prompt to the vLLM engine and collects the output. """ response_sender = request.get_response_sender() self.ongoing_request_count += 1 try: request_id = random_uuid() prompt = pb_utils.get_input_tensor_by_name(request, "PROMPT").as_numpy()[0] if isinstance(prompt, bytes): prompt = prompt.decode("utf-8") stream = pb_utils.get_input_tensor_by_name(request, "STREAM").as_numpy()[0] parameters_input_tensor = pb_utils.get_input_tensor_by_name( request, "SAMPLING_PARAMETERS" ) if parameters_input_tensor: parameters = parameters_input_tensor.as_numpy()[0].decode("utf-8") else: parameters = request.parameters() sampling_params_dict = self.get_sampling_params_dict(parameters) sampling_params = SamplingParams(**sampling_params_dict) last_output = None async for output in self.llm_engine.generate( prompt, sampling_params, request_id ): if stream: response_sender.send(self.create_response(output)) else: last_output = output if not stream: response_sender.send(self.create_response(last_output)) except Exception as e: self.logger.log_info(f"Error generating stream: {e}") error = pb_utils.TritonError(f"Error generating stream: {e}") triton_output_tensor = pb_utils.Tensor( "TEXT", np.asarray(["N/A"], dtype=self.output_dtype) ) response = pb_utils.InferenceResponse( output_tensors=[triton_output_tensor], error=error ) response_sender.send(response) raise e finally: response_sender.send(flags=pb_utils.TRITONSERVER_RESPONSE_COMPLETE_FINAL) self.ongoing_request_count -= 1 def execute(self, requests): """ Triton core issues requests to the backend via this method. When this method returns, new requests can be issued to the backend. Blocking this function would prevent the backend from pulling additional requests from Triton into the vLLM engine. This can be done if the kv cache within vLLM engine is too loaded. We are pushing all the requests on mistral7b and let it handle the full traffic. """ for request in requests: self.create_task(self.generate(request)) return None def finalize(self): """ Triton virtual method; called when the model is unloaded. """ self.logger.log_info("Issuing finalize to mistral7b backend") self._shutdown_event.set() if self._loop_thread is not None: self._loop_thread.join() self._loop_thread = None
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/vllm-nvidia-triton-server-gpu/model_repository/mistral7b/1/model.py
blueprints/inference/vllm-nvidia-triton-server-gpu/model_repository/mistral7b/1/model.py
# Reference from https://github.com/triton-inference-server/vllm_backend import asyncio import json import os import threading from typing import AsyncGenerator import numpy as np import triton_python_backend_utils as pb_utils from vllm import SamplingParams from vllm.engine.arg_utils import AsyncEngineArgs from vllm.engine.async_llm_engine import AsyncLLMEngine from vllm.utils import random_uuid import huggingface_hub # Environment and configuration setup _VLLM_ENGINE_ARGS_FILENAME = "vllm_engine_args.json" huggingface_hub.login(token=os.environ.get("HUGGING_FACE_TOKEN", "")) class TritonPythonModel: # initialize method sets up the model configuration, initializes the vLLM engine with the provided parameters, # and starts an asynchronous event loop to process requests. def initialize(self, args): self.logger = pb_utils.Logger self.model_config = json.loads(args["model_config"]) # assert are in decoupled mode. Currently, Triton needs to use # decoupled policy for asynchronously forwarding requests to # vLLM engine. self.using_decoupled = pb_utils.using_decoupled_model_transaction_policy( self.model_config ) assert ( self.using_decoupled ), "vLLM Triton backend must be configured to use decoupled model transaction policy" # Load engine arguments from the model repository engine_args_filepath = os.path.join( args["model_repository"], _VLLM_ENGINE_ARGS_FILENAME ) # GPU ID gpu_id = args.get("model_instance_device_id", "0") os.environ["CUDA_VISIBLE_DEVICES"] = gpu_id vllm_engine_config = { "model": "mistralai/Mistral-7B-Instruct-v0.2", "disable_log_requests": "true", "tensor_parallel_size": int(os.environ.get("tensor_parallel_size", 1)), "gpu_memory_utilization": float( os.environ.get("gpu_memory_utilization", "0.8") ), "dtype": os.environ.get("dtype", "auto"), "max_model_len": int(os.environ.get("max_model_len", 4096)), "enforce_eager": True, } # Create an AsyncLLMEngine from the config from JSON self.llm_engine = AsyncLLMEngine.from_engine_args( AsyncEngineArgs(**vllm_engine_config) ) output_config = pb_utils.get_output_config_by_name(self.model_config, "TEXT") self.output_dtype = pb_utils.triton_string_to_numpy(output_config["data_type"]) # Counter to keep track of ongoing request counts self.ongoing_request_count = 0 # Starting asyncio event loop to process the received requests asynchronously. self._loop = asyncio.get_event_loop() self._loop_thread = threading.Thread( target=self.engine_loop, args=(self._loop,) ) self._shutdown_event = asyncio.Event() self._loop_thread.start() def create_task(self, coro): """ The create_task method schedules asynchronous tasks on the event loop running in a separate thread. """ assert ( self._shutdown_event.is_set() is False ), "Cannot create tasks after shutdown has been requested" return asyncio.run_coroutine_threadsafe(coro, self._loop) def engine_loop(self, loop): """ Runs the engine's event loop on a separate thread. """ asyncio.set_event_loop(loop) self._loop.run_until_complete(self.await_shutdown()) async def await_shutdown(self): """ Primary coroutine running on the engine event loop. This coroutine is responsible for keeping the engine alive until a shutdown is requested. """ # first await the shutdown signal while self._shutdown_event.is_set() is False: await asyncio.sleep(5) # Wait for the ongoing_requests while self.ongoing_request_count > 0: self.logger.log_info( "Awaiting remaining {} requests".format(self.ongoing_request_count) ) await asyncio.sleep(5) self.logger.log_info("Shutdown complete") def get_sampling_params_dict(self, params_json): """ This functions parses the dictionary values into their expected format. """ params_dict = json.loads(params_json) # Special parsing for the supported sampling parameters bool_keys = ["ignore_eos", "skip_special_tokens", "use_beam_search"] for k in bool_keys: if k in params_dict: params_dict[k] = bool(params_dict[k]) float_keys = [ "frequency_penalty", "length_penalty", "presence_penalty", "temperature", "top_p", ] for k in float_keys: if k in params_dict: params_dict[k] = float(params_dict[k]) int_keys = ["best_of", "max_tokens", "n", "top_k"] for k in int_keys: if k in params_dict: params_dict[k] = int(params_dict[k]) return params_dict def create_response(self, vllm_output): """ Responses are created using the create_response method and sent back to Triton. """ prompt = vllm_output.prompt text_outputs = [ (prompt + output.text).encode("utf-8") for output in vllm_output.outputs ] triton_output_tensor = pb_utils.Tensor( "TEXT", np.asarray(text_outputs, dtype=self.output_dtype) ) return pb_utils.InferenceResponse(output_tensors=[triton_output_tensor]) async def generate(self, request): """ Generate method forwards the input prompt to the vLLM engine and collects the output. """ response_sender = request.get_response_sender() self.ongoing_request_count += 1 try: request_id = random_uuid() prompt = pb_utils.get_input_tensor_by_name(request, "PROMPT").as_numpy()[0] if isinstance(prompt, bytes): prompt = prompt.decode("utf-8") stream = pb_utils.get_input_tensor_by_name(request, "STREAM").as_numpy()[0] parameters_input_tensor = pb_utils.get_input_tensor_by_name( request, "SAMPLING_PARAMETERS" ) if parameters_input_tensor: parameters = parameters_input_tensor.as_numpy()[0].decode("utf-8") else: parameters = request.parameters() sampling_params_dict = self.get_sampling_params_dict(parameters) sampling_params = SamplingParams(**sampling_params_dict) last_output = None async for output in self.llm_engine.generate( prompt, sampling_params, request_id ): if stream: response_sender.send(self.create_response(output)) else: last_output = output if not stream: response_sender.send(self.create_response(last_output)) except Exception as e: self.logger.log_info(f"Error generating stream: {e}") error = pb_utils.TritonError(f"Error generating stream: {e}") triton_output_tensor = pb_utils.Tensor( "TEXT", np.asarray(["N/A"], dtype=self.output_dtype) ) response = pb_utils.InferenceResponse( output_tensors=[triton_output_tensor], error=error ) response_sender.send(response) raise e finally: response_sender.send(flags=pb_utils.TRITONSERVER_RESPONSE_COMPLETE_FINAL) self.ongoing_request_count -= 1 def execute(self, requests): """ Triton core issues requests to the backend via this method. When this method returns, new requests can be issued to the backend. Blocking this function would prevent the backend from pulling additional requests from Triton into the vLLM engine. This can be done if the kv cache within vLLM engine is too loaded. We are pushing all the requests on mistral7b and let it handle the full traffic. """ for request in requests: self.create_task(self.generate(request)) return None def finalize(self): """ Triton virtual method; called when the model is unloaded. """ self.logger.log_info("Issuing finalize to mistral7b backend") self._shutdown_event.set() if self._loop_thread is not None: self._loop_thread.join() self._loop_thread = None
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/vllm-nvidia-triton-server-gpu/model_repository/llama2/1/model.py
blueprints/inference/vllm-nvidia-triton-server-gpu/model_repository/llama2/1/model.py
# Reference from https://github.com/triton-inference-server/vllm_backend import asyncio import json import os import threading from typing import AsyncGenerator import numpy as np import triton_python_backend_utils as pb_utils from vllm import SamplingParams from vllm.engine.arg_utils import AsyncEngineArgs from vllm.engine.async_llm_engine import AsyncLLMEngine from vllm.utils import random_uuid import huggingface_hub # Environment and configuration setup _VLLM_ENGINE_ARGS_FILENAME = "vllm_engine_args.json" huggingface_hub.login(token=os.environ.get("HUGGING_FACE_TOKEN", "")) class TritonPythonModel: # initialize method sets up the model configuration, initializes the vLLM engine with the provided parameters, # and starts an asynchronous event loop to process requests. def initialize(self, args): self.logger = pb_utils.Logger self.model_config = json.loads(args["model_config"]) # assert are in decoupled mode. Currently, Triton needs to use # decoupled policy for asynchronously forwarding requests to # vLLM engine. self.using_decoupled = pb_utils.using_decoupled_model_transaction_policy( self.model_config ) assert ( self.using_decoupled ), "vLLM Triton backend must be configured to use decoupled model transaction policy" # Load engine arguments from the model repository engine_args_filepath = os.path.join( args["model_repository"], _VLLM_ENGINE_ARGS_FILENAME ) # GPU ID gpu_id = args.get("model_instance_device_id", "1") os.environ["CUDA_VISIBLE_DEVICES"] = gpu_id vllm_engine_config = { "model": os.environ.get("model_name", "meta-llama/Llama-2-7b-chat-hf"), "disable_log_requests": "true", "tensor_parallel_size": int(os.environ.get("tensor_parallel_size", 1)), "gpu_memory_utilization": float( os.environ.get("gpu_memory_utilization", "0.8") ), "dtype": os.environ.get("dtype", "auto"), "max_model_len": int(os.environ.get("max_model_len", 4096)), "enforce_eager": True, } # Create an AsyncLLMEngine from the config from JSON self.llm_engine = AsyncLLMEngine.from_engine_args( AsyncEngineArgs(**vllm_engine_config) ) output_config = pb_utils.get_output_config_by_name(self.model_config, "TEXT") self.output_dtype = pb_utils.triton_string_to_numpy(output_config["data_type"]) # Counter to keep track of ongoing request counts self.ongoing_request_count = 0 # Starting asyncio event loop to process the received requests asynchronously. self._loop = asyncio.get_event_loop() self._loop_thread = threading.Thread( target=self.engine_loop, args=(self._loop,) ) self._shutdown_event = asyncio.Event() self._loop_thread.start() def create_task(self, coro): """ The create_task method schedules asynchronous tasks on the event loop running in a separate thread. """ assert ( self._shutdown_event.is_set() is False ), "Cannot create tasks after shutdown has been requested" return asyncio.run_coroutine_threadsafe(coro, self._loop) def engine_loop(self, loop): """ Runs the engine's event loop on a separate thread. """ asyncio.set_event_loop(loop) self._loop.run_until_complete(self.await_shutdown()) async def await_shutdown(self): """ Primary coroutine running on the engine event loop. This coroutine is responsible for keeping the engine alive until a shutdown is requested. """ # first await the shutdown signal while self._shutdown_event.is_set() is False: await asyncio.sleep(5) # Wait for the ongoing_requests while self.ongoing_request_count > 0: self.logger.log_info( "Awaiting remaining {} requests".format(self.ongoing_request_count) ) await asyncio.sleep(5) self.logger.log_info("Shutdown complete") def get_sampling_params_dict(self, params_json): """ This functions parses the dictionary values into their expected format. """ params_dict = json.loads(params_json) # Special parsing for the supported sampling parameters bool_keys = ["ignore_eos", "skip_special_tokens", "use_beam_search"] for k in bool_keys: if k in params_dict: params_dict[k] = bool(params_dict[k]) float_keys = [ "frequency_penalty", "length_penalty", "presence_penalty", "temperature", "top_p", ] for k in float_keys: if k in params_dict: params_dict[k] = float(params_dict[k]) int_keys = ["best_of", "max_tokens", "n", "top_k"] for k in int_keys: if k in params_dict: params_dict[k] = int(params_dict[k]) return params_dict def create_response(self, vllm_output): """ Responses are created using the create_response method and sent back to Triton. """ prompt = vllm_output.prompt text_outputs = [ (prompt + output.text).encode("utf-8") for output in vllm_output.outputs ] triton_output_tensor = pb_utils.Tensor( "TEXT", np.asarray(text_outputs, dtype=self.output_dtype) ) return pb_utils.InferenceResponse(output_tensors=[triton_output_tensor]) async def generate(self, request): """ Generate method forwards the input prompt to the vLLM engine and collects the output. """ response_sender = request.get_response_sender() self.ongoing_request_count += 1 try: request_id = random_uuid() prompt = pb_utils.get_input_tensor_by_name(request, "PROMPT").as_numpy()[0] if isinstance(prompt, bytes): prompt = prompt.decode("utf-8") stream = pb_utils.get_input_tensor_by_name(request, "STREAM").as_numpy()[0] parameters_input_tensor = pb_utils.get_input_tensor_by_name( request, "SAMPLING_PARAMETERS" ) if parameters_input_tensor: parameters = parameters_input_tensor.as_numpy()[0].decode("utf-8") else: parameters = request.parameters() sampling_params_dict = self.get_sampling_params_dict(parameters) sampling_params = SamplingParams(**sampling_params_dict) last_output = None async for output in self.llm_engine.generate( prompt, sampling_params, request_id ): if stream: response_sender.send(self.create_response(output)) else: last_output = output if not stream: response_sender.send(self.create_response(last_output)) except Exception as e: self.logger.log_info(f"Error generating stream: {e}") error = pb_utils.TritonError(f"Error generating stream: {e}") triton_output_tensor = pb_utils.Tensor( "TEXT", np.asarray(["N/A"], dtype=self.output_dtype) ) response = pb_utils.InferenceResponse( output_tensors=[triton_output_tensor], error=error ) response_sender.send(response) raise e finally: response_sender.send(flags=pb_utils.TRITONSERVER_RESPONSE_COMPLETE_FINAL) self.ongoing_request_count -= 1 def execute(self, requests): """ Triton core issues requests to the backend via this method. When this method returns, new requests can be issued to the backend. Blocking this function would prevent the backend from pulling additional requests from Triton into the vLLM engine. This can be done if the kv cache within vLLM engine is too loaded. We are pushing all the requests on mistral7b and let it handle the full traffic. """ for request in requests: self.create_task(self.generate(request)) return None def finalize(self): """ Triton virtual method; called when the model is unloaded. """ self.logger.log_info("Issuing finalize to mistral7b backend") self._shutdown_event.set() if self._loop_thread is not None: self._loop_thread.join() self._loop_thread = None
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/stable-diffusion-xl-base-rayserve-inf2/ray_serve_stablediffusion.py
blueprints/inference/stable-diffusion-xl-base-rayserve-inf2/ray_serve_stablediffusion.py
from io import BytesIO from fastapi import FastAPI from fastapi.responses import Response import os import base64 from ray import serve app = FastAPI() neuron_cores = 2 @serve.deployment(name="stable-diffusion-api", num_replicas=1, route_prefix="/") @serve.ingress(app) class APIIngress: def __init__(self, diffusion_model_handle) -> None: self.handle = diffusion_model_handle @app.get( "/imagine", responses={200: {"content": {"image/png": {}}}}, response_class=Response, ) async def generate(self, prompt: str): image = await self.handle.generate.remote(prompt) file_stream = BytesIO() image.save(file_stream, "PNG") return Response(content=file_stream.getvalue(), media_type="image/png") @serve.deployment(name="stable-diffusion-v2", autoscaling_config={"min_replicas": 0, "max_replicas": 6}, ray_actor_options={ "resources": {"neuron_cores": neuron_cores}, "runtime_env": {"env_vars": {"NEURON_CC_FLAGS": "-O1"}}, }, ) class StableDiffusionV2: def __init__(self): from optimum.neuron import NeuronStableDiffusionXLPipeline model_id = os.getenv('MODEL_ID') # To avoid saving the model locally, we can use the pre-compiled model directly from HF self.pipe = NeuronStableDiffusionXLPipeline.from_pretrained(model_id, device_ids=[0, 1]) async def generate(self, prompt: str): assert len(prompt), "prompt parameter cannot be empty" image = self.pipe(prompt).images[0] return image entrypoint = APIIngress.bind(StableDiffusionV2.bind())
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/gradio-ui/gradio-app-llama.py
blueprints/inference/gradio-ui/gradio-app-llama.py
import gradio as gr import requests import os # Constants for model endpoint and service name model_endpoint = "/infer" # service_name = "http://<REPLACE_ME_WITH_ELB_DNS_NAME>/serve" service_name = os.environ.get("SERVICE_NAME", "http://localhost:8000") # Function to generate text def text_generation(message, history): prompt = message # Create the URL for the inference url = f"{service_name}{model_endpoint}" try: # Send the request to the model service response = requests.get(url, params={"sentence": prompt}, timeout=180) response.raise_for_status() # Raise an exception for HTTP errors full_output = response.json()[0] # Removing the original question from the output answer_only = full_output.replace(prompt, "", 1).strip('["]?\n') # Safety filter to remove harmful or inappropriate content answer_only = filter_harmful_content(answer_only) return answer_only except requests.exceptions.RequestException as e: # Handle any request exceptions (e.g., connection errors) return f"AI: Error: {str(e)}" # Define the safety filter function (you can implement this as needed) def filter_harmful_content(text): # TODO: Implement a safety filter to remove any harmful or inappropriate content from the text # For now, simply return the text as-is return text # Define the Gradio ChatInterface chat_interface = gr.ChatInterface( text_generation, chatbot=gr.Chatbot(line_breaks=True), textbox=gr.Textbox(placeholder="Ask me a question", container=False, scale=7), title="Llama2/3 AI Chat", description="Ask me any question", theme="soft", examples=["How many languages are in India", "What is Generative AI?"], cache_examples=False, retry_btn=None, undo_btn="Delete Previous", clear_btn="Clear", ) # Launch the ChatInterface chat_interface.launch(server_name="0.0.0.0")
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/gradio-ui/gradio-app-stable-diffusion.py
blueprints/inference/gradio-ui/gradio-app-stable-diffusion.py
import gradio as gr import requests import os from PIL import Image from io import BytesIO # Constants for model endpoint and service name model_endpoint = os.environ.get("MODEL_ENDPOINT", "/imagine") service_name = os.environ.get("SERVICE_NAME", "http://localhost:8000") # Function to generate image based on prompt def generate_image(prompt): # Create the URL for the inference url = f"{service_name}{model_endpoint}" try: # Send the request to the model service response = requests.get(url, params={"prompt": prompt}, timeout=180) response.raise_for_status() # Raise an exception for HTTP errors i = Image.open(BytesIO(response.content)) return i except requests.exceptions.RequestException as e: # Handle any request exceptions (e.g., connection errors) # return f"AI: Error: {str(e)}" return Image.new('RGB', (100, 100), color='red') # Define the Gradio PromptInterface demo = gr.Interface(fn=generate_image, inputs=[gr.Textbox(label="Enter the Prompt")], outputs=gr.Image(type='pil'), ) # Launch demo.launch(server_name="0.0.0.0")
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/vllm-rayserve-gpu/client.py
blueprints/inference/vllm-rayserve-gpu/client.py
import aiohttp import asyncio import os import time import logging import csv from uuid import uuid4 as random_uuid import json # Setup logging configuration logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Constants for model endpoint and service name model_endpoint = os.getenv("MODEL_ENDPOINT", "/vllm") service_name = os.getenv("SERVICE_NAME", "http://localhost:8000") # Function to count tokens in a response def count_tokens(text): return len(text.split()) # Function to generate text asynchronously async def generate_text(session, prompt): SYSTEM_PROMPT = """<<SYS>>\nKeep short answers of no more than 100 sentences.\n<</SYS>>\n\n""" payload = { "prompt": "<s>[INST]" + SYSTEM_PROMPT + prompt + "[/INST]", "stream": False, "max_tokens": 512, # Increased to allow for longer responses "temperature": 0.01, # Adjusted for balanced responses "top_p": 1, # Adjusted for balanced responses "top_k": 20, # Increased for more diversity in responses "stop": None } # Create the URL for the inference url = f"{service_name}{model_endpoint}" try: # Measure the start time for the inference start_time = time.perf_counter() # Send the request to the model service async with session.post(url, json=payload, timeout=180) as response: # Measure the end time for the inference end_time = time.perf_counter() latency = end_time - start_time # Log response status and content type logger.info(f"Response status: {response.status}") if response.status == 200: if response.content_type == 'application/json': response_data = await response.json() elif response.content_type == 'application/octet-stream': response_data = await response.read() response_data = response_data.decode('utf-8') response_data = json.loads(response_data) else: logger.error(f"Unexpected content type: {response.content_type}") return None, latency, 0 text = response_data["text"][0].strip() num_tokens = count_tokens(text) return text, latency, num_tokens else: # Print the error message logger.error(f"Failed to get response from model. Status code: {response.status}") logger.error(f"Error message: {await response.text()}") return None, latency, 0 except aiohttp.ClientError as e: # Handle any request exceptions (e.g., connection errors) logger.error(f"Request exception: {str(e)}") return None, None, 0 # Function to warm up the model async def warmup(session): """Warm up the model to reduce cold start latency.""" prompt = "Warmup" payload = { "prompt": prompt, "stream": False, "max_tokens": 1, "temperature": 0.7, "top_p": 0.9, "top_k": 50, "stop": None } url = f"{service_name}{model_endpoint}" try: # Send the warm-up request to the model service async with session.post(url, json=payload, timeout=180) as response: if response.status == 200: logger.info("Warm-up successful") else: logger.error(f"Failed to warm up model. Status code: {response.status}") logger.error(f"Error message: {await response.text()}") except aiohttp.ClientError as e: logger.error(f"Warm-up request exception: {str(e)}") # Function to read prompts from a file def read_prompts(file_path): with open(file_path, 'r') as file: return [line.strip() for line in file.readlines()] # Function to write results to a file def write_results(file_path, results, summary): with open(file_path, 'w') as file: # Write each request and response with detailed information for result in results: prompt, latency, response_text, num_tokens = result file.write(f"Prompt: {prompt}\n") file.write(f"Response Time: {latency:.2f} seconds\n") file.write(f"Token Length: {num_tokens}\n") file.write(f"Response: {response_text}\n") file.write("=" * 80 + "\n") # Write summary file.write("\nSummary of Latency:\n") file.write("Total Prompts: {}\n".format(len(results))) file.write("Average Latency: {:.2f} seconds\n".format(summary['average_latency'])) file.write("Max Latency: {:.2f} seconds\n".format(summary['max_latency'])) file.write("Min Latency: {:.2f} seconds\n".format(summary['min_latency'])) # Main function to handle asynchronous execution async def main(): prompts = read_prompts('prompts.txt') # List to store results results = [] total_latency = 0 max_latency = float('-inf') min_latency = float('inf') async with aiohttp.ClientSession() as session: # Warm up the model await warmup(session) tasks = [generate_text(session, prompt) for prompt in prompts] responses = await asyncio.gather(*tasks) for prompt, (response_text, latency, num_tokens) in zip(prompts, responses): if latency is not None: results.append([prompt, latency, response_text, num_tokens]) total_latency += latency max_latency = max(max_latency, latency) min_latency = min(min_latency, latency) print(f"Prompt: {prompt}") print(f"Response Time: {latency:.2f} seconds") print(f"Token Length: {num_tokens}") print("=" * 80) else: results.append([prompt, "N/A", response_text, 0]) print(f"Prompt: {prompt}") print(f"Response Time: N/A") print(f"Token Length: 0") print("=" * 80) # Calculate summary valid_latencies = [r[1] for r in results if isinstance(r[1], float)] if valid_latencies: summary = { 'average_latency': sum(valid_latencies) / len(valid_latencies), 'max_latency': max(valid_latencies), 'min_latency': min(valid_latencies) } # Write results and summary to the local file write_results('results.txt', results, summary) # Run the main function if __name__ == "__main__": asyncio.run(main())
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/vllm-rayserve-gpu/vllm_serve.py
blueprints/inference/vllm-rayserve-gpu/vllm_serve.py
import json from typing import AsyncGenerator from fastapi import BackgroundTasks from starlette.requests import Request from starlette.responses import StreamingResponse, Response, JSONResponse from vllm.engine.arg_utils import AsyncEngineArgs from vllm.engine.async_llm_engine import AsyncLLMEngine from vllm.sampling_params import SamplingParams from vllm.utils import random_uuid from ray import serve import os import logging from huggingface_hub import login # Environment and configuration setup logger = logging.getLogger("ray.serve") @serve.deployment(name="mistral-deployment", route_prefix="/vllm", ray_actor_options={"num_gpus": 1}, autoscaling_config={"min_replicas": 1, "max_replicas": 2}, ) class VLLMDeployment: def __init__(self, **kwargs): hf_token = os.getenv("HUGGING_FACE_HUB_TOKEN") logger.info(f"token: {hf_token=}") if not hf_token: raise ValueError("HUGGING_FACE_HUB_TOKEN environment variable is not set") login(token=hf_token) logger.info("Successfully logged in to Hugging Face Hub") args = AsyncEngineArgs( model=os.getenv("MODEL_ID", "mistralai/Mistral-7B-Instruct-v0.2"), # Model identifier from Hugging Face Hub or local path. dtype="auto", # Automatically determine the data type (e.g., float16 or float32) for model weights and computations. gpu_memory_utilization=float(os.getenv("GPU_MEMORY_UTILIZATION", "0.8")), # Percentage of GPU memory to utilize, reserving some for overhead. max_model_len=int(os.getenv("MAX_MODEL_LEN", "4096")), # Maximum sequence length (in tokens) the model can handle, including both input and output tokens. max_num_seqs=int(os.getenv("MAX_NUM_SEQ", "512")), # Maximum number of sequences (requests) to process in parallel. max_num_batched_tokens=int(os.getenv("MAX_NUM_BATCHED_TOKENS", "32768")), # Maximum number of tokens processed in a single batch across all sequences (max_model_len * max_num_seqs). trust_remote_code=True, # Allow execution of untrusted code from the model repository (use with caution). enable_chunked_prefill=False, # Disable chunked prefill to avoid compatibility issues with prefix caching. tokenizer_pool_size=4, # Number of tokenizer instances to handle concurrent requests efficiently. tokenizer_pool_type="ray", # Pool type for tokenizers; 'ray' uses Ray for distributed processing. max_parallel_loading_workers=2, # Number of parallel workers to load the model concurrently. pipeline_parallel_size=1, # Number of pipeline parallelism stages; typically set to 1 unless using model parallelism. tensor_parallel_size=1, # Number of tensor parallelism stages; typically set to 1 unless using model parallelism. enable_prefix_caching=True, # Enable prefix caching to improve performance for similar prompt prefixes. enforce_eager=True, disable_log_requests=True, ) self.engine = AsyncLLMEngine.from_engine_args(args) self.max_model_len = args.max_model_len logger.info(f"VLLM Engine initialized with max_model_len: {self.max_model_len}") async def stream_results(self, results_generator) -> AsyncGenerator[bytes, None]: num_returned = 0 async for request_output in results_generator: text_outputs = [output.text for output in request_output.outputs] assert len(text_outputs) == 1 text_output = text_outputs[0][num_returned:] ret = {"text": text_output} yield (json.dumps(ret) + "\n").encode("utf-8") num_returned += len(text_output) async def may_abort_request(self, request_id) -> None: await self.engine.abort(request_id) async def __call__(self, request: Request) -> Response: try: request_dict = await request.json() except json.JSONDecodeError: return JSONResponse(status_code=400, content={"error": "Invalid JSON in request body"}) context_length = request_dict.pop("context_length", 8192) # Default to 8k # Ensure context length is either 8k or 32k if context_length not in [8192, 32768]: context_length = 8192 # Default to 8k if invalid prompt = request_dict.pop("prompt") stream = request_dict.pop("stream", False) # Get model config and tokenizer model_config = await self.engine.get_model_config() tokenizer = await self.engine.get_tokenizer() input_token_ids = tokenizer.encode(prompt) input_tokens = len(input_token_ids) max_possible_new_tokens = min(context_length, model_config.max_model_len) - input_tokens max_new_tokens = min(request_dict.get("max_tokens", 8192), max_possible_new_tokens) sampling_params = SamplingParams( max_tokens=max_new_tokens, temperature=request_dict.get("temperature", 0.7), top_p=request_dict.get("top_p", 0.9), top_k=request_dict.get("top_k", 50), stop=request_dict.get("stop", None), ) request_id = random_uuid() logger.info(f"Processing request {request_id} with {input_tokens} input tokens") results_generator = self.engine.generate(prompt, sampling_params, request_id) if stream: background_tasks = BackgroundTasks() # Using background_tasks to abort the request # if the client disconnects. background_tasks.add_task(self.may_abort_request, request_id) return StreamingResponse( self.stream_results(results_generator), background=background_tasks ) # Non-streaming case final_output = None async for request_output in results_generator: if await request.is_disconnected(): # Abort the request if the client disconnects. await self.engine.abort(request_id) logger.warning(f"Client disconnected for request {request_id}") return Response(status_code=499) final_output = request_output assert final_output is not None prompt = final_output.prompt text_outputs = [prompt + output.text for output in final_output.outputs] ret = {"text": text_outputs} logger.info(f"Completed request {request_id}") return Response(content=json.dumps(ret)) deployment = VLLMDeployment.bind()
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/stable-diffusion-rayserve-gpu/ray_serve_stablediffusion.py
blueprints/inference/stable-diffusion-rayserve-gpu/ray_serve_stablediffusion.py
from io import BytesIO from fastapi import FastAPI from fastapi.responses import Response import torch import os import ray from ray import serve app = FastAPI() @serve.deployment(num_replicas=1) @serve.ingress(app) class APIIngress: def __init__(self, diffusion_model_handle) -> None: self.handle = diffusion_model_handle @app.get( "/imagine", responses={200: {"content": {"image/png": {}}}}, response_class=Response, ) async def generate(self, prompt: str, img_size: int = 768): assert len(prompt), "prompt parameter cannot be empty" image = await self.handle.generate.remote(prompt, img_size=img_size) file_stream = BytesIO() image.save(file_stream, "PNG") return Response(content=file_stream.getvalue(), media_type="image/png") @serve.deployment(name="stable-diffusion-v2", ray_actor_options={"num_gpus": 1}, autoscaling_config={"min_replicas": 1, "max_replicas": 2}, ) class StableDiffusionV2: def __init__(self): from diffusers import EulerDiscreteScheduler, StableDiffusionPipeline model_id = os.getenv('MODEL_ID') scheduler = EulerDiscreteScheduler.from_pretrained( model_id, subfolder="scheduler" ) self.pipe = StableDiffusionPipeline.from_pretrained( model_id, scheduler=scheduler ) self.pipe = self.pipe.to("cuda") def generate(self, prompt: str, img_size: int = 768): assert len(prompt), "prompt parameter cannot be empty" image = self.pipe(prompt, height=img_size, width=img_size).images[0] return image entrypoint = APIIngress.bind(StableDiffusionV2.bind())
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/stable-diffusion-rayserve-gpu/locust/locustfile.py
blueprints/inference/stable-diffusion-rayserve-gpu/locust/locustfile.py
import json from locust import HttpUser, task, between class StableDiffusionUser(HttpUser): wait_time = between(1, 2) # Seconds between requests @task def generate_image(self): prompt = "A beautiful sunset over the ocean" payload = { "prompt": prompt } headers = { "Content-Type": "application/json" } response = self.client.get( "/imagine", params=payload, data=json.dumps(payload), headers=headers ) if response.status_code == 200: print(f"Generated image for prompt: {prompt}") else: print(f"Error generating image: {response.text}") # You can add more tasks here if needed
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/llama3-8b-instruct-rayserve-inf2/ray_serve_llama3.py
blueprints/inference/llama3-8b-instruct-rayserve-inf2/ray_serve_llama3.py
import os import time import torch from transformers import AutoTokenizer from transformers_neuronx.llama.model import LlamaForSampling from transformers import LlamaForCausalLM, LlamaTokenizer, PreTrainedTokenizerFast from transformers_neuronx import NeuronConfig from transformers_neuronx import GQA from transformers_neuronx import QuantizationConfig from transformers_neuronx.config import GenerationConfig from fastapi import FastAPI from ray import serve from huggingface_hub import login app = FastAPI() # Set this to the Hugging Face model ID hf_token = os.getenv('HUGGING_FACE_HUB_TOKEN') model_id = os.getenv('MODEL_ID') neuron_cores = 24 # inf2.24xlarge 6 Neurons (12 Neuron cores) and inf2.48xlarge 12 Neurons (24 Neuron cores) neuron_config = NeuronConfig( on_device_embedding=False, attention_layout='BSH', fuse_qkv=True, group_query_attention=GQA.REPLICATED_HEADS, quant=QuantizationConfig(), on_device_generation=GenerationConfig(do_sample=True) ) # Define the APIIngress class responsible for handling inference requests @serve.deployment(num_replicas=1) @serve.ingress(app) class APIIngress: def __init__(self, llama_model_handle) -> None: self.handle = llama_model_handle # Define an endpoint for inference @app.get("/infer") async def infer(self, sentence: str): # Asynchronously perform inference using the provided sentence result = await self.handle.infer.remote(sentence) return result # Define the LlamaModel class responsible for managing the Llama language model # Increase the number of replicas for the LlamaModel deployment. # This will allow Ray Serve to handle more concurrent requests. @serve.deployment( ray_actor_options={ "resources": {"neuron_cores": neuron_cores}, "runtime_env": {"env_vars": {"NEURON_CC_FLAGS": "-O1"}}, }, autoscaling_config={"min_replicas": 1, "max_replicas": 2}, ) class LlamaModel: def __init__(self): # Log in to the Hugging Face Hub login(token=hf_token) # Load and compile the Neuron-optimized Llama model self.neuron_model = LlamaForSampling.from_pretrained( model_id, neuron_config=neuron_config, batch_size=1, tp_degree=neuron_cores, amp='f16', n_positions=4096 ) self.neuron_model.to_neuron() # Initialize tokenizer for the model self.tokenizer = AutoTokenizer.from_pretrained(model_id) # Define the method for performing inference with the Llama model def infer(self, sentence: str): # Tokenize the input sentence and encode it input_ids = self.tokenizer.encode(sentence, return_tensors="pt") # Perform inference with Neuron-optimized model with torch.inference_mode(): generated_sequences = self.neuron_model.sample( input_ids, temperature=0.5, sequence_length=2048, top_p=0.9, no_repeat_ngram_size=3 ) # Decode the generated sequences and return the results return [self.tokenizer.decode(seq, skip_special_tokens=True) for seq in generated_sequences] # Create an entry point for the FastAPI application entrypoint = APIIngress.bind(LlamaModel.bind())
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/vllm-llama3.1-405b-trn1/docker/neuron_multi_node_runner.py
blueprints/inference/vllm-llama3.1-405b-trn1/docker/neuron_multi_node_runner.py
# NOTE: This API server is used only for demonstrating usage of AsyncEngine # and simple performance benchmarks. It is not intended for production use. # For production use, we recommend using our OpenAI compatible server. # change `vllm/entrypoints/openai/api_server.py` instead. from vllm.entrypoints.neuron_multi_node import api_server import os import torch import argparse print(os.getenv(k) for k in ['']) def main(): rank_id = int(os.getenv("NEURON_RANK_ID", "0")) if rank_id == 0: master() else: main_worker() def master(): rank_id = int(os.getenv("NEURON_RANK_ID", "0")) print(f"**** init master node with rank_id: {rank_id}") args, _ = api_server.initialize_worker() api_server.run_master(args) # call asyn llm engine def main_worker(): rank_id = int(os.getenv("NEURON_RANK_ID", "0")) print(f"**** init worker node with rank_id: {rank_id}") args, engine = api_server.initialize_worker() worker = engine.engine.model_executor.driver_worker while True: worker.execute_model() if "__main__" == __name__: main()
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/llama2-13b-chat-rayserve-inf2/ray_serve_llama2.py
blueprints/inference/llama2-13b-chat-rayserve-inf2/ray_serve_llama2.py
import os import logging from fastapi import FastAPI from ray import serve import torch from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig from transformers_neuronx.llama.model import LlamaForSampling from transformers_neuronx.module import save_pretrained_split app = FastAPI() llm_model_split = "llama-2-13b-chat-hf-split" neuron_cores = int(os.getenv('NEURON_CORES', 24)) # Read from environment variable, default to 24 # --- Logging Setup --- logger = logging.getLogger("ray.serve") logger.setLevel(logging.INFO) logging.basicConfig(level=logging.INFO) # Define the APIIngress class responsible for handling inference requests @serve.deployment(num_replicas=1) @serve.ingress(app) class APIIngress: def __init__(self, llama_model_handle): self.handle = llama_model_handle @app.get("/infer") async def infer(self, sentence: str): # Asynchronously perform inference using the provided sentence result = await self.handle.infer.remote(sentence) return result # Define the LlamaModel class responsible for managing the Llama language model @serve.deployment( name="Llama-2-13b-chat-hf", autoscaling_config={"min_replicas": 1, "max_replicas": 2}, ray_actor_options={ "resources": {"neuron_cores": neuron_cores}, "runtime_env": {"env_vars": {"NEURON_CC_FLAGS": "-O1"}}, }, ) class LlamaModel: def __init__(self): from transformers_neuronx.llama.model import LlamaForSampling from transformers_neuronx.module import save_pretrained_split llm_model = os.getenv('MODEL_ID', 'NousResearch/Llama-2-13b-chat-hf') logger.info(f"Using model ID: {llm_model}") # Check if the model split exists locally, and if not, download it if not os.path.exists(llm_model_split): logger.info(f"Saving model split for {llm_model} to local path {llm_model_split}") try: self.model = AutoModelForCausalLM.from_pretrained(llm_model) # Set and validate generation config generation_config = GenerationConfig( do_sample=True, temperature=0.9, top_p=0.6, top_k=50, ) generation_config.validate() self.model.generation_config = generation_config save_pretrained_split(self.model, llm_model_split) except Exception as e: logger.error(f"Error during model download or split saving: {e}") raise e else: logger.info(f"Using existing model split {llm_model_split}") logger.info(f"Loading and compiling model {llm_model_split} for Neuron") try: self.neuron_model = LlamaForSampling.from_pretrained( llm_model_split, batch_size=1, tp_degree=neuron_cores, amp='f16' ) self.neuron_model.to_neuron() logger.info("Model loaded and compiled successfully") except Exception as e: logger.error(f"Error during model loading or compilation: {e}") raise e self.tokenizer = AutoTokenizer.from_pretrained(llm_model) def infer(self, sentence: str): input_ids = self.tokenizer.encode(sentence, return_tensors="pt") with torch.inference_mode(): try: logger.info(f"Performing inference on input: {sentence}") generated_sequences = self.neuron_model.sample( input_ids, sequence_length=2048, top_k=50 ) decoded_sequences = [self.tokenizer.decode(seq, skip_special_tokens=True) for seq in generated_sequences] logger.info(f"Inference result: {decoded_sequences}") return decoded_sequences except Exception as e: logger.error(f"Error during inference: {e}") return {"error": "Inference failed"} # Create an entry point for the FastAPI application entrypoint = APIIngress.bind(LlamaModel.bind())
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/blueprints/inference/mistral-7b-rayserve-inf2/ray_serve_mistral.py
blueprints/inference/mistral-7b-rayserve-inf2/ray_serve_mistral.py
# Import necessary libraries and modules from io import BytesIO from fastapi import FastAPI import os from ray import serve import torch # Initialize the FastAPI app app = FastAPI() # Define the number of Neuron cores to be used neuron_cores = 2 # Deployment settings for the API ingress using Ray Serve @serve.deployment(name="mistral-deployment", num_replicas=1, route_prefix="/") @serve.ingress(app) class APIIngress: # Constructor to initialize the API with a model handle def __init__(self, mistral_model_handle) -> None: self.handle = mistral_model_handle # Define a GET endpoint for inference @app.get("/infer") async def infer(self, sentence: str): # Asynchronously perform inference using the provided sentence and return the result result = await self.handle.infer.remote(sentence) return result # Deployment settings for the Mistral model using Ray Serve @serve.deployment(name="mistral-7b", autoscaling_config={"min_replicas": 0, "max_replicas": 6}, ray_actor_options={ "resources": {"neuron_cores": neuron_cores}, "runtime_env": {"env_vars": {"NEURON_CC_FLAGS": "-O1"}}, }, ) class MistralModel: # Constructor to initialize and load the model def __init__(self): # Import additional necessary modules from transformers import AutoTokenizer from transformers_neuronx import MistralForSampling, GQA, NeuronConfig from huggingface_hub import login # Retrieve environment variables for API authentication and model ID hf_token = os.getenv('HUGGING_FACE_HUB_TOKEN') model_id = os.getenv('MODEL_ID') # Log in to the Hugging Face Hub login(token=hf_token) # Set the sharding strategy for the model to optimize performance neuron_config = NeuronConfig( group_query_attention=GQA.SHARD_OVER_HEADS ) # Load and compile the Neuron model with specific configuration self.neuron_model = MistralForSampling.from_pretrained(model_id, amp='bf16', neuron_config=neuron_config) self.neuron_model.to_neuron() # Initialize tokenizer for the model self.tokenizer = AutoTokenizer.from_pretrained(model_id) # Define the inference method to process input text def infer(self, sentence: str): # Prepare input text with specific format text = "[INST]" + sentence + "[/INST]" # Tokenize and encode the input text encoded_input = self.tokenizer.encode(text, return_tensors='pt') # Perform inference in a context that disables gradient calculation with torch.inference_mode(): generated_sequence = self.neuron_model.sample(encoded_input, sequence_length=512, start_ids=None) # Decode the generated sequences into human-readable text and return return [self.tokenizer.decode(seq) for seq in generated_sequence] # Bind the model to the API ingress to enable endpoint functionality entrypoint = APIIngress.bind(MistralModel.bind())
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/infra/trainium-inferentia/examples/dp-bert-large-pretrain/docker/assets/dump_env.py
infra/trainium-inferentia/examples/dp-bert-large-pretrain/docker/assets/dump_env.py
#!/usr/bin/env python3 # Test script to dump the current environment, Neuron devices, EFA devices, etc. for # troubleshooting import os import sys import subprocess print("Environment variables:") for key in os.environ.keys(): print(f"{key} -> {os.environ[key]}") print("\nCommandline Args:") for i,a in enumerate(sys.argv): print(f"{i}: {a}") neuron_devs = subprocess.check_output("find /dev/ -name neur*", shell=True) print(f"\nNeuron devices:\n{neuron_devs.decode()}") pci_devs = subprocess.check_output("lspci", shell=True) print(f"\nPCI devices:\n{pci_devs.decode()}") try: efa_devs = subprocess.check_output("/opt/amazon/efa/bin/fi_info -p efa", shell=True) print(f"\nEFA devices:\n{efa_devs.decode()}") except subprocess.CalledProcessError as e: print("\nNo EFA detected") pip_packages = subprocess.check_output("pip3 list|grep -e neuron -e torch", shell=True) print(f"\nPIP packages:\n{pip_packages.decode()}")
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/infra/trainium-inferentia/examples/dp-bert-large-pretrain/lib/trn1_dist_ddp.py
infra/trainium-inferentia/examples/dp-bert-large-pretrain/lib/trn1_dist_ddp.py
import torchx.specs as specs import shlex import os import re # Define the root directory for caching data on shared storage (FSx for Lustre) CACHE_ROOT = "/data/" # Create an AppDef based on code snippets borrowed from the TorchX DDP builtin # see: https://github.com/pytorch/torchx/blob/main/torchx/components/dist.py # Function to generate an AppDef based on provided parameters def generateAppDef(script_args: str, nnodes: int, nproc_per_node: int, script: str, image: str, name: str, precompile: bool=False, bf16: bool=False, cacheset: str="default", instance_type: str="trn1.32xlarge", node_selectors: str="", tolerations: str="") -> specs.AppDef: # Convert node selectors to a dictionary node_selector_dict = {} if node_selectors: node_selector_pairs = node_selectors.split(',') for pair in node_selector_pairs: key, value = pair.split('=') node_selector_dict[key] = value # Convert tolerations to a list of dictionaries tolerations_list = [] if tolerations: toleration_pairs = tolerations.split(',') for pair in toleration_pairs: key_value_effect = pair.split('=') tolerations_list.append( { 'key': key_value_effect[0], 'value': key_value_effect[1].split(':')[0], 'effect': key_value_effect[1].split(':')[1] } ) # Define the location of the Neuron compiler cache and Transformers cache on shared storage. # Note: "cacheset" is a unique name/key chosen by the user to distinguish between jobs. The # user must ensure that only one job is running for a given cacheset at any given time in # order to avoid corrupting the cache if not re.match(r"^[A-Za-z0-9_-]+$", cacheset): raise ValueError("Error: please make sure that 'cacheset' contains only letters, numbers, dashes, and underscores.") # Define paths for Neuron and Transformers caches based on user-defined cacheset NEURON_CACHE = os.path.join(CACHE_ROOT, "neuron_cache", f"{cacheset}_{specs.macros.replica_id}") TRANSFORMERS_CACHE = os.path.join(CACHE_ROOT, "transformers_cache", f"{cacheset}_{specs.macros.replica_id}") # Construct the command to run the distributed script cmd = [ "python3", "-m", "torch.distributed.run", "--rdzv_backend", "etcd", "--rdzv_endpoint", "etcd-server:2379", "--rdzv_id", f"{specs.macros.app_id}", "--nnodes", str(nnodes), "--nproc_per_node", str(nproc_per_node), "--tee", "3", "--role", "", script ] env_mapping = { "CCOM_SOCKET_IFNAME": "eth0", "FI_EFA_USE_DEVICE_RDMA": "1", "FI_PROVIDER": "efa", "FI_EFA_FORK_SAFE": "1", "NEURON_RT_RESET_CORES": "1", "XLA_TRANSFER_SEED_ASYNC": "1", "NEURON_CC_FLAGS": f"--cache_dir={NEURON_CACHE}", "TRANSFORMERS_CACHE": TRANSFORMERS_CACHE } # Configure BF16 if requested by user if bf16: env_mapping["XLA_DOWNCAST_BF16"] = "1" #---------------------------------------------------------------------- # Karpenter does not work with this option enabled "vpc.amazonaws.com/efa": num_efas #---------------------------------------------------------------------- instance_type = instance_type.lower() if instance_type == "trn1n.32xlarge": num_efas = 16 elif instance_type == "trn1.32xlarge": num_efas = 8 else: raise Exception(f"Instance type {instance_type} is not supported.\n" + "Please try trn1.32xlarge or trn1n.32xlarge.") resourcedef = specs.Resource(cpu=0, gpu=0, memMB=0, capabilities={"node.kubernetes.io/instance-type": instance_type}, devices={"aws.amazon.com/neuron": 16, "vpc.amazonaws.com/efa": num_efas}) # resourcedef = specs.Resource(cpu=0, gpu=0, memMB=0, # capabilities=node_selector_dict, # devices={"aws.amazon.com/neuron": 16}) print(f"resourcedef: {resourcedef}") # Determine entrypoint and arguments based on precompile request if precompile: entrypoint = "neuron_parallel_compile" args = cmd + script_args.split() else: entrypoint = "python3" args = cmd[1:] + script_args.split() # Define the AppDef configuration appdef = specs.AppDef( name=name or "test_name", roles=[ specs.Role( name="role1", image=image, entrypoint=entrypoint, num_replicas=nnodes, resource=resourcedef, args=args, env=env_mapping, max_retries=3, retry_policy=specs.RetryPolicy("APPLICATION"), mounts=[specs.VolumeMount(src="fsx-claim", dst_path="/data")] ) ], ) print(f"appdef: {appdef}") return appdef # Function to join arguments while avoiding shlex.quote for arguments wrapped in _noquote def _args_join(args): """ _args_join is like shlex.join but if the argument is wrapped in _noquote it'll not quote that argument. """ quoted = [arg if isinstance(arg, _noquote) else shlex.quote(arg) for arg in args] return " ".join(quoted) # Wrapper class to indicate that an argument should not be quoted by shlex class _noquote(str): """ _noquote is a wrapper around str that indicates that the argument shouldn't be passed through shlex.quote. """ pass
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/infra/jark-stack/src/service/dogbooth.py
infra/jark-stack/src/service/dogbooth.py
from io import BytesIO from fastapi import FastAPI from fastapi.responses import Response import torch import os from ray import serve app = FastAPI() @serve.deployment(num_replicas=1, route_prefix="/") @serve.ingress(app) class APIIngress: def __init__(self, diffusion_model_handle) -> None: self.handle = diffusion_model_handle @app.get( "/imagine", responses={200: {"content": {"image/png": {}}}}, response_class=Response, ) async def generate(self, prompt: str, img_size: int = 768): assert len(prompt), "prompt parameter cannot be empty" image_ref = await self.handle.generate.remote(prompt, img_size=img_size) image = await image_ref file_stream = BytesIO() image.save(file_stream, "PNG") return Response(content=file_stream.getvalue(), media_type="image/png") @serve.deployment( ray_actor_options={"num_gpus": 1}, autoscaling_config={"min_replicas": 1, "max_replicas": 2}, ) class StableDiffusionV2: def __init__(self): from diffusers import EulerDiscreteScheduler, StableDiffusionPipeline model_id = os.getenv('MODEL_ID') scheduler = EulerDiscreteScheduler.from_pretrained( model_id, subfolder="scheduler" ) self.pipe = StableDiffusionPipeline.from_pretrained( model_id, scheduler=scheduler ) self.pipe = self.pipe.to("cuda") def generate(self, prompt: str, img_size: int = 768): assert len(prompt), "prompt parameter cannot be empty" image = self.pipe(prompt, height=img_size, width=img_size).images[0] return image entrypoint = APIIngress.bind(StableDiffusionV2.bind())
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/infra/jark-stack/src/app/streamlit.py
infra/jark-stack/src/app/streamlit.py
import streamlit as st import requests from urllib.parse import urlencode from PIL import Image import tempfile ### Update Hostname before building image base_url="" st.title("Welcome to dogbooth! :dog:") st.header("_a place to create images of [v]dog in beautiful scenes._") prompt = st.chat_input("a photo of a [v]dog ...") if prompt: query_params = { "prompt": prompt } encoded_query = urlencode(query_params) image_url = f"{base_url}?{encoded_query}" with st.spinner("Wait for it..."): response = requests.get(image_url, timeout=180) if response.status_code == 200: content_size = len(response.content) with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f: f.write(response.content) st.image(Image.open(f.name), caption=prompt) st.balloons() else: st.error(f"Failed to download image. Status code: {response.status_code}")
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
awslabs/ai-on-eks
https://github.com/awslabs/ai-on-eks/blob/754f4163e145c1344182965d8b41a044aa956f45/infra/emr-spark-rapids/examples/xgboost/etl-xgboost-train-transform.py
infra/emr-spark-rapids/examples/xgboost/etl-xgboost-train-transform.py
import time import os import sys from pyspark import broadcast, SparkContext from pyspark.conf import SparkConf from pyspark.sql import SparkSession, HiveContext from pyspark.sql.functions import * from pyspark.sql.types import * from pyspark.sql.window import Window # Import ML Libraries from xgboost.spark import SparkXGBClassifier, SparkXGBClassifierModel from pyspark.ml.evaluation import MulticlassClassificationEvaluator dataRoot = None if len(sys.argv) > 2: dataRoot = sys.argv[1] outputRoot = sys.argv[2] num_workers = sys.argv[3] else: print("Data Root path not provided") sys.exit(1) orig_raw_path = f"{dataRoot}/fannie-mae-single-family-loan-performance/" orig_raw_path_csv2parquet = f"{outputRoot}/csv2parquet/" # Create spark session spark = SparkSession\ .builder.appName("EMR-XGBoost-Spark-GPU")\ .enableHiveSupport()\ .getOrCreate() # Set True to save processed dataset after ETL # Set False, the dataset after ETL will be directly used in XGBoost train and transform is_save_dataset=True output_path_data=f"{outputRoot}/fannie-mae-single-family-loan-performance/mortgage/output/data/" # the path to save the xgboost model output_path_model=f"{outputRoot}/fannie-mae-single-family-loan-performance/mortgage/output/model/" # File schema _csv_raw_schema = StructType([ StructField("reference_pool_id", StringType()), StructField("loan_id", LongType()), StructField("monthly_reporting_period", StringType()), StructField("orig_channel", StringType()), StructField("seller_name", StringType()), StructField("servicer", StringType()), StructField("master_servicer", StringType()), StructField("orig_interest_rate", DoubleType()), StructField("interest_rate", DoubleType()), StructField("orig_upb", DoubleType()), StructField("upb_at_issuance", StringType()), StructField("current_actual_upb", DoubleType()), StructField("orig_loan_term", IntegerType()), StructField("orig_date", StringType()), StructField("first_pay_date", StringType()), StructField("loan_age", DoubleType()), StructField("remaining_months_to_legal_maturity", DoubleType()), StructField("adj_remaining_months_to_maturity", DoubleType()), StructField("maturity_date", StringType()), StructField("orig_ltv", DoubleType()), StructField("orig_cltv", DoubleType()), StructField("num_borrowers", DoubleType()), StructField("dti", DoubleType()), StructField("borrower_credit_score", DoubleType()), StructField("coborrow_credit_score", DoubleType()), StructField("first_home_buyer", StringType()), StructField("loan_purpose", StringType()), StructField("property_type", StringType()), StructField("num_units", IntegerType()), StructField("occupancy_status", StringType()), StructField("property_state", StringType()), StructField("msa", DoubleType()), StructField("zip", IntegerType()), StructField("mortgage_insurance_percent", DoubleType()), StructField("product_type", StringType()), StructField("prepayment_penalty_indicator", StringType()), StructField("interest_only_loan_indicator", StringType()), StructField("interest_only_first_principal_and_interest_payment_date", StringType()), StructField("months_to_amortization", StringType()), StructField("current_loan_delinquency_status", IntegerType()), StructField("loan_payment_history", StringType()), StructField("mod_flag", StringType()), StructField("mortgage_insurance_cancellation_indicator", StringType()), StructField("zero_balance_code", StringType()), StructField("zero_balance_effective_date", StringType()), StructField("upb_at_the_time_of_removal", StringType()), StructField("repurchase_date", StringType()), StructField("scheduled_principal_current", StringType()), StructField("total_principal_current", StringType()), StructField("unscheduled_principal_current", StringType()), StructField("last_paid_installment_date", StringType()), StructField("foreclosed_after", StringType()), StructField("disposition_date", StringType()), StructField("foreclosure_costs", DoubleType()), StructField("prop_preservation_and_repair_costs", DoubleType()), StructField("asset_recovery_costs", DoubleType()), StructField("misc_holding_expenses", DoubleType()), StructField("holding_taxes", DoubleType()), StructField("net_sale_proceeds", DoubleType()), StructField("credit_enhancement_proceeds", DoubleType()), StructField("repurchase_make_whole_proceeds", StringType()), StructField("other_foreclosure_proceeds", DoubleType()), StructField("non_interest_bearing_upb", DoubleType()), StructField("principal_forgiveness_upb", StringType()), StructField("original_list_start_date", StringType()), StructField("original_list_price", StringType()), StructField("current_list_start_date", StringType()), StructField("current_list_price", StringType()), StructField("borrower_credit_score_at_issuance", StringType()), StructField("co-borrower_credit_score_at_issuance", StringType()), StructField("borrower_credit_score_current", StringType()), StructField("co-Borrower_credit_score_current", StringType()), StructField("mortgage_insurance_type", DoubleType()), StructField("servicing_activity_indicator", StringType()), StructField("current_period_modification_loss_amount", StringType()), StructField("cumulative_modification_loss_amount", StringType()), StructField("current_period_credit_event_net_gain_or_loss", StringType()), StructField("cumulative_credit_event_net_gain_or_loss", StringType()), StructField("homeready_program_indicator", StringType()), StructField("foreclosure_principal_write_off_amount", StringType()), StructField("relocation_mortgage_indicator", StringType()), StructField("zero_balance_code_change_date", StringType()), StructField("loan_holdback_indicator", StringType()), StructField("loan_holdback_effective_date", StringType()), StructField("delinquent_accrued_interest", StringType()), StructField("property_valuation_method", StringType()), StructField("high_balance_loan_indicator", StringType()), StructField("arm_initial_fixed-rate_period_lt_5_yr_indicator", StringType()), StructField("arm_product_type", StringType()), StructField("initial_fixed-rate_period", StringType()), StructField("interest_rate_adjustment_frequency", StringType()), StructField("next_interest_rate_adjustment_date", StringType()), StructField("next_payment_change_date", StringType()), StructField("index", StringType()), StructField("arm_cap_structure", StringType()), StructField("initial_interest_rate_cap_up_percent", StringType()), StructField("periodic_interest_rate_cap_up_percent", StringType()), StructField("lifetime_interest_rate_cap_up_percent", StringType()), StructField("mortgage_margin", StringType()), StructField("arm_balloon_indicator", StringType()), StructField("arm_plan_number", StringType()), StructField("borrower_assistance_plan", StringType()), StructField("hltv_refinance_option_indicator", StringType()), StructField("deal_name", StringType()), StructField("repurchase_make_whole_proceeds_flag", StringType()), StructField("alternative_delinquency_resolution", StringType()), StructField("alternative_delinquency_resolution_count", StringType()), StructField("total_deferral_amount", StringType()) ]) # name mappings _name_mapping = [ ("WITMER FUNDING, LLC", "Witmer"), ("WELLS FARGO CREDIT RISK TRANSFER SECURITIES TRUST 2015", "Wells Fargo"), ("WELLS FARGO BANK, NA" , "Wells Fargo"), ("WELLS FARGO BANK, N.A." , "Wells Fargo"), ("WELLS FARGO BANK, NA" , "Wells Fargo"), ("USAA FEDERAL SAVINGS BANK" , "USAA"), ("UNITED SHORE FINANCIAL SERVICES, LLC D\\/B\\/A UNITED WHOLESALE MORTGAGE" , "United Seq(e"), ("U.S. BANK N.A." , "US Bank"), ("SUNTRUST MORTGAGE INC." , "Suntrust"), ("STONEGATE MORTGAGE CORPORATION" , "Stonegate Mortgage"), ("STEARNS LENDING, LLC" , "Stearns Lending"), ("STEARNS LENDING, INC." , "Stearns Lending"), ("SIERRA PACIFIC MORTGAGE COMPANY, INC." , "Sierra Pacific Mortgage"), ("REGIONS BANK" , "Regions"), ("RBC MORTGAGE COMPANY" , "RBC"), ("QUICKEN LOANS INC." , "Quicken Loans"), ("PULTE MORTGAGE, L.L.C." , "Pulte Mortgage"), ("PROVIDENT FUNDING ASSOCIATES, L.P." , "Provident Funding"), ("PROSPECT MORTGAGE, LLC" , "Prospect Mortgage"), ("PRINCIPAL RESIDENTIAL MORTGAGE CAPITAL RESOURCES, LLC" , "Principal Residential"), ("PNC BANK, N.A." , "PNC"), ("PMT CREDIT RISK TRANSFER TRUST 2015-2" , "PennyMac"), ("PHH MORTGAGE CORPORATION" , "PHH Mortgage"), ("PENNYMAC CORP." , "PennyMac"), ("PACIFIC UNION FINANCIAL, LLC" , "Other"), ("OTHER" , "Other"), ("NYCB MORTGAGE COMPANY, LLC" , "NYCB"), ("NEW YORK COMMUNITY BANK" , "NYCB"), ("NETBANK FUNDING SERVICES" , "Netbank"), ("NATIONSTAR MORTGAGE, LLC" , "Nationstar Mortgage"), ("METLIFE BANK, NA" , "Metlife"), ("LOANDEPOT.COM, LLC" , "LoanDepot.com"), ("J.P. MORGAN MADISON AVENUE SECURITIES TRUST, SERIES 2015-1" , "JP Morgan Chase"), ("J.P. MORGAN MADISON AVENUE SECURITIES TRUST, SERIES 2014-1" , "JP Morgan Chase"), ("JPMORGAN CHASE BANK, NATIONAL ASSOCIATION" , "JP Morgan Chase"), ("JPMORGAN CHASE BANK, NA" , "JP Morgan Chase"), ("JP MORGAN CHASE BANK, NA" , "JP Morgan Chase"), ("IRWIN MORTGAGE, CORPORATION" , "Irwin Mortgage"), ("IMPAC MORTGAGE CORP." , "Impac Mortgage"), ("HSBC BANK USA, NATIONAL ASSOCIATION" , "HSBC"), ("HOMEWARD RESIDENTIAL, INC." , "Homeward Mortgage"), ("HOMESTREET BANK" , "Other"), ("HOMEBRIDGE FINANCIAL SERVICES, INC." , "HomeBridge"), ("HARWOOD STREET FUNDING I, LLC" , "Harwood Mortgage"), ("GUILD MORTGAGE COMPANY" , "Guild Mortgage"), ("GMAC MORTGAGE, LLC (USAA FEDERAL SAVINGS BANK)" , "GMAC"), ("GMAC MORTGAGE, LLC" , "GMAC"), ("GMAC (USAA)" , "GMAC"), ("FREMONT BANK" , "Fremont Bank"), ("FREEDOM MORTGAGE CORP." , "Freedom Mortgage"), ("FRANKLIN AMERICAN MORTGAGE COMPANY" , "Franklin America"), ("FLEET NATIONAL BANK" , "Fleet National"), ("FLAGSTAR CAPITAL MARKETS CORPORATION" , "Flagstar Bank"), ("FLAGSTAR BANK, FSB" , "Flagstar Bank"), ("FIRST TENNESSEE BANK NATIONAL ASSOCIATION" , "Other"), ("FIFTH THIRD BANK" , "Fifth Third Bank"), ("FEDERAL HOME LOAN BANK OF CHICAGO" , "Fedral Home of Chicago"), ("FDIC, RECEIVER, INDYMAC FEDERAL BANK FSB" , "FDIC"), ("DOWNEY SAVINGS AND LOAN ASSOCIATION, F.A." , "Downey Mortgage"), ("DITECH FINANCIAL LLC" , "Ditech"), ("CITIMORTGAGE, INC." , "Citi"), ("CHICAGO MORTGAGE SOLUTIONS DBA INTERFIRST MORTGAGE COMPANY" , "Chicago Mortgage"), ("CHICAGO MORTGAGE SOLUTIONS DBA INTERBANK MORTGAGE COMPANY" , "Chicago Mortgage"), ("CHASE HOME FINANCE, LLC" , "JP Morgan Chase"), ("CHASE HOME FINANCE FRANKLIN AMERICAN MORTGAGE COMPANY" , "JP Morgan Chase"), ("CHASE HOME FINANCE (CIE 1)" , "JP Morgan Chase"), ("CHASE HOME FINANCE" , "JP Morgan Chase"), ("CASHCALL, INC." , "CashCall"), ("CAPITAL ONE, NATIONAL ASSOCIATION" , "Capital One"), ("CALIBER HOME LOANS, INC." , "Caliber Funding"), ("BISHOPS GATE RESIDENTIAL MORTGAGE TRUST" , "Bishops Gate Mortgage"), ("BANK OF AMERICA, N.A." , "Bank of America"), ("AMTRUST BANK" , "AmTrust"), ("AMERISAVE MORTGAGE CORPORATION" , "Amerisave"), ("AMERIHOME MORTGAGE COMPANY, LLC" , "AmeriHome Mortgage"), ("ALLY BANK" , "Ally Bank"), ("ACADEMY MORTGAGE CORPORATION" , "Academy Mortgage"), ("NO CASH-OUT REFINANCE" , "OTHER REFINANCE"), ("REFINANCE - NOT SPECIFIED" , "OTHER REFINANCE"), ("Other REFINANCE" , "OTHER REFINANCE")] # String columns cate_col_names = [ "orig_channel", "first_home_buyer", "loan_purpose", "property_type", "occupancy_status", "property_state", "product_type", "relocation_mortgage_indicator", "seller_name", "mod_flag" ] # Numeric columns label_col_name = "delinquency_12" numeric_col_names = [ "orig_interest_rate", "orig_upb", "orig_loan_term", "orig_ltv", "orig_cltv", "num_borrowers", "dti", "borrower_credit_score", "num_units", "zip", "mortgage_insurance_percent", "current_loan_delinquency_status", "current_actual_upb", "interest_rate", "loan_age", "msa", "non_interest_bearing_upb", label_col_name ] all_col_names = cate_col_names + numeric_col_names #------------------------------------------- # Define the function to do the ETL process #------------------------------------------- # Define function to get quarter from input CSV file name def _get_quarter_from_csv_file_name(): return substring_index(substring_index(input_file_name(), ".", 1), "/", -1) # Define function to read raw CSV data file def read_raw_csv(spark, path): return spark.read.format('csv') \ .option('nullValue', '') \ .option('header', False) \ .option('delimiter', '|') \ .schema(_csv_raw_schema) \ .load(path) \ .withColumn('quarter', _get_quarter_from_csv_file_name()) # Functions to extract perf and acq columns from raw schema def extract_perf_columns(rawDf): perfDf = rawDf.select( col("loan_id"), date_format(to_date(col("monthly_reporting_period"),"MMyyyy"), "MM/dd/yyyy").alias("monthly_reporting_period"), upper(col("servicer")).alias("servicer"), col("interest_rate"), col("current_actual_upb"), col("loan_age"), col("remaining_months_to_legal_maturity"), col("adj_remaining_months_to_maturity"), date_format(to_date(col("maturity_date"),"MMyyyy"), "MM/yyyy").alias("maturity_date"), col("msa"), col("current_loan_delinquency_status"), col("mod_flag"), col("zero_balance_code"), date_format(to_date(col("zero_balance_effective_date"),"MMyyyy"), "MM/yyyy").alias("zero_balance_effective_date"), date_format(to_date(col("last_paid_installment_date"),"MMyyyy"), "MM/dd/yyyy").alias("last_paid_installment_date"), date_format(to_date(col("foreclosed_after"),"MMyyyy"), "MM/dd/yyyy").alias("foreclosed_after"), date_format(to_date(col("disposition_date"),"MMyyyy"), "MM/dd/yyyy").alias("disposition_date"), col("foreclosure_costs"), col("prop_preservation_and_repair_costs"), col("asset_recovery_costs"), col("misc_holding_expenses"), col("holding_taxes"), col("net_sale_proceeds"), col("credit_enhancement_proceeds"), col("repurchase_make_whole_proceeds"), col("other_foreclosure_proceeds"), col("non_interest_bearing_upb"), col("principal_forgiveness_upb"), col("repurchase_make_whole_proceeds_flag"), col("foreclosure_principal_write_off_amount"), col("servicing_activity_indicator"), col('quarter') ) return perfDf.select("*").filter("current_actual_upb != 0.0") def extract_acq_columns(rawDf): acqDf = rawDf.select( col("loan_id"), col("orig_channel"), upper(col("seller_name")).alias("seller_name"), col("orig_interest_rate"), col("orig_upb"), col("orig_loan_term"), date_format(to_date(col("orig_date"),"MMyyyy"), "MM/yyyy").alias("orig_date"), date_format(to_date(col("first_pay_date"),"MMyyyy"), "MM/yyyy").alias("first_pay_date"), col("orig_ltv"), col("orig_cltv"), col("num_borrowers"), col("dti"), col("borrower_credit_score"), col("first_home_buyer"), col("loan_purpose"), col("property_type"), col("num_units"), col("occupancy_status"), col("property_state"), col("zip"), col("mortgage_insurance_percent"), col("product_type"), col("coborrow_credit_score"), col("mortgage_insurance_type"), col("relocation_mortgage_indicator"), dense_rank().over(Window.partitionBy("loan_id").orderBy(to_date(col("monthly_reporting_period"),"MMyyyy"))).alias("rank"), col('quarter') ) return acqDf.select("*").filter(col("rank")==1) # Define function to parse dates in Performance data def _parse_dates(perf): return perf \ .withColumn("monthly_reporting_period", to_date(col("monthly_reporting_period"), "MM/dd/yyyy")) \ .withColumn("monthly_reporting_period_month", month(col("monthly_reporting_period"))) \ .withColumn("monthly_reporting_period_year", year(col("monthly_reporting_period"))) \ .withColumn("monthly_reporting_period_day", dayofmonth(col("monthly_reporting_period"))) \ .withColumn("last_paid_installment_date", to_date(col("last_paid_installment_date"), "MM/dd/yyyy")) \ .withColumn("foreclosed_after", to_date(col("foreclosed_after"), "MM/dd/yyyy")) \ .withColumn("disposition_date", to_date(col("disposition_date"), "MM/dd/yyyy")) \ .withColumn("maturity_date", to_date(col("maturity_date"), "MM/yyyy")) \ .withColumn("zero_balance_effective_date", to_date(col("zero_balance_effective_date"), "MM/yyyy")) # Define function to create deliquency dataframe from Performance data def _create_perf_deliquency(spark, perf): aggDF = perf.select( col("quarter"), col("loan_id"), col("current_loan_delinquency_status"), when(col("current_loan_delinquency_status") >= 1, col("monthly_reporting_period")).alias("delinquency_30"), when(col("current_loan_delinquency_status") >= 3, col("monthly_reporting_period")).alias("delinquency_90"), when(col("current_loan_delinquency_status") >= 6, col("monthly_reporting_period")).alias("delinquency_180")) \ .groupBy("quarter", "loan_id") \ .agg( max("current_loan_delinquency_status").alias("delinquency_12"), min("delinquency_30").alias("delinquency_30"), min("delinquency_90").alias("delinquency_90"), min("delinquency_180").alias("delinquency_180")) \ .select( col("quarter"), col("loan_id"), (col("delinquency_12") >= 1).alias("ever_30"), (col("delinquency_12") >= 3).alias("ever_90"), (col("delinquency_12") >= 6).alias("ever_180"), col("delinquency_30"), col("delinquency_90"), col("delinquency_180")) joinedDf = perf \ .withColumnRenamed("monthly_reporting_period", "timestamp") \ .withColumnRenamed("monthly_reporting_period_month", "timestamp_month") \ .withColumnRenamed("monthly_reporting_period_year", "timestamp_year") \ .withColumnRenamed("current_loan_delinquency_status", "delinquency_12") \ .withColumnRenamed("current_actual_upb", "upb_12") \ .select("quarter", "loan_id", "timestamp", "delinquency_12", "upb_12", "timestamp_month", "timestamp_year") \ .join(aggDF, ["loan_id", "quarter"], "left_outer") # calculate the 12 month delinquency and upb values months = 12 monthArray = [lit(x) for x in range(0, 12)] # explode on a small amount of data is actually slightly more efficient than a cross join testDf = joinedDf \ .withColumn("month_y", explode(array(monthArray))) \ .select( col("quarter"), floor(((col("timestamp_year") * 12 + col("timestamp_month")) - 24000) / months).alias("josh_mody"), floor(((col("timestamp_year") * 12 + col("timestamp_month")) - 24000 - col("month_y")) / months).alias("josh_mody_n"), col("ever_30"), col("ever_90"), col("ever_180"), col("delinquency_30"), col("delinquency_90"), col("delinquency_180"), col("loan_id"), col("month_y"), col("delinquency_12"), col("upb_12")) \ .groupBy("quarter", "loan_id", "josh_mody_n", "ever_30", "ever_90", "ever_180", "delinquency_30", "delinquency_90", "delinquency_180", "month_y") \ .agg(max("delinquency_12").alias("delinquency_12"), min("upb_12").alias("upb_12")) \ .withColumn("timestamp_year", floor((lit(24000) + (col("josh_mody_n") * lit(months)) + (col("month_y") - 1)) / lit(12))) \ .selectExpr("*", "pmod(24000 + (josh_mody_n * {}) + month_y, 12) as timestamp_month_tmp".format(months)) \ .withColumn("timestamp_month", when(col("timestamp_month_tmp") == lit(0), lit(12)).otherwise(col("timestamp_month_tmp"))) \ .withColumn("delinquency_12", ((col("delinquency_12") > 3).cast("int") + (col("upb_12") == 0).cast("int")).alias("delinquency_12")) \ .drop("timestamp_month_tmp", "josh_mody_n", "month_y") return perf.withColumnRenamed("monthly_reporting_period_month", "timestamp_month") \ .withColumnRenamed("monthly_reporting_period_year", "timestamp_year") \ .join(testDf, ["quarter", "loan_id", "timestamp_year", "timestamp_month"], "left") \ .drop("timestamp_year", "timestamp_month") # Define function to create acquisition dataframe from Acquisition data def _create_acquisition(spark, acq): nameMapping = spark.createDataFrame(_name_mapping, ["from_seller_name", "to_seller_name"]) return acq.join(nameMapping, col("seller_name") == col("from_seller_name"), "left") \ .drop("from_seller_name") \ .withColumn("old_name", col("seller_name")) \ .withColumn("seller_name", coalesce(col("to_seller_name"), col("seller_name"))) \ .drop("to_seller_name") \ .withColumn("orig_date", to_date(col("orig_date"), "MM/yyyy")) \ .withColumn("first_pay_date", to_date(col("first_pay_date"), "MM/yyyy")) # Define function to get column dictionary def _gen_dictionary(etl_df, col_names): cnt_table = etl_df.select(posexplode(array([col(i) for i in col_names])))\ .withColumnRenamed("pos", "column_id")\ .withColumnRenamed("col", "data")\ .filter("data is not null")\ .groupBy("column_id", "data")\ .count() windowed = Window.partitionBy("column_id").orderBy(desc("count")) return cnt_table.withColumn("id", row_number().over(windowed)).drop("count") # Define function to convert string columns to numeric def _cast_string_columns_to_numeric(spark, input_df): cached_dict_df = _gen_dictionary(input_df, cate_col_names).cache() output_df = input_df # Generate the final table with all columns being numeric. for col_pos, col_name in enumerate(cate_col_names): col_dict_df = cached_dict_df.filter(col("column_id") == col_pos)\ .drop("column_id")\ .withColumnRenamed("data", col_name) output_df = output_df.join(broadcast(col_dict_df), col_name, "left")\ .drop(col_name)\ .withColumnRenamed("id", col_name) return output_df # ------------------------------------------- # In this Main Function: # 1/ Parse date in Performance data by calling _parse_dates (parsed_perf) # 2/ Create deliqency dataframe(perf_deliqency) form Performance data by calling _create_perf_deliquency # 3/ Create cleaned acquisition dataframe(cleaned_acq) from Acquisition data by calling _create_acquisition # 4/ Join deliqency dataframe(perf_deliqency) and cleaned acquisition dataframe(cleaned_acq), get clean_df # 5/ Cast String column to Numeric in clean_df by calling _cast_string_columns_to_numeric, get casted_clean_df # 6/ Return casted_clean_df as final result # ------------------------------------------- def run_mortgage(spark, perf, acq): parsed_perf = _parse_dates(perf) perf_deliqency = _create_perf_deliquency(spark, parsed_perf) cleaned_acq = _create_acquisition(spark, acq) clean_df = perf_deliqency.join(cleaned_acq, ["loan_id", "quarter"], "inner").drop("quarter") casted_clean_df = _cast_string_columns_to_numeric(spark, clean_df)\ .select(all_col_names)\ .withColumn(label_col_name, when(col(label_col_name) > 0, 1).otherwise(0))\ .fillna(float(0)) return casted_clean_df # Read Raw Data and Run ETL Process, Save the Result rawDf = read_raw_csv(spark, orig_raw_path) print(f"Raw Dataframe CSV Rows count : {rawDf.count()}") # Save raw data to parquet rawDf.write.parquet(orig_raw_path_csv2parquet, mode='overwrite',compression='snappy') # Read data from parquet rawDf = spark.read.parquet(orig_raw_path_csv2parquet) print(f"Raw Dataframe Parquet Rows count : {rawDf.count()}") acq = extract_acq_columns(rawDf) perf = extract_perf_columns(rawDf) # run main function to process data out = run_mortgage(spark, perf, acq) # save processed data if is_save_dataset: start = time.time() out.write.parquet(output_path_data, mode="overwrite") end = time.time() print("ETL takes {}".format(end - start)) #------------------------------------------- # XGBoost Spark with GPU #------------------------------------------- # Specify the Data Schema and Load the Data label = "delinquency_12" schema = StructType([ StructField("orig_channel", FloatType()), StructField("first_home_buyer", FloatType()), StructField("loan_purpose", FloatType()), StructField("property_type", FloatType()), StructField("occupancy_status", FloatType()), StructField("property_state", FloatType()), StructField("product_type", FloatType()), StructField("relocation_mortgage_indicator", FloatType()), StructField("seller_name", FloatType()), StructField("mod_flag", FloatType()), StructField("orig_interest_rate", FloatType()), StructField("orig_upb", DoubleType()), StructField("orig_loan_term", IntegerType()), StructField("orig_ltv", FloatType()), StructField("orig_cltv", FloatType()), StructField("num_borrowers", FloatType()), StructField("dti", FloatType()), StructField("borrower_credit_score", FloatType()), StructField("num_units", IntegerType()), StructField("zip", IntegerType()), StructField("mortgage_insurance_percent", FloatType()), StructField("current_loan_delinquency_status", IntegerType()), StructField("current_actual_upb", FloatType()), StructField("interest_rate", FloatType()), StructField("loan_age", FloatType()), StructField("msa", FloatType()), StructField("non_interest_bearing_upb", FloatType()), StructField(label, IntegerType()), ]) features = [ x.name for x in schema if x.name != label ] if is_save_dataset: # load dataset from file etlDf = spark.read.parquet(output_path_data) splits = etlDf.randomSplit([0.8, 0.2]) train_data = splits[0] test_data = splits[1] else: # use Dataframe from ETL directly splits = out.randomSplit([0.8, 0.2]) train_data = splits[0] test_data = splits[1] train_data.printSchema() # This sample uses 8 worker(GPU) to run XGBoost training, you can change according to your GPU resources params = { "tree_method": "gpu_hist", "grow_policy": "depthwise", "num_workers": int(num_workers), "device": "cuda", } params['features_col'] = features params['label_col'] = label classifier = SparkXGBClassifier(**params) def with_benchmark(phrase, action): start = time.time() result = action() end = time.time() print("{} takes {} seconds".format(phrase, end - start)) return result # Training time will be printed here # e.g., Training takes 18.92583155632019 seconds model = with_benchmark("Training", lambda: classifier.fit(train_data)) model.write().overwrite().save(output_path_model) loaded_model = SparkXGBClassifierModel().load(output_path_model) # Transformation time in seconds will be printed here def transform(): result = loaded_model.transform(test_data).cache() result.foreachPartition(lambda _: None) return result result = with_benchmark("Transformation", transform) result.select(label, "rawPrediction", "probability", "prediction").show(5) # Evaluation takes x seconds will be printed here # Accuracy is x (e.g., 0.9999999999999999) accuracy = with_benchmark( "Evaluation", lambda: MulticlassClassificationEvaluator().setLabelCol(label).evaluate(result)) print("Accuracy is " + str(accuracy)) spark.stop()
python
Apache-2.0
754f4163e145c1344182965d8b41a044aa956f45
2026-01-05T07:12:30.786413Z
false
mengdeer589/PythonSizeCruncher
https://github.com/mengdeer589/PythonSizeCruncher/blob/7ccd528adbdb5b74eb88eff1aa10fd05c42a28b5/main.py
main.py
# -*- coding: utf-8 -*- """本脚本用于打包后的程序瘦身""" import json import os import sys import threading from _tkinter import TclError from pathlib import Path from tkinter import * from tkinter import filedialog, messagebox from tkinter.ttk import * from typing import List # 记录白名单文件路径 WHITE_FILE = Path(__file__).parent.joinpath("white_files.json") # 默认的白名单文件内容,用于第一次程序运行生成json文件 WHITE_DICT = { "python": [ "api-ms-win-core", "base_library.zip", ".tcl", "tclIndex", "MSVCP140.dll", "cacert.pem", "cp936.enc", "__init__", "python.exe", "pythonw.exe", "VCRUNTIME140_1.dll", ], "matplotlib": ["matplotlibrc", ".load-order", "matplotlib.svg"], "request": ["msgcat-1.6.1.tm"], "plotly": ["plotly.json", "plotly.min.js", "package_data\\templates",'favicon.ico'], "kaleido": ["kaleido\\executable"], "pyecharts": ["pyecharts"], "pyqtwebengine": [ "QtWebEngineProcess.exe", "icudtl.dat", "qtwebengine_devtools_resources.pak", "qtwebengine_resources", "qt.conf", ], "streamlit": ["streamlit\\static"], "trame_vtk": ["static_viewer.html"], "python-docx": ["docx\\templates"], "python-pptx": ["pptx\\templates"], "setuptools": ["setuptools\\_vendor"] } WHITE_FILE_TYPE = [".pyi", ".py", ".pyc", ".pth", "._pth"] class WinGUI(Tk): """窗口类""" def __init__(self): super().__init__() self._win() self.tk_button_file = self._tk_button_file(self) self.tk_button_start = self._tk_button_start(self) self.tk_input_file = self._tk_input_file(self) self.tk_button_info = self._tk_button_info(self) self.tk_check_button_safe = self._tk_check_button_mode(self) self.tk_label_progressbar = self._tk_label_progressbar(self) self.tk_progressbar_progress = self._tk_progressbar_progress(self) def _win(self): """设置窗口属性""" self.title("打包瘦身小程序") width = 500 height = 150 screenwidth = self.winfo_screenwidth() screenheight = self.winfo_screenheight() geometry = f"{width}x{height}+{int((screenwidth - width) / 2)}+{int((screenheight - height) / 2)}" self.geometry(geometry) self.resizable(width=False, height=False) @staticmethod def _tk_button_file(parent) -> Button: """ 创建文件选择button :param parent:父组件对象 :return: button组件对象 """ btn = Button( parent, text="目标文件夹", takefocus=False, ) btn.place(x=5, y=10, width=100, height=30) return btn @staticmethod def _tk_button_start(parent) -> Button: """ 创建开始button :param parent: 父组件对象 :return: button组件对象 """ btn = Button( parent, text="开始处理", takefocus=False, ) btn.place(x=5, y=100, width=100, height=30) return btn @staticmethod def _tk_input_file(parent) -> Entry: """ 创建文件夹路径输入框 :param parent: 父组件对象 :return: entry组件对象 """ ipt = Entry( parent, ) ipt.place(x=110, y=10, width=370, height=30) return ipt @staticmethod def _tk_button_info(parent) -> Button: """ 创建程序说明button :param parent:父组件对象 :return: button对象 """ btn = Button( parent, text="使用说明", takefocus=False, ) btn.place(x=380, y=100, width=100, height=30) return btn @staticmethod def _tk_check_button_mode(parent) -> Checkbutton: """ 创建模式选择 :param parent: 父组件对象 :return: checkbutton对象 """ cb = Checkbutton( parent, text="当前:安全模式", ) cb.place(x=180, y=100, width=120, height=30) return cb @staticmethod def _tk_label_progressbar(parent) -> Label: """ 创建进度条标签 :param parent: 父组件对象 :return: label对象 """ label = Label(parent, text="处理进度", anchor="center") label.place(x=5, y=55, width=100, height=30) return label @staticmethod def _tk_progressbar_progress(parent) -> Progressbar: """ 创建进度条对象 :param parent:父组件对象 :return: 进度条对象 """ progressbar = Progressbar(parent, orient=HORIZONTAL) progressbar.place(x=110, y=55, width=370, height=30) return progressbar class MasterGui(WinGUI): """程序主窗口""" def __init__(self): super().__init__() icon = Path(__file__).parent.joinpath("file.ico") try: self.wm_iconbitmap(icon) except FileNotFoundError: pass except TclError: pass self.mode_var = BooleanVar() self.progress_var = DoubleVar() self.tk_button_file.config(command=self.select_file) self.tk_button_start.config(command=self.start_work) self.tk_button_info.config(command=self.sys_info) self.tk_check_button_safe.config(variable=self.mode_var) self.mode_var.set(True) self.mode_var.trace_add("write", self.mode_change) self.tk_progressbar_progress.config( variable=self.progress_var, length=100, ) self.file_name = "" self.white_dict = None self.white_list = [] self.ini_window() self.protocol("WM_DELETE_WINDOW", self.close_window) self.mainloop() def ini_window(self) -> None: """程序初始化白名单列表,通过读取白名单json文件或读取默认白名单字典""" if Path(WHITE_FILE).exists(): print("读取白名单配置文件", f"{WHITE_FILE}") with open(WHITE_FILE, "r", encoding="utf-8") as file: self.white_dict = json.load(file) try: assert isinstance(self.white_dict, dict) except AssertionError: print("读取的白名单配置文件不合法,请检查!") input("按回车键继续...") sys.exit() else: self.white_dict = WHITE_DICT for value in self.white_dict.values(): self.white_list.extend(value) @staticmethod def close_window(): """设置窗口关闭事件,保存默认白名单文件列表""" if not Path(WHITE_FILE).exists(): json_data = json.dumps(WHITE_DICT, indent=4) with open(WHITE_FILE, "w", encoding="utf-8") as json_file: json_file.write(json_data) print(f"已写入默认白名单配置文件,{WHITE_FILE},下次运行程序自动读取") messagebox.showinfo( title="保存", message=f"已写入默认白名单配置文件{WHITE_FILE}" ) sys.exit() def update_progress_safe(self, value: float) -> None: """ 安全地更新进度条的值 :param value: 进度 :return: None """ self.after(0, lambda: self.update_progress(value)) def update_progress(self, value: float) -> None: """ 更新进度条的值 :param value: 进度 :return: None """ self.progress_var.set(value) self.update_idletasks() def select_file(self) -> None: """设置需要处理的目标文件夹""" selected_path = filedialog.askdirectory() if selected_path == "": return selected_path = Path(selected_path) self.tk_input_file.delete(0, "end") self.tk_input_file.insert(0, str(selected_path)) def work_thread_func(self, white_list: List[str], file_path: Path) -> None: """ 在额外线程进行文件夹操作 :param white_list: 包含白名单文件的列表 :param file_path: 需要操作的目标文件夹路径 :return: None """ file_dir_new = Path(str(file_path) + "_new") only_binary_file = self.mode_var.get() file_list = FileRemove.get_file_paths(file_path, only_binary_file) file_move_lst = [] file_trans_lst = Path(file_path).parent.joinpath( f"{file_path.name}_文件移动清单.txt" ) file_count = len(file_list) for idx, filename in enumerate(file_list): if FileRemove.check_file(white_list, filename) and ( not FileRemove.is_file_in_use(filename) ): # 检查白名单和占用 relative_path = filename.relative_to(file_path) filename_new = file_dir_new.joinpath(relative_path) FileRemove.move_file_to_folder(filename, filename_new) file_move_lst.append(str(filename)) print(f"移动 {filename}") else: print(f"跳过 {filename}") self.update_progress_safe((idx + 1) / file_count * 100) if file_trans_lst.exists(): os.remove(str(file_trans_lst)) try: with open(file_trans_lst, "w", encoding="utf-8") as file: for temp_file in file_move_lst: file.write(f"{temp_file}\n") except PermissionError: messagebox.showerror( title="错误", message="权限不足,考虑用管理员权限重新运行程序" ) return messagebox.showinfo("提示", "文件移动结束!") self.tk_button_start.config(state=ACTIVE) def start_work(self): """开始处理""" file_path = self.tk_input_file.get() if file_path == "": return file_path = Path(file_path) if not file_path.exists(): return # 为了避免误操作C盘重要文件,本程序不允许在C盘进行操作 if "C" in file_path.drive: result = messagebox.askokcancel( title="警告", message=f"检测到当前需要处理的路径为C盘路径,\n程序可能因为权限不足无法正常工作。请确认是否执行\n待处理文件夹:{file_path}", ) if not result: return self.tk_progressbar_progress.config(value=0) file_move_thread = threading.Thread( target=self.work_thread_func, args=( self.white_list, file_path, ), daemon=True, ) self.tk_button_start.config(state=DISABLED) file_move_thread.start() def mode_change(self, *args) -> None: """ 模式切换点击事件 :param args: 无 :return: None """ value = self.mode_var.get() if value: self.tk_check_button_safe.config(text="当前:安全模式") else: self.tk_check_button_safe.config(text="当前:极限模式") @staticmethod def sys_info() -> None: """程序说明""" message = ( "运行原理:\n" "本程序仅在win7/win11测试过。" '程序通过指定文件目录,来移动该目录下未被程序占用的文件,移动的文件会在指定文件目录外的"目录_new"里面,同时生成文件移动清单.txt。\n\n' "使用方法:\n1,运行自己打包好的程序,尽量将功能全开,实现程序完全加载;\n2,使用本程序,指定程序目录,程序开始移动未占用的文件;\n" "3,移动结束后,请再运行一遍自己的程序,如果提示文件缺少,可以从《文件移动清单.txt》去找,还原即可。\n\n" "瘦身效果:\n根据我的历史使用记录,本程序针对pyinstaller打包的程序能缩小体积50%左右,针对nuitka打包的程序能缩小体积30%左右。\n" "注意事项:程序目前还不够完善,对于有些文件,是程序必须的,但是又在程序运行时不占用的,就需要将文件名加入白名单列表中。\n\n" "安全模式:程序只针对pyd,dll文件进行操作,瘦身成功率较高;\n" "极限模式:程序针对所有类型文件进行操作,瘦身效果较好,默认使用安全模式。\n\n" "程序第一次运行,会自动生成white_files.json文件,该文件记录了程序运行时不移动的文件,请根据使用情况酌情添加。" ) messagebox.showinfo(title="使用说明", message=message) class FileRemove: """文件移动函数类""" @staticmethod def get_file_paths(directory: Path, only_binary: bool = True) -> List[Path]: """ 获取指定路径所有文件名,only_binary设置是否只检测pyd,dll文件 :param directory: 目标路径 :param only_binary: 是否只检测二进制文件,即dll,pyd文件 :return: """ file_list = [] if only_binary: files_paths = [ path for pattern in ("*.pyd", "*.dll") for path in directory.glob("**/" + pattern) ] else: files_paths = directory.glob("**/*.*") for path in files_paths: if path.is_file(): file_list.append(path) return file_list @staticmethod def is_file_in_use(file_name: Path) -> bool: """ 判断该文件是否被占用,被占用即返回True :param file_name: 文件路径 :return: 是否是被占用状态 """ file_name = str(file_name) try: # 尝试以读写模式打开文件 with open(file_name, "r+", encoding="utf-8") as _: # 如果文件成功打开,说明没有被占用 return False except OSError: # 如果抛出OSError异常,说明文件被占用 return True except Exception as e: # 处理其他可能的异常 print(f"An error occurred: {e}") return True @staticmethod def move_file_to_folder(file_old: Path, file_new: Path) -> None: """ 使用Path.rename方法移动文件 :param file_old: 文件原路径 :param file_new: 文件新路径 :return: None """ if not file_new.parent.exists(): try: os.makedirs(file_new.parent) except (PermissionError, FileNotFoundError) as e: print(e) try: file_old.rename(file_new) except Exception as e: print(e) @staticmethod def check_file(white_list: List[str], file: Path) -> bool: """ 通过检测字符串包含的方法,判断该文件是否在白名单中,或在白名单文件类型中,如果在即返回False :param white_list: 白名单文件列表 :param file: 文件路径 :return: """ file = str(file) for data in white_list: if data in file: return False for file_type in WHITE_FILE_TYPE: if Path(file).suffix == file_type: return False return True def start(): """本模块外部接口,便于打包""" win = MasterGui() if __name__ == "__main__": win = MasterGui()
python
MIT
7ccd528adbdb5b74eb88eff1aa10fd05c42a28b5
2026-01-05T07:13:15.175986Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/convert_sillytavern_cards_to_data.py
convert_sillytavern_cards_to_data.py
from PIL import Image from bw_utils import get_child_paths,lang_detect,is_image,decode_base64,save_json_file,create_dir import PIL.PngImagePlugin import os import json card_dir = "./data/sillytavern_cards" names = os.listdir(card_dir) for name in names: path = os.path.join(card_dir, name) role_code = name.split('.')[0].replace(" ","_") if is_image(path): with open(path, 'rb',encoding="utf-8") as f: image = Image.open(f) card_info = json.loads(decode_base64(image.text['chara'])) language = lang_detect(card_info['data']['description']) role_info = { "role_code": f"{role_code}-{language}", "role_name": card_info['data']['name'], "source": "", "profile": card_info['data']['description'], "nickname": card_info['data']['name'], "relation": {}, "card_data": card_info['data'] } create_dir(f"./data/roles/sillytavern/{role_code}") save_json_file(os.path.join(f"./data/roles/sillytavern/{role_code}","role_info.json"),role_info) image.save(os.path.join(f"./data/roles/sillytavern/{role_code}",f"icon.png")) print(f"{name} converted successfully.")
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/bw_utils.py
bw_utils.py
import os import pickle import json import logging import datetime import re import random import base64 MODEL_NAME_DICT = { "gpt-3.5":"openai/gpt-3.5-turbo", "gpt-4":"openai/gpt-4", "gpt-4o":"openai/gpt-4o", "gpt-4o-mini":"openai/gpt-4o-mini", "gpt-3.5-turbo":"openai/gpt-3.5-turbo", "deepseek-r1":"deepseek/deepseek-r1", "deepseek-v3":"deepseek/deepseek-chat", "gemini-2.0-flash":"google/gemini-2.0-flash-001", "gemini-1.5-flash":"google/gemini-flash-1.5", "llama3-70b": "meta-llama/llama-3.3-70b-instruct", "qwen-turbo":"qwen/qwen-turbo", "qwen-plus":"qwen/qwen-plus", "qwen-max":"qwen/qwen-max", "qwen-2.5-72b":"qwen/qwen-2.5-72b-instruct", "claude-3.5-haiku": "anthropic/claude-3.5-haiku", "claude-3.5-sonnet":"anthropic/claude-3.5-sonnet", "claude-3.7-sonnet":"anthropic/claude-3.7-sonnet", "phi-4":"microsoft/phi-4", } def get_models(model_name): if os.getenv("OPENROUTER_API_KEY", default="") and model_name in MODEL_NAME_DICT: from modules.llm.OpenRouter import OpenRouter return OpenRouter(model=MODEL_NAME_DICT[model_name]) elif model_name.startswith('gpt'): from modules.llm.LangChainGPT import LangChainGPT if model_name.startswith('gpt-3.5'): return LangChainGPT(model="gpt-3.5-turbo") elif model_name == 'gpt-4' or model_name == 'gpt-4-turbo': return LangChainGPT(model="gpt-4") elif model_name == 'gpt-4o': return LangChainGPT(model="gpt-4o") elif model_name == "gpt-4o-mini": return LangChainGPT(model="gpt-4o-mini") elif model_name.startswith("claude"): from modules.llm.Claude import Claude if model_name.startswith("claude-3.5-sonnet"): return Claude(model="claude-3-5-sonnet-latest") elif model_name.startswith("claude-3.7-sonnet"): return Claude(model="claude-3-7-sonnet-latest") elif model_name.startswith("claude-3.5-haiku"): return Claude(model="claude-3-5-haiku-latest") return Claude() elif model_name.startswith('qwen'): from modules.llm.Qwen import Qwen return Qwen(model = model_name) elif model_name.startswith('deepseek'): from modules.llm.DeepSeek import DeepSeek return DeepSeek(model = model_name) elif model_name.startswith('doubao'): from modules.llm.Doubao import Doubao return Doubao() elif model_name.startswith('gemini'): from modules.llm.Gemini import Gemini if model_name.startswith('gemini-2.0'): return Gemini(model="gemini-2.0-flash") elif model_name.startswith('gemini-1.5'): return Gemini(model="gemini-1.5-flash") elif model_name.startswith('gemini-2.5-flash'): return Gemini(model="gemini-2.5-flash-preview-04-17") elif model_name.startswith('gemini-2.5-pro'): return Gemini(model="gemini-2.5-pro-preview-05-06") return Gemini() else: print(f'Warning! undefined model {model_name}, use gpt-4o-mini instead.') from modules.llm.LangChainGPT import LangChainGPT return LangChainGPT() def build_world_agent_data(world_file_path,max_words = 30): world_dir = os.path.dirname(world_file_path) details_dir = os.path.join(world_dir,"./world_details") data = [] settings = [] if os.path.exists(details_dir): for path in get_child_paths(details_dir): if os.path.splitext(path)[-1] == ".txt": text = load_text_file(path) data += split_text_by_max_words(text,max_words) if os.path.splitext(path)[-1] == ".jsonl": jsonl = load_jsonl_file(path) data += [f"{dic['term']}:{dic['detail']}" for dic in jsonl] settings += jsonl return data,settings def build_db(data, db_name, db_type, embedding, save_type="persistent"): if not data or not db_name: return None if True: from modules.db.ChromaDB import ChromaDB db = ChromaDB(embedding,save_type) db_name = db_name db.init_from_data(data,db_name) return db def get_root_dir(): current_file_path = os.path.abspath(__file__) root_dir = os.path.dirname(current_file_path) return root_dir def create_dir(dirname): if not os.path.exists(dirname): os.makedirs(dirname) def get_logger(experiment_name): logger = logging.getLogger(experiment_name) logger.setLevel(logging.INFO) current_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") create_dir(f"{get_root_dir()}/log/{experiment_name}") file_handler = logging.FileHandler(os.path.join(get_root_dir(),f"./log/{experiment_name}/{current_time}.log"),encoding='utf-8') file_handler.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') file_handler.setFormatter(formatter) logger.addHandler(file_handler) # Avoid logging duplication logger.propagate = False return logger def merge_text_with_limit(text_list, max_words, language = 'en'): """ Merge a list of text strings into one, stopping when adding another text exceeds the maximum count. Args: text_list (list): List of strings to be merged. max_count (int): Maximum number of characters (for Chinese) or words (for English). is_chinese (bool): If True, count Chinese characters; if False, count English words. Returns: str: The merged text, truncated as needed. """ merged_text = "" current_count = 0 for text in text_list: if language == 'zh': # Count Chinese characters text_length = len(text) else: # Count English words text_length = len(text.split(" ")) if current_count + text_length > max_words: break merged_text += text + "\n" current_count += text_length return merged_text def normalize_string(text): # 去除空格并将所有字母转为小写 import re return re.sub(r'[\s\,\;\t\n]+', '', text).lower() def fuzzy_match(str1, str2, threshold=0.8): str1_normalized = normalize_string(str1) str2_normalized = normalize_string(str2) if str1_normalized == str2_normalized: return True return False def load_character_card(path): from PIL import Image import PIL.PngImagePlugin image = Image.open(path) if isinstance(image, PIL.PngImagePlugin.PngImageFile): for key, value in image.text.items(): try: character_info = json.loads(decode_base64(value)) if character_info: return character_info except: continue return None def decode_base64(encoded_string): # Convert the string to bytes if it's not already if isinstance(encoded_string, str): encoded_bytes = encoded_string.encode('ascii') else: encoded_bytes = encoded_string # Decode the Base64 bytes decoded_bytes = base64.b64decode(encoded_bytes) # Try to convert the result to a string, assuming UTF-8 encoding try: decoded_string = decoded_bytes.decode('utf-8') return decoded_string except UnicodeDecodeError: # If it's not valid UTF-8 text, return the raw bytes return decoded_bytes def remove_list_elements(list1, *args): for target in args: if isinstance(target,list) or isinstance(target,dict): list1 = [i for i in list1 if i not in target] else: list1 = [i for i in list1 if i != target] return list1 def extract_html_content(html): from bs4 import BeautifulSoup soup = BeautifulSoup(html, "html.parser") content_div = soup.find("div", {"id": "content"}) if not content_div: return "" paragraphs = [] for div in content_div.find_all("div"): paragraphs.append(div.get_text(strip=True)) main_content = "\n\n".join(paragraphs) return main_content def load_text_file(path): with open(path,"r",encoding="utf-8") as f: text = f.read() return text def save_text_file(path,target): with open(path,"w",encoding="utf-8") as f: text = f.write(target) def load_json_file(path): with open(path,"r",encoding="utf-8") as f: return json.load(f) def save_json_file(path,target): dir_name = os.path.dirname(path) if not os.path.exists(dir_name): os.makedirs(dir_name) with open(path,"w",encoding="utf-8") as f: json.dump(target, f, ensure_ascii=False,indent=True) def load_jsonl_file(path): data = [] with open(path,"r",encoding="utf-8") as f: for line in f: data.append(json.loads(line)) return data def save_jsonl_file(path,target): with open(path, "w",encoding="utf-8") as f: for row in target: print(json.dumps(row, ensure_ascii=False), file=f) def split_text_by_max_words(text: str, max_words: int = 30): segments = [] current_segment = [] current_length = 0 lines = text.splitlines() for line in lines: words_in_line = len(line) current_segment.append(line + '\n') current_length += words_in_line if current_length + words_in_line > max_words: segments.append(''.join(current_segment)) current_segment = [] current_length = 0 if current_segment: segments.append(''.join(current_segment)) return segments def lang_detect(text): import re def count_chinese_characters(text): # 使用正则表达式匹配所有汉字字符 chinese_chars = re.findall(r'[\u4e00-\u9fff]', text) return len(chinese_chars) if count_chinese_characters(text) > len(text) * 0.05: lang = 'zh' else: lang = 'en' return lang def dict_to_str(dic): res = "" for key in dic: res += f"{key}: {dic[key]};" return res def count_tokens_num(string, encoding_name = "cl100k_base"): encoding = tiktoken.get_encoding(encoding_name) num_tokens = len(encoding.encode(string)) return num_tokens def json_parser(output): output = output.replace("\n", "") output = output.replace("\t", "") if "{" not in output: output = "{" + output if "}" not in output: output += "}" pattern = r'\{.*\}' matches = re.findall(pattern, output, re.DOTALL) try: parsed_json = eval(matches[0]) except: try: parsed_json = json.loads(matches[0]) except json.JSONDecodeError: try: detail = re.search(r'"detail":\s*(.+?)\s*}', matches[0]).group(1) detail = f"\"{detail}\"" new_output = re.sub(r'"detail":\s*(.+?)\s*}', f"\"detail\":{detail}}}", matches[0]) parsed_json = json.loads(new_output) except Exception as e: raise ValueError("No valid JSON found in the input string") return parsed_json def action_detail_decomposer(detail): thoughts = re.findall(r'【(.*?)】', detail) actions = re.findall(r'((.*?))', detail) dialogues = re.findall(r'「(.*?)」', detail) return thoughts,actions,dialogues def conceal_thoughts(detail): text = re.sub(r'【.*?】', '', detail) text = re.sub(r'\[.*?\]', '', text) return text def extract_first_number(text): match = re.search(r'\b\d+(?:\.\d+)?\b', text) return int(match.group()) if match else None def check_role_code_availability(role_code,role_file_dir): for path in get_grandchild_folders(role_file_dir): if role_code in path: return True return False def get_grandchild_folders(root_folder, if_full = True): folders = [] for resource in os.listdir(root_folder): subpath = os.path.join(root_folder,resource) for folder_name in os.listdir(subpath): folder_path = os.path.join(subpath, folder_name) if if_full: folders.append(folder_path) else: folders.append(folder_name) return folders def get_child_folders(root_folder, if_full = True): folders = [] for resource in os.listdir(root_folder): if if_full: path = os.path.join(root_folder,resource) if os.path.isdir(path): folders.append(path) else: path = resource if os.path.isdir(os.path.join(root_folder, path)): folders.append(path) return folders def get_child_paths(root_folder, if_full = True): paths = [] for resource in os.listdir(root_folder): if if_full: path = os.path.join(root_folder,resource) if os.path.isfile(path): paths.append(path) else: path = resource if os.path.isfile(os.path.join(root_folder, path)): paths.append(path) return paths def get_first_directory(path): try: for item in os.listdir(path): full_path = os.path.join(path, item) if os.path.isdir(full_path): return full_path return None except Exception as e: print(f"Error: {e}") return None def find_files_with_suffix(directory, suffix): matched_files = [] for root, dirs, files in os.walk(directory): # 遍历目录及其子目录 for file in files: if file.endswith(suffix): # 检查文件后缀 matched_files.append(os.path.join(root, file)) # 将符合条件的文件路径加入列表 return matched_files def remove_element_with_probability(lst, threshold=3, probability=0.2): # 确保列表不为空 if len(lst) > threshold and random.random() < probability: # 随机选择一个元素的索引 index = random.randint(0, len(lst) - 1) # 删除该索引位置的元素 lst.pop(index) return lst def count_token_num(text): from transformers import GPT2TokenizerFast tokenizer = GPT2TokenizerFast.from_pretrained('gpt2') return len(tokenizer.encode(text)) def get_cost(model_name,prompt,output): input_price=0 output_price=0 if model_name.startswith("gpt-4"): input_price=10 output_price=30 elif model_name.startswith("gpt-3.5"): input_price=0.5 output_price=1.5 return input_price*count_token_num(prompt)/1000000 + output_price * count_token_num(output)/1000000 def is_image(filepath): if not os.path.isfile(filepath): return False valid_image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff','.webp'] file_extension = os.path.splitext(filepath)[1].lower() # 判断扩展名是否在有效图片扩展名列表中 if file_extension in valid_image_extensions: return True return False def clean_collection_name(name: str) -> str: cleaned_name = name.replace(' ', '_') cleaned_name = cleaned_name.replace('.', '_') if not all(ord(c) < 128 for c in cleaned_name): encoded = base64.b64encode(cleaned_name.encode('utf-8')).decode('ascii') encoded = encoded[:60] if len(encoded) > 60 else encoded valid_name = f"mem_{encoded}" else: valid_name = cleaned_name valid_name = re.sub(r'[^a-zA-Z0-9_-]', '-', valid_name) valid_name = re.sub(r'\.\.+', '-', valid_name) valid_name = re.sub(r'^[^a-zA-Z0-9]+', '', valid_name) # 移除开头非法字符 valid_name = re.sub(r'[^a-zA-Z0-9]+$', '', valid_name) valid_name = valid_name[:60] return valid_name cache_sign = True cache = None def cached(func): def wrapper(*args,**kwargs): global cache cache_path = "bw_cache.pkl" if cache == None: if not os.path.exists(cache_path): cache = {} else: cache = pickle.load(open(cache_path, 'rb')) key = (func.__name__, str([args[0].role_code, args[0].__class__, args[0].llm_name , args[0].history]), str(kwargs.items())) if (cache_sign and key in cache and cache[key] not in [None, '[TOKEN LIMIT]']) : return cache[key] else: result = func(*args, **kwargs) if result != 'busy' and result != None: cache[key] = result pickle.dump(cache, open(cache_path, 'wb')) return result return wrapper
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/BookWorld.py
BookWorld.py
from datetime import datetime from tqdm import tqdm import json import os import warnings import random from typing import Any, Dict, List, Optional, Literal from collections import defaultdict import uuid from bw_utils import * from modules.main_role_agent import RPAgent from modules.world_agent import WorldAgent from modules.history_manager import HistoryManager from modules.embedding import get_embedding_model import argparse warnings.filterwarnings('ignore') class Server(): def __init__(self, preset_path: str, world_llm_name: str, role_llm_name: str, embedding_name:str = "bge-small") : """ The initialization function of the system. Args: preset_path (str): The path to config file of this experiment. world_llm_name (str, optional): The base model of the world agent. Defaults to 'gpt-4o'. role_llm_name (str, optional): The base model of all the role agents. Defaults to 'gpt-4o'. mode (str, optional): If set to be 'script', the role agents will act according to the given script. If set to be 'free', the role agents will act freely based on their backround. Defaults to 'free'. """ self.role_llm_name: str = role_llm_name self.world_llm_name: str = world_llm_name self.embedding_name:str = embedding_name config = load_json_file(preset_path) self.preset_path = preset_path self.config: Dict = config self.experiment_name: str = os.path.basename(preset_path).replace(".json","") + "/" + config["experiment_subname"] + "_" + role_llm_name role_agent_codes: List[str] = config['role_agent_codes'] world_file_path: str = config["world_file_path"] map_file_path: str = config["map_file_path"] if "map_file_path" in config else "" role_file_dir: str = config["role_file_dir"] if "role_file_dir" in config else "./data/roles/" loc_file_path: str = config["loc_file_path"] self.intervention: str = config["intervention"] if "intervention" in config else "" self.event = self.intervention self.script: str = config["script"] if "script" in config else "" self.language: str = config["language"] if "language" in config else "zh" self.source:str = config["source"] if "source" in config else "" self.idx: int = 0 self.cur_round: int = 0 self.progress: str = "剧本刚刚开始,还什么都没有发生" if self.language == 'zh' else "The story has just begun, nothing happens yet." self.moving_roles_info: Dict[str, Any] = {} self.history_manager = HistoryManager() self.start_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") self.current_status = { "location_code":"", "group":role_agent_codes, } self.scene_characters = {} self.event_history = [] self.role_llm = get_models(role_llm_name) self.logger = get_logger(self.experiment_name) self.embedding = get_embedding_model(embedding_name, language=self.language) self.init_role_agents(role_agent_codes = role_agent_codes, role_file_dir = role_file_dir, world_file_path=world_file_path, llm = self.role_llm, embedding = self.embedding) if world_llm_name == role_llm_name: self.world_llm = self.role_llm else: self.world_llm = get_models(world_llm_name) self.init_world_agent_from_file(world_file_path = world_file_path, map_file_path = map_file_path, loc_file_path = loc_file_path, llm = self.world_llm, embedding = self.embedding) # Init def init_role_agents(self, role_agent_codes: List[str], role_file_dir:str, world_file_path:str, llm=None, embedding=None) -> None: self.role_codes: List[str] = role_agent_codes self.role_agents: Dict[str, RPAgent] = {} for role_code in role_agent_codes: if check_role_code_availability(role_code,role_file_dir): self.role_agents[role_code] = RPAgent(role_code=role_code, role_file_dir=role_file_dir, world_file_path=world_file_path, source = self.source, language=self.language, llm_name = self.role_llm_name, llm = llm, embedding_name=self.embedding_name, embedding = embedding ) # print(f"{role_code} Initialized.") else: print(f"Warning: The specified role `{role_code}` does not exist.") def init_world_agent_from_file(self, world_file_path: str, map_file_path: str, loc_file_path: str, llm=None, embedding=None) -> None: self.world_agent: WorldAgent = WorldAgent(world_file_path = world_file_path, location_file_path = loc_file_path, map_file_path = map_file_path, llm_name=self.world_llm_name, llm = llm, embedding_name=self.embedding_name, embedding = embedding, language=self.language) for role_code in self.role_agents: self.role_agents[role_code].world_db = self.world_agent.db self.role_agents[role_code].world_db_name = self.world_agent.db_name def init_role_locations(self, random_allocate: bool = True): """ Set initial positions of the roles. Args: random_allocate (bool, optional): if set to be True, the initial positions of the roles are randomly assigned. Defaults to True. """ init_locations_code = random.choices(self.world_agent.locations, k = len(self.role_codes)) for i,role_code in enumerate(self.role_codes): self.role_agents[role_code].set_location(init_locations_code[i], self.world_agent.find_location_name(init_locations_code[i])) info_text = f"{self.role_agents[role_code].nickname} 现在位于 {self.world_agent.find_location_name(init_locations_code[i])}" \ if self.language == "zh" else f"{self.role_agents[role_code].nickname} is now located at {self.world_agent.find_location_name(init_locations_code[i])}" self.log(info_text) def reset_llm(self, role_llm_name, world_llm_name): self.role_llm = get_models(role_llm_name) for role_code in self.role_codes: self.role_agents[role_code].llm = self.role_llm self.role_agents[role_code].llm_name = role_llm_name if world_llm_name == role_llm_name: self.world_llm = self.role_llm else: self.world_llm = get_models(world_llm_name) self.world_agent.llm = self.world_llm self.role_llm_name = role_llm_name self.world_llm_name = world_llm_name # Simulation def simulate_generator(self, rounds: int = 10, save_dir: str = "", if_save: Literal[0,1] = 0, mode: Literal["free", "script"] = "free", scene_mode: Literal[0,1] = 1,): """ The main function of the simulation. Args: rounds (int, optional): The max rounds of simulation. Defaults to 10. save_dir (str, optional): _description_. Defaults to "". if_save (Literal[0,1], optional): _description_. Defaults to 0. """ self.mode = mode meta_info: Dict[str, Any] = self.continue_simulation_from_file(save_dir) self.if_save: int = if_save start_round: int = meta_info["round"] sub_start_round:int = meta_info["sub_round"] if "sub_round" in meta_info else 0 if start_round == rounds: return # Setting Locations if not meta_info["location_setted"]: self.log("========== Start Location Setting ==========") self.init_role_locations() self._save_current_simulation("location") # Setting Goals if not meta_info["goal_setted"]: yield ("system","","-- Setting Goals --",None) self.log("========== Start Goal Setting ==========") if self.mode == "free": self.get_event() self.log(f"--------- Free Mode: Current Event ---------\n{self.event}\n") yield ("system","",f"--------- Current Event ---------\n{self.event}\n", None) self.event_history.append(self.event) elif self.mode == "script": self.get_script() self.log(f"--------- Script Mode: Setted Script ---------\n{self.script}\n") yield ("system","",f"--------- Setted Script ---------\n{self.script}\n", None) self.event_history.append(self.event) if self.mode == "free": for role_code in self.role_codes: motivation = self.role_agents[role_code].set_motivation( world_description = self.world_agent.description, other_roles_info = self._get_group_members_info_dict(self.role_agents), intervention = self.event, script = self.script ) info_text = f"{self.role_agents[role_code].nickname} 设立了动机: {motivation}" \ if self.language == "zh" else f"{self.role_agents[role_code].nickname} has set the motivation: {motivation}" record_id = str(uuid.uuid4()) self.log(info_text) self.record(role_code=role_code, detail=info_text, actor = role_code, group = [role_code], actor_type = 'role', act_type="goal setting", record_id = record_id) yield ("role",role_code,info_text,record_id) self._save_current_simulation("goal") yield ("system","","-- Simulation Started --",None) selected_role_codes = [] # Simulating for current_round in range(start_round, rounds): self.cur_round = current_round self.log(f"========== Round {current_round+1} Started ==========") if self.event and current_round >= 1: self.log(f"--------- Current Event ---------\n{self.event}\n") yield ("world","","-- Current Event --\n"+self.event, None) self.event_history.append(self.event) if len(self.moving_roles_info) == len(self.role_codes): self.settle_movement() continue # Characters in next scene if scene_mode: group = self._name2code( self.world_agent.decide_scene_actors( self._get_locations_info(False), self.history_manager.get_recent_history(5), self.event, list(set(selected_role_codes + list(self.moving_roles_info.keys()))))) selected_role_codes += group if len(selected_role_codes) >= len(self.role_codes): selected_role_codes = [] else: group = self.role_codes self.current_status['group'] = group self.current_status['location_code'] = self.role_agents[group[0]].location_code self.scene_characters[str(current_round)] = group # Prologue # if current_round == 0 and len(group) > 0 # prologue = self.world_agent.generate_location_prologue(location_code=self.role_agents[group[0]].location_code, history_text=self._get_history_text(group),event=self.event,location_info_text=self._find_roles_at_location(self.role_agents[group[0]].location_code,name=True)) # self.log("--Prologue--: "+prologue) # self.record(role_code="None",detail=prologue,act_type="prologue") start_idx = len(self.history_manager) sub_round = sub_start_round for sub_round in range(sub_start_round,3): if self.mode == "script": self.script_instruct(self.progress) else: for role_code in group: self.role_agents[role_code].update_goal(other_roles_status=self._get_status_text(self.role_codes)) for role_code in group: if scene_mode: role_code = self._name2code(self.world_agent.decide_next_actor("\n".join(self.history_manager.get_recent_history(3)),self._get_group_members_info_text(group,status=True),self.script)) if scene_mode else role_code yield from self.implement_next_plan(role_code = role_code, group = group) self._save_current_simulation("action", current_round, sub_round) if_end,epilogue = self.world_agent.judge_if_ended("\n".join(self.history_manager.get_recent_history(len(self.history_manager)-start_idx))) if if_end: record_id = str(uuid.uuid4()) self.log("--Epilogue--: "+epilogue) self.record(role_code = "None", detail = epilogue, actor_type="world", act_type="epilogue", actor = "world", group = [], record_id = record_id) yield ("world","","--Epilogue--: "+epilogue, record_id) break for role_code in group: yield from self.decide_whether_to_move(role_code = role_code, group = self._find_group(role_code)) self.role_agents[role_code].update_status() self.settle_movement() self.update_event(group) sub_start_round = 0 self._save_current_simulation("action", current_round + 1,sub_round + 1) # Main functions using llm def implement_next_plan(self,role_code: str, group: List[str]): other_roles_info = self._get_group_members_info_dict(group) plan = self.role_agents[role_code].plan( other_roles_info = other_roles_info, available_locations = self.world_agent.locations, world_description = self.world_agent.description, intervention = self.event, ) info_text = plan["detail"] if plan["target_role_codes"]: plan["target_role_codes"] = self._name2code(plan["target_role_codes"]) record_id = str(uuid.uuid4()) self.log(f"-Action-\n{self.role_agents[role_code].role_name}: "+ info_text) self.record(role_code = role_code, detail = plan["detail"], actor_type = 'role', act_type = "plan", actor = role_code, group = plan["target_role_codes"] + [role_code], plan = plan, record_id = record_id ) yield ("role", role_code, info_text, record_id) if plan["interact_type"] == "single" and len(plan["target_role_codes"]) == 1 and plan["target_role_codes"][0] in group: yield from self.start_single_role_interaction(plan, record_id) elif plan["interact_type"] == "multi" and len(plan["target_role_codes"]) > 1 and set(plan["target_role_codes"]).issubset(set(group)) : yield from self.start_multi_role_interaction(plan, record_id) elif plan["interact_type"] == "enviroment": yield from self.start_enviroment_interaction(plan,role_code, record_id) elif plan["interact_type"] == "npc" and plan["target_npc_name"]: yield from self.start_npc_interaction(plan,role_code,target_name=plan["target_npc_name"], record_id = record_id) return info_text def decide_whether_to_move(self, role_code: str, group: List[str]): if len(self.world_agent.locations) <= 1: return False if_move, move_detail, destination_code = self.role_agents[role_code].move(locations_info_text = self._get_locations_info(), locations_info = self.world_agent.locations_info) if if_move: self.log(move_detail) print(f"角色选择移动。{self.role_agents[role_code].role_name}正在前往{self.world_agent.find_location_name(destination_code)}" if self.language == "zh" else f"The role decides to move. {self.role_agents[role_code].role_name} is heading to {self.world_agent.find_location_name(destination_code)}.") self.record(role_code = role_code, detail = move_detail, actor_type = 'role', act_type = "move", actor = role_code, group = [role_code], destinatiion_code = destination_code ) yield ("role",role_code,move_detail,None) distance = self.world_agent.get_distance(self.role_agents[role_code].location_code, destination_code) self.role_agents[role_code].set_location(location_code=None, location_name=None) self.moving_roles_info[role_code] = { "location_code":destination_code, "distance":distance } return if_move def start_enviroment_interaction(self, plan: Dict[str, Any], role_code: str, record_id: str): """ Handles the role's interaction with the environment. It gets interaction results from agents in the world, record the result and update the status of the role. Args: plan (Dict[str, Any]): The details of the action. role_code (str): The action maker. Returns: (str): The enviroment response. """ if "action" not in plan: plan["action"] = "" self.current_status['group'] = [role_code] location_code = self.role_agents[role_code].location_code result = self.world_agent.enviroment_interact(action_maker_name = self.role_agents[role_code].role_name, action = plan["action"], action_detail = conceal_thoughts(self.history_manager.search_record_detail(record_id)), location_code = location_code) env_record_id = str(uuid.uuid4()) self.log(f"(Enviroment):{result}") self.record(role_code = role_code, detail = result, actor_type = 'world', act_type = "enviroment", initiator = role_code, actor = "world", group = [role_code], record_id = env_record_id) yield ("world","","(Enviroment):" + result, env_record_id) return conceal_thoughts(self.history_manager.search_record_detail(record_id)) + self.history_manager.search_record_detail(env_record_id) def start_npc_interaction(self, plan: Dict[str, Any], role_code: str, target_name: str, record_id: str, max_rounds: int = 3): """ Handles the role's interaction with the environment. It gets interaction results from agents in the world, record the result and update the status of the role. Args: plan (Dict[str, Any]): The details of the action. role_code (str): The action maker. target_name (str): The target npc. Returns: (str): The enviroment response. """ interaction = plan start_idx = len(self.history_manager) self.log(f"----------NPC Interaction----------\n") self.current_status['group'] = [role_code,target_name] for round in range(max_rounds): npc_interaction = self.world_agent.npc_interact(action_maker_name=self.role_agents[role_code].role_name, action_detail=self.history_manager.search_record_detail(record_id), location_name=self.role_agents[role_code].location_name, target_name=target_name) npc_detail = npc_interaction["detail"] npc_record_id = str(uuid.uuid4()) self.log(f"{target_name}: " + npc_detail) self.record(role_code = role_code, detail = npc_detail, actor_type = 'world', act_type = "npc", actor = "world", group = [role_code], npc_name = target_name, record_id = npc_record_id ) yield ("world","",f"(NPC-{target_name}):" + npc_detail, npc_record_id) if npc_interaction["if_end_interaction"]: break interaction = self.role_agents[role_code].npc_interact( npc_name = target_name, npc_response = self.history_manager.search_record_detail(npc_record_id), history = self.history_manager.get_subsequent_history(start_idx = start_idx), intervention = self.event ) detail = interaction["detail"] record_id = str(uuid.uuid4()) self.log(f"{self.role_agents[role_code].role_name}: " + detail) self.record(role_code = role_code, detail = detail, actor_type = 'role', act_type = "npc", actor = role_code, group = [role_code], npc_name = target_name, record_id = record_id) yield ("role",role_code,detail,record_id) if interaction["if_end_interaction"]: break if_end,epilogue = self.world_agent.judge_if_ended("\n".join(self.history_manager.get_subsequent_history(start_idx))) if if_end: break return "\n".join(self.history_manager.get_subsequent_history(start_idx = start_idx)) def start_single_role_interaction(self, plan: Dict[str, Any], record_id: str, max_rounds: int = 8): interaction = plan acted_role_code = interaction["role_code"] acting_role_code = interaction["target_role_codes"][0] if acting_role_code not in self.role_codes: print(f"Warning: Role {acting_role_code} does not exist.") return self.current_status['group'] = [acted_role_code,acting_role_code] start_idx = len(self.history_manager) for round in range(max_rounds): interaction = self.role_agents[acting_role_code].single_role_interact( action_maker_code = acted_role_code, action_maker_name = self.role_agents[acted_role_code].role_name, action_detail = conceal_thoughts(self.history_manager.search_record_detail(record_id)), action_maker_profile = self.role_agents[acted_role_code].role_profile, intervention = self.event ) detail = interaction["detail"] record_id = str(uuid.uuid4()) self.log(f"{self.role_agents[acting_role_code].role_name}: " + detail) self.record(role_code = acting_role_code, detail = detail, actor_type = 'role', act_type = "single", group = [acted_role_code,acting_role_code], target_role_code = acting_role_code, planning_role_code = plan["role_code"], round = round, record_id = record_id ) yield ("role",acting_role_code,detail,record_id) if interaction["if_end_interaction"]: return if interaction["extra_interact_type"] == "npc": print("---Extra NPC Interact---") result = yield from self.start_npc_interaction(plan=interaction, role_code=acted_role_code, target_name=interaction["target_npc_name"], record_id=record_id) interaction["detail"] = result elif interaction["extra_interact_type"] == "enviroment": print("---Extra Env Interact---") result = yield from self.start_enviroment_interaction(plan=interaction,role_code=acted_role_code,record_id=record_id) interaction["detail"] = result if_end,epilogue = self.world_agent.judge_if_ended("\n".join(self.history_manager.get_subsequent_history(start_idx))) if if_end: break acted_role_code,acting_role_code = acting_role_code,acted_role_code return def start_multi_role_interaction(self, plan: Dict[str, Any], record_id: str, max_rounds: int = 8): interaction = plan acted_role_code = interaction["role_code"] group = interaction["target_role_codes"] group.append(acted_role_code) for code in group: if code not in self.role_codes: print(f"Warning: Role {code} does not exist.") return self.current_status['group'] = group start_idx = len(self.history_manager) other_roles_info = self._get_group_members_info_dict(group) for round in range(max_rounds): acting_role_code = self._name2code(self.world_agent.decide_next_actor(history_text = "\n".join(self.history_manager.get_recent_history(3)), roles_info_text = self._get_group_members_info_text(remove_list_elements(group,acted_role_code),status=True))) interaction = self.role_agents[acting_role_code].multi_role_interact( action_maker_code = acted_role_code, action_maker_name = self.role_agents[acted_role_code].role_name, action_detail = conceal_thoughts(self.history_manager.search_record_detail(record_id)), action_maker_profile = self.role_agents[acted_role_code].role_profile, other_roles_info = other_roles_info, intervention = self.event ) detail = interaction["detail"] record_id = str(uuid.uuid4()) self.log(f"{self.role_agents[acting_role_code].role_name}: "+ detail) self.record(role_code = acting_role_code, detail = detail, actor_type = 'role', act_type = "multi", group = group, actor = acting_role_code, planning_role_code = plan["role_code"], round = round, record_id = record_id ) yield ("role",acting_role_code,detail,record_id) if interaction["if_end_interaction"]: break result = "" if interaction["extra_interact_type"] == "npc": print("---Extra NPC Interact---") result = yield from self.start_npc_interaction(plan=interaction,role_code=acting_role_code,target_name=interaction["target_npc_name"],record_id = record_id) elif interaction["extra_interact_type"] == "enviroment": print("---Extra Env Interact---") result = yield from self.start_enviroment_interaction(plan=interaction,role_code=acting_role_code,record_id = record_id) interaction["detail"] = self.history_manager.search_record_detail(record_id) + result acted_role_code = acting_role_code if_end,epilogue = self.world_agent.judge_if_ended("\n".join(self.history_manager.get_subsequent_history(start_idx))) if if_end: break return # Sub functions using llm def script_instruct(self, last_progress: str, top_k: int = 5): """ Under the script mode, generate instruction for the roles at the beginning of each round. Args: last_progress (str): Where the script went in the last round. top_k (int, optional): The number of action history of each role to refer. Defaults to 1. Returns: Dict[str, Any]: Instruction for each role. """ roles_info_text = self._get_group_members_info_text(self.role_codes,status=True) history_text = self.history_manager.get_recent_history(top_k) instruction = self.world_agent.get_script_instruction( roles_info_text=roles_info_text, event = self.event, history_text=history_text, script=self.script, last_progress = last_progress) for code in instruction: if code == "progress": self.log("剧本进度:"+ instruction["progress"]) if self.language == "zh" else self.log("Current Stage:"+ instruction["progress"]) elif code in self.role_codes: # self.role_agents[code].update_goal(instruction = instruction[code]) self.role_agents[code].goal = instruction[code] else:
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
true
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/server.py
server.py
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, HTTPException, Request from fastapi.staticfiles import StaticFiles from fastapi.responses import HTMLResponse, FileResponse import uvicorn import json import asyncio import os from pathlib import Path from datetime import datetime from bw_utils import is_image, load_json_file from BookWorld import BookWorld app = FastAPI() default_icon_path = './frontend/assets/images/default-icon.jpg' config = load_json_file('config.json') for key in config: if "API_KEY" in key and config[key]: os.environ[key] = config[key] static_file_abspath = os.path.abspath(os.path.join(os.path.dirname(__file__), 'frontend')) app.mount("/frontend", StaticFiles(directory=static_file_abspath), name="frontend") # 预设文件目录 PRESETS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'experiment_presets') class ConnectionManager: def __init__(self): self.active_connections: dict[str, WebSocket] = {} self.story_tasks: dict[str, asyncio.Task] = {} if True: if "preset_path" in config and config["preset_path"]: if os.path.exists(config["preset_path"]): preset_path = config["preset_path"] else: raise ValueError(f"The preset path {config['preset_path']} does not exist.") elif "genre" in config and config["genre"]: genre = config["genre"] preset_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),f"./config/experiment_{genre}.json") else: raise ValueError("Please set the preset_path in `config.json`.") self.bw = BookWorld(preset_path = preset_path, world_llm_name = config["world_llm_name"], role_llm_name = config["role_llm_name"], embedding_name = config["embedding_model_name"]) self.bw.set_generator(rounds = config["rounds"], save_dir = config["save_dir"], if_save = config["if_save"], mode = config["mode"], scene_mode = config["scene_mode"],) else: from BookWorld_test import BookWorld_test self.bw = BookWorld_test() async def connect(self, websocket: WebSocket, client_id: str): await websocket.accept() self.active_connections[client_id] = websocket def disconnect(self, client_id: str): if client_id in self.active_connections: del self.active_connections[client_id] self.stop_story(client_id) def stop_story(self, client_id: str): if client_id in self.story_tasks: self.story_tasks[client_id].cancel() del self.story_tasks[client_id] async def start_story(self, client_id: str): if client_id in self.story_tasks: # 如果已经有任务在运行,先停止它 self.stop_story(client_id) # 创建新的故事任务 self.story_tasks[client_id] = asyncio.create_task( self.generate_story(client_id) ) async def generate_story(self, client_id: str): """持续生成故事的协程""" try: while True: if client_id in self.active_connections: message,status = await self.get_next_message() await self.active_connections[client_id].send_json({ 'type': 'message', 'data': message }) await self.active_connections[client_id].send_json({ 'type': 'status_update', 'data': status }) # 添加延迟,控制消息发送频率 await asyncio.sleep(0.2) # 可以调整这个值 else: break except asyncio.CancelledError: # 任务被取消时的处理 print(f"Story generation cancelled for client {client_id}") except Exception as e: print(f"Error in generate_story: {e}") async def get_initial_data(self): """获取初始化数据""" return { 'characters': self.bw.get_characters_info(), 'map': self.bw.get_map_info(), 'settings': self.bw.get_settings_info(), 'status': self.bw.get_current_status(), 'history_messages':self.bw.get_history_messages(save_dir = config["save_dir"]), } async def get_next_message(self): """从BookWorld获取下一条消息""" message = self.bw.generate_next_message() if not os.path.exists(message["icon"]) or not is_image(message["icon"]): message["icon"] = default_icon_path status = self.bw.get_current_status() return message,status manager = ConnectionManager() @app.get("/") async def get(): html_file = Path("index.html") return HTMLResponse(html_file.read_text(encoding="utf-8")) @app.get("/data/{full_path:path}") async def get_file(full_path: str): # 可以设置多个基础路径 base_paths = [ Path("/data/") ] for base_path in base_paths: file_path = base_path / full_path if file_path.exists() and file_path.is_file(): return FileResponse(file_path) else: return FileResponse(default_icon_path) raise HTTPException(status_code=404, detail="File not found") @app.get("/api/list-presets") async def list_presets(): try: # 获取所有json文件 presets = [f for f in os.listdir(PRESETS_DIR) if f.endswith('.json')] return {"presets": presets} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.post("/api/load-preset") async def load_preset(request: Request): try: data = await request.json() preset_name = data.get('preset') if not preset_name: raise HTTPException(status_code=400, detail="No preset specified") preset_path = os.path.join(PRESETS_DIR, preset_name) print(f"Loading preset from: {preset_path}") if not os.path.exists(preset_path): raise HTTPException(status_code=404, detail=f"Preset not found: {preset_path}") try: # 更新BookWorld实例的预设 manager.bw = BookWorld( preset_path=preset_path, world_llm_name=config["world_llm_name"], role_llm_name=config["role_llm_name"], embedding_name=config["embedding_model_name"] ) manager.bw.set_generator( rounds=config["rounds"], save_dir=config["save_dir"], if_save=config["if_save"], mode=config["mode"], scene_mode=config["scene_mode"] ) # 获取初始数据 initial_data = await manager.get_initial_data() return { "success": True, "data": initial_data } except Exception as e: print(f"Error initializing BookWorld: {str(e)}") raise HTTPException(status_code=500, detail=f"Error initializing BookWorld: {str(e)}") except Exception as e: print(f"Error in load_preset: {str(e)}") raise HTTPException(status_code=500, detail=str(e)) @app.websocket("/ws/{client_id}") async def websocket_endpoint(websocket: WebSocket, client_id: str): await manager.connect(websocket, client_id) try: initial_data = await manager.get_initial_data() await websocket.send_json({ 'type': 'initial_data', 'data': initial_data }) while True: data = await websocket.receive_text() message = json.loads(data) if message['type'] == 'user_message': # 处理用户消息 await websocket.send_json({ 'type': 'message', 'data': { 'username': 'User', 'timestamp': message['timestamp'], 'text': message['text'], 'icon': default_icon_path, } }) elif message['type'] == 'control': # 处理控制命令 if message['action'] == 'start': await manager.start_story(client_id) elif message['action'] == 'pause': manager.stop_story(client_id) elif message['action'] == 'stop': manager.stop_story(client_id) # 可以在这里添加额外的停止逻辑 elif message['type'] == 'edit_message': # 处理消息编辑 edit_data = message['data'] # 假设 BookWorld 类有一个处理编辑的方法 manager.bw.handle_message_edit( record_id=edit_data['uuid'], new_text=edit_data['text'] ) elif message['type'] == 'request_scene_characters': manager.bw.select_scene(message['scene']) scene_characters = manager.bw.get_characters_info() await websocket.send_json({ 'type': 'scene_characters', 'data': scene_characters }) elif message['type'] == 'generate_story': # 生成故事文本 story_text = manager.bw.generate_story() # 发送生成的故事作为新消息 await websocket.send_json({ 'type': 'message', 'data': { 'username': 'System', 'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'text': story_text, 'icon': default_icon_path, 'type': 'story' } }) except Exception as e: print(f"WebSocket error: {e}") finally: manager.disconnect(client_id) @app.post("/api/save-config") async def save_config(request: Request): global config global manager try: config_data = await request.json() # 检查必要字段是否存在 if 'provider' not in config_data or 'model' not in config_data or 'apiKey' not in config_data: raise HTTPException(status_code=400, detail="缺少必要的字段") llm_provider = config_data['provider'] model = config_data['model'] api_key = config_data['apiKey'] config['role_llm_name'] = model config['world_llm_name'] = model if 'openai' in llm_provider.lower(): os.environ['OPENAI_API_KEY'] = api_key elif 'anthropic' in llm_provider.lower(): os.environ['ANTHROPIC_API_KEY'] = api_key elif 'alibaba' in llm_provider.lower(): os.environ['DASHSCOPE_API_KEY'] = api_key elif 'openrouter' in llm_provider.lower(): os.environ['OPENROUTER_API_KEY'] = api_key manager.bw.server.reset_llm(model,model) return {"status": "success", "message": llm_provider + " 配置已保存"} except Exception as e: print(f"保存配置失败: {e}") raise HTTPException(status_code=500, detail="保存配置失败") if __name__ == "__main__": uvicorn.run("server:app", host="0.0.0.0", port=8000, reload=True)
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/extract_data/extract_data.py
extract_data/extract_data.py
import sys sys.path.append("../") from bw_utils import * from extract_utils import * import os import csv import json config = load_json_file("./extract_config.json") def get_default_character_info(role_name,language,other_role_names): default_character_info = { "role_name": role_name, "role_code": convert_name_to_code(role_name,language), "source": book_source, "profile": "", "activity":1, "nickname": role_name, "relation": {convert_name_to_code(role_name,language):{ "relation":[], "detail":"" } for role_name in other_role_names} } return default_character_info def get_defaule_location_info(location_name,language): default_location_info = { "location_name": location_name, "location_code": convert_name_to_code(location_name,language), "description": "", "detail": "", } return default_location_info def process_character_chunk(chunk, character_name, language): prompt = f"""Analyze the following text and extract information about {character_name}. Focus on their personality, background, and key characteristics. If the text doesn't contain relevant information about {character_name}, respond with 'NO_INFO'. Text: {chunk} Provide a concise summary of the character's profile.""" response = llm.chat(prompt) if response.strip() != 'NO_INFO': return response.strip() return None def process_character_relation(chunk, char1, char2, language): prompt = f"""Analyze the relationship between {char1} and {char2} in the following text. Focus on their interaction, feelings towards each other, and relationship dynamics. If the text doesn't contain relevant information about their relationship, respond with 'NO_INFO'. Text: {chunk} Provide a concise description of their relationship.""" response = llm.chat(prompt) if response.strip() != 'NO_INFO': return response.strip() return None def process_location_chunk(chunk, location_name, language): prompt = f"""Analyze the following text and extract information about {location_name}. Focus on its physical description, significance, and key features. If the text doesn't contain relevant information about {location_name}, respond with 'NO_INFO'. Text: {chunk} Provide a concise description of the location.""" response = llm.chat(prompt) if response.strip() != 'NO_INFO': return response.strip() return None def update_character_info(character_name, new_profile): if character_name not in target_characters_info: target_characters_info[character_name] = get_default_character_info( character_name, language, [name for name in target_character_names if name != character_name] ) if new_profile: current_profile = target_characters_info[character_name]["profile"] if current_profile: target_characters_info[character_name]["profile"] = current_profile + "\n" + new_profile else: target_characters_info[character_name]["profile"] = new_profile def update_character_relation(char1, char2, new_relation): if new_relation: char1_code = convert_name_to_code(char1, language) char2_code = convert_name_to_code(char2, language) if char1 not in target_characters_info: target_characters_info[char1] = get_default_character_info( char1, language, [name for name in target_character_names if name != char1] ) current_relation = target_characters_info[char1]["relation"][char2_code]["detail"] if current_relation: target_characters_info[char1]["relation"][char2_code]["detail"] = current_relation + "\n" + new_relation else: target_characters_info[char1]["relation"][char2_code]["detail"] = new_relation def update_location_info(location_name, new_description): location_code = convert_name_to_code(location_name,language) if location_code not in target_locations_info: target_locations_info[location_code] = get_defaule_location_info(location_name, language) if new_description: current_description = target_locations_info[location_code]["description"] if current_description: target_locations_info[location_code]["description"] = current_description + "\n" + new_description else: target_locations_info[location_code]["description"] = new_description def ensure_dir(path): """Ensure directory exists, create if not""" if not os.path.exists(path): os.makedirs(path) def save_role_info(role_info): """Save individual role information to specified path""" role_code = role_info["role_code"] role_dir = f"./data/roles/{book_source}/{role_code}" ensure_dir(role_dir) role_path = f"{role_dir}/role_info.json" save_json_file(role_path, role_info) return role_path def save_location_info(locations_info): """Save all location information to specified path""" location_dir = f"./data/locations" ensure_dir(location_dir) location_path = f"{location_dir}/{book_source}.json" save_json_file(location_path, locations_info) return location_path def save_world_info(world_info): world_dir = f"./data/worlds/{book_source}" ensure_dir(world_dir) world_path = f"{world_dir}/general.json" save_json_file(world_path, world_info) return world_path def save_map(target_location_names,default_distance = 1): location_codes = [convert_name_to_code(name,language) for name in target_location_names] map_dir = f"./data/maps" ensure_dir(map_dir) map_path = f"{map_dir}/{book_source}.csv" n = len(location_codes) matrix = [] header = [""] + location_codes matrix.append(header) for i in range(n): row = [location_codes[i]] for j in range(n): if i == j: row.append(0) else: row.append(default_distance) matrix.append(row) with open(map_path, "w", newline='', encoding="utf-8") as f: writer = csv.writer(f) writer.writerows(matrix) return map_path def save_json_file(path, data): """Save data as JSON file""" with open(path, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=2) def extract_main_characters(text, language): """Extract main characters from the text using LLM""" prompt = f"""Analyze the following text and identify the main characters. Focus on characters who: 1. Appear frequently throughout the story 2. Have significant impact on the plot 3. Are central to the narrative For each character, provide their name and a brief reason why they are important. Format: name: reason Text: {text} List the main characters (maximum 10):""" response = llm.chat(prompt) characters = [] for line in response.strip().split('\n'): if ':' in line: name = line.split(':')[0].strip() characters.append(name) return characters def extract_key_locations(text, language): """Extract key locations from the text using LLM""" prompt = f"""Analyze the following text and identify the key locations. Focus on places that: 1. Are frequently mentioned 2. Have significant importance to the story 3. Are central to the plot For each location, provide its name and a brief reason why it is important. Format: name: reason Text: {text} List the key locations (maximum 10):""" response = llm.chat(prompt) locations = [] for line in response.strip().split('\n'): if ':' in line: name = line.split(':')[0].strip() locations.append(name) return locations def process_large_text(text, max_length=10000): """Process large text by splitting into overlapping chunks""" chunks = [] start = 0 while start < len(text): end = start + max_length if end >= len(text): chunks.append(text[start:]) break # Find the last period or newline before max_length last_period = text.rfind('.', start, end) last_newline = text.rfind('\n', start, end) split_point = max(last_period, last_newline) if split_point == -1: split_point = end chunks.append(text[start:split_point + 1]) start = split_point + 1 return chunks def auto_extract_targets(): """Automatically extract target characters and locations from the book""" print("Starting automatic extraction of main characters and locations...") # Combine all chapter contents full_text = "" for chapter in data: full_text += chapter['content'] + "\n" # Process text in chunks to handle context limits text_chunks = process_large_text(full_text) # Extract characters and locations from each chunk all_characters = set() all_locations = set() for chunk in text_chunks: characters = extract_main_characters(chunk, language) locations = extract_key_locations(chunk, language) all_characters.update(characters) all_locations.update(locations) # Convert sets to lists target_character_names = list(all_characters) target_location_names = list(all_locations) print(f"Extracted {len(target_character_names)} main characters and {len(target_location_names)} key locations") return target_character_names, target_location_names if __name__ == "__main__": target_character_names = config["target_character_names"] if "target_character_names" in config else [] target_location_names = config["target_location_names"] if "target_location_names" in config else [] book_path = config["book_path"] try: book_name = os.path.basename(book_path).split(".")[0] except Exception as e: book_name = config["book_source"] if config["book_source"] else "new_book_1" language = config["language"] if config["language"] else lang_detect(book_name) book_source = config["book_source"] if config["book_source"] else book_name print(language,book_source) for key in config: if "API_KEY" in key and config[key]: os.environ[key] = config[key] target_characters_info = {} target_locations_info = {} if config["if_auto_extract"]: llm = get_models(config["llm_model_name"]) data = get_chapters(book_path) # chapters = [{"idx":"","title":"","content":""}] if not target_character_names or not target_location_names: print("No target characters or locations specified. Starting automatic extraction...") target_character_names, target_location_names = auto_extract_targets() # Update config with extracted targets config["target_character_names"] = target_character_names config["target_location_names"] = target_location_names save_json_file("./extract_config.json", config) print("Updated config file with extracted targets") print(target_character_names,target_location_names) # main for chapter in data: text = chapter['content'] if language == 'en': chunks = split_text_by_max_words(text, max_words=2000) else: chunks = split_text_by_max_words(text, max_words=4000) for chunk in chunks: # Profile for character in target_character_names: if character in chunk: new_profile = process_character_chunk(chunk, character, language) update_character_info(character, new_profile) # Relation for i, char1 in enumerate(target_character_names): for char2 in target_character_names[i+1:]: if char1 in chunk and char2 in chunk: char1_pos = chunk.find(char1) char2_pos = chunk.find(char2) if abs(char1_pos - char2_pos) <= 100: new_relation = process_character_relation(chunk, char1, char2, language) update_character_relation(char1, char2, new_relation) # Location for location in target_location_names: if location in chunk: new_description = process_location_chunk(chunk, location, language) update_location_info(location, new_description) # Replace the original save results section print("Starting to save extracted data...") # Save role information for role_info in target_characters_info.values(): role_path = save_role_info(role_info) print(f"Role information saved: {role_path}") # Save location information location_path = save_location_info(target_locations_info) print(f"Location information saved: {location_path}") print(f"Data extraction completed!") print(f"Role information saved to ./data/roles/{book_source}/") print(f"Location information saved to {location_path}") else: for role_name in target_character_names: target_characters_info[convert_name_to_code(role_name,language)] = get_default_character_info(role_name,language,[name for name in target_character_names if name != role_name]) for role_info in target_characters_info.values(): role_path = save_role_info(role_info) print(f"Role information saved: {role_path}") for loc_name in target_location_names: target_locations_info[convert_name_to_code(loc_name,language)] = get_defaule_location_info(loc_name,language) location_path = save_location_info(target_locations_info) print(f"Location information saved: {location_path}") world_path = save_world_info({ "source": book_source, "world_name": "", "description": "", "language":language }) print(f"World information saved: {world_path}") map_path = save_map(target_location_names, 1) print(f"Map saved: {map_path}") preset = { "experiment_subname": "1", "world_file_path":f"./data/worlds/{book_source}/general.json", "map_file_path":f"./data/maps/{book_source}.csv", "loc_file_path":f"./data/locations/{book_source}.json", "role_file_dir":"./data/roles/", "role_agent_codes":[convert_name_to_code(name,language) for name in target_character_names], "intervention":"", "script":"", "source":book_source, "language":language } ensure_dir("./experiment_presets/") preset_path = f"./experiment_presets/{book_source}.json" save_json_file(preset_path,preset) print(f"Preset generated: {preset_path}")
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/extract_data/extract_settings.py
extract_data/extract_settings.py
import sys sys.path.append("../") from bw_utils import * from extract_utils import * import os config = load_json_file("./extract_config.json") book_path = config["book_path"] book_name = os.path.basename(book_path).split(".")[0] language = config["language"] if config["language"] else lang_detect(book_name) book_source = config["book_source"] if config["book_source"] else book_name print(language,book_source) for key in config: if "API_KEY" in key and config[key]: os.environ[key] = config[key] llm = get_models(config["llm_model_name"]) data = get_chapters(book_path) # chapters = [{"idx":"","title":"","content":""}] lis_settings = [] # [{'term':...,'nature':[..],'detail':..,'source':...}] dic_settings = {} EXTRACT_SETTINGS_PROMPT = """ Identify the world-building elements and facts reflected in the given text from {source}, focusing on unique aspects such as magical systems in fantasy works, levels of technological advancement in science fiction, pivotal historical events, cultural consensus... Utilize your reasoning skills to uncover the underlying facts hidden beneath the surface. ** Make sure the settings you extract are common to most people in the world ** - Target text: {text} Notice that: 1.**Don't include any name, action, status of certain characters**. If the character's actions reflect certain social norms or settings, keep only the reflected part. 2.Don't include temporary event or seasonal description like 'It's raining...' or 'It's winter...' 3.Avoid using ambiguous or trival pronouns like 'their','The room'. 4.Avoid describing specific environment, such as 'The room's small and shabby condition...', - If the fact is about an infrequent term or terms, write the extracted fat following this format: [term](nature):detail - If the fact contains no certain term, write it with natural language. Each extracted fact should be separated by a newline. """ if language == 'zh': EXTRACT_SETTINGS_PROMPT += """ ==Example output(use the same language with the Target text)== [多斯拉克](民族):多斯拉克文化中,马是重要的象征,战士的地位和成就与他们的骑术密切相关。 [黑暗森林法则](理论):黑暗森林法则是对人类未能发现外星文明的一种假想解释。该假说认为宇宙中存在许多外星文明,但它们都沉默而多疑。该假说假设任何航天文明都会将其他智能生命视为不可避免的威胁,并因此摧毁任何暴露其行踪的新生文明。 在这个世界,在室内打伞被视为一种不吉利的行为。 """ else: EXTRACT_SETTINGS_PROMPT += """ ==Example output(use the same language with the Target text)== [Invisibility Cloak](artifact):Invisibility Cloak grants the wearer complete invisibility by concealing them from sight, making it an invaluable tool for stealth and evasion. In this world, throwing stones into rivers is considered an unlucky symbol. """ def update_settings(dic,lis,settings,source): settings = clear_blank_row(settings) for row in settings.split("\n"): parsed = parse_fact(row) if parsed: term,nature,detail = parsed if not term: lis.append({'term':term,'nature':[nature],'detail':[detail],'source':[source]}) continue if term not in dic: dic[term] = {"nature":[nature], "detail":[detail], "source":[source]} else: if nature not in dic[term]["nature"]: dic[term]["nature"].append(nature) if source not in dic[term]["source"]: dic[term]["source"].append(source) dic[term]["detail"].append(detail) else: lis.append({'term':"",'nature':"",'detail':[row],'source':[source]}) return dic,lis for chapter in data: text = chapter['content'] title = chapter['title'] idx = chapter["idx"] print(idx) if language == 'en': chunks = split_text_by_max_words(text,max_words=2000) else: chunks = split_text_by_max_words(text,max_words=4000) for chunk in chunks: prompt = EXTRACT_SETTINGS_PROMPT.format(**{ "text":chunk, "source":book_source }) output = clear_blank_row(llm.chat(prompt)) print(output) dic_settings,lis_settings = update_settings(dic_settings, lis_settings, output,f"{idx}_{title}") dic_settings2 = dict(sorted(dic_settings.items(), key=lambda x:x[0])) lis_settings2 = lis_settings for term in dic_settings2: value = dic_settings2[term] nature = value["nature"] detail = value["detail"] source = value["source"] lis_settings2.append({'term':term, 'nature':nature, 'detail':detail,'source':source}) path = f"./worlds/{book_source}/world_details/{book_source}.jsonl" save_jsonl_file(path,lis_settings2) print(f"Extaction on {book_source} has finished. The extracted settings have been saved to", path)
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/extract_data/extract_utils.py
extract_data/extract_utils.py
import ebooklib from ebooklib import epub from bs4 import BeautifulSoup import os import json import chardet from bw_utils import * import re import pdfplumber def extract_text_by_bookmark(book, start_href, end_href = ""): """ end_href: 搜索截止处。若为空字符串则直接搜索到结尾 """ # 查找 href 对应的章节 start_name = start_href.split("#")[0] end_name = end_href.split("#")[0] start_anchor = start_href.split("#")[1] if "#" in start_href else "" end_anchor = end_href.split("#")[1] if "#" in end_href else "" if start_name == end_name: for item in book.get_items(): if start_name in item.file_name: result = chardet.detect(item.get_content()) encoding = result['encoding'] if result else 'utf-8' soup = BeautifulSoup(item.get_content(), 'lxml',from_encoding=encoding) return extract_text_between_anchors(soup,start_anchor,end_anchor) print("href not found.") return "" flag = False text = "" for item in book.get_items(): if start_name in item.file_name: # 匹配 href 文件名 flag = True # 解析 HTML 内容 result = chardet.detect(item.get_content()) encoding = result['encoding'] if result else 'utf-8' soup = BeautifulSoup(item.get_content(), 'lxml',from_encoding = encoding) # 如果是锚点 (#),提取对应部分 if start_anchor: text += extract_text_after_anchor(soup,start_anchor) else: text += soup.get_text() elif end_name in item.file_name: flag = False if end_anchor: result = chardet.detect(item.get_content()) encoding = result['encoding'] if result else 'utf-8' soup = BeautifulSoup(item.get_content(), 'lxml',from_encoding = encoding) text += extract_text_before_anchor(soup,end_anchor) elif flag: result = chardet.detect(item.get_content()) encoding = result['encoding'] if result else 'utf-8' soup = BeautifulSoup(item.get_content(), 'lxml',from_encoding = encoding) text += soup.get_text() return text def extract_text_after_anchor(soup, anchor): # 找到具有指定 ID 的元素 element = soup.find(id=anchor) if not element: print("Anchor not found in the document.") return "" result = [] # 添加目标元素的文本 if element.get_text(): result.append(element.get_text()) # 遍历后续兄弟节点 for sibling in element.next_siblings: if sibling.name: # 如果是标签节点,提取其文本 result.append(sibling.get_text()) elif isinstance(sibling, str): # 如果是字符串,直接添加 result.append(sibling.strip()) return "\n".join(result) def extract_text_before_anchor(soup, anchor): # 找到具有指定 ID 的元素 element = soup.find(id=anchor) if not element: print("Anchor not found in the document.") return soup.get_text() result = [] # 遍历目标元素之前的兄弟节点 for sibling in element.previous_siblings: if sibling.name: # 如果是标签节点,提取其文本 result.insert(0, sibling.get_text()) # 插入到前面,保持顺序 elif isinstance(sibling, str): # 如果是字符串,直接添加 result.insert(0, sibling.strip()) return "\n".join(result).strip() def extract_text_between_anchors(soup, start_anchor, end_anchor): # 找到 start_anchor 和 end_anchor 元素 start_element = soup.find(id=start_anchor) end_element = soup.find(id=end_anchor) if not start_element: return extract_text_before_anchor(soup,end_anchor) if not end_element: return extract_text_after_anchor(soup,start_anchor) result = [] # 添加 start_anchor 的文本 if start_element.get_text(): result.append(start_element.get_text()) # 遍历从 start_anchor 到 end_anchor 的兄弟节点 for sibling in start_element.next_siblings: # 停止在 end_anchor if sibling == end_element: break # 如果是标签节点,提取文本 if sibling.name: result.append(sibling.get_text()) # 如果是字符串节点,直接添加 elif isinstance(sibling, str): result.append(sibling.strip()) return "\n".join(result).strip() def extract_bookmarks(toc, prefix = ""): bookmarks = [] titles = [] for item in toc: if isinstance(item, epub.Link): if prefix: title = prefix+"|"+item.title else: title = item.title titles.append(title) if title in titles[:-1]: title += str(titles.count(title) + 1) bookmarks.append((title, item.href)) elif isinstance(item, tuple): bookmarks.extend(extract_bookmarks(item[1],item[0].title)) return bookmarks def split_text_by_bookmarks(book): chapters = [] bookmarks = extract_bookmarks(book.toc) n = len(bookmarks) for i,(title,href) in enumerate(bookmarks): end_href = bookmarks[i+1][1] if i < n-1 else "" chapters.append({ "idx":i+1, "title":title, "content":extract_text_by_bookmark(book,href,end_href) }) return chapters def save_json_file(path,target): dir_name = os.path.dirname(path) if not os.path.exists(dir_name): os.makedirs(dir_name) with open(path,"w",encoding="utf-8") as f: json.dump(target, f, ensure_ascii=False,indent=True) def get_chapters(book_path): if book_path.endswith(".epub"): book = epub.read_epub(book_path) book_title = book.get_metadata('DC', 'title')[0][0] language = lang_detect(book_title) chapters = split_text_by_bookmarks(book) elif book_path.endswith(".txt"): chapters = extract_chapters_from_txt(book_path) elif book_path.endswith(".pdf"): chapters = extract_chapters_from_pdf(book_path) elif book_path.endswith(".json"): chapters = load_json_file(book_path) else: raise ValueError("Unsupported file format. Please provide a .epub, .txt, .pdf, or .json file.") return chapters def extract_chapters_from_txt(file_path): """从TXT文件提取章节""" chapters = [] chapter_patterns = [ r'第[一二三四五六七八九十百千万\d]+章', # 中文数字章节 r'第\d+章', # 阿拉伯数字章节 r'[Cc][Hh][Aa][Pp][Tt][Ee][Rr]\s*\d+', # 英文章节 r'CHAPTER\s*\d+', # 大写英文章节 ] patterns = [re.compile(pattern) for pattern in chapter_patterns] try: with open(file_path, 'r', encoding='utf-8') as f: content = f.read() # 初始化第一个章节(前言) current_chapter = { "idx": "0", "title": "前言", "content": [] } chapter_count = 0 # 按行分割内容 lines = content.split('\n') for line in lines: line = line.strip() if not line: continue # 检查是否是新章节 is_chapter_title = False for pattern in patterns: match = pattern.search(line) if match: # 保存当前章节 if current_chapter["content"]: current_chapter["content"] = '\n'.join(current_chapter["content"]) chapters.append(current_chapter) # 创建新章节 chapter_count += 1 current_chapter = { "idx": str(chapter_count), "title": line, "content": [] } is_chapter_title = True break if not is_chapter_title: current_chapter["content"].append(line) # 保存最后一个章节 if current_chapter["content"]: current_chapter["content"] = '\n'.join(current_chapter["content"]) chapters.append(current_chapter) return chapters except Exception as e: print(f"Error processing TXT file: {str(e)}") return [] def extract_chapters_from_pdf(file_path): """从PDF文件提取章节""" chapters = [] chapter_patterns = [ r'第[一二三四五六七八九十百千万\d]+章', r'第\d+章', r'[Cc][Hh][Aa][Pp][Tt][Ee][Rr]\s*\d+', r'CHAPTER\s*\d+', ] patterns = [re.compile(pattern) for pattern in chapter_patterns] try: with pdfplumber.open(file_path) as pdf: current_chapter = { "idx": "0", "title": "前言", "content": [] } chapter_count = 0 for page in pdf.pages: text = page.extract_text() if not text: continue lines = text.split('\n') for line in lines: line = line.strip() if not line: continue is_chapter_title = False for pattern in patterns: match = pattern.search(line) if match: if current_chapter["content"]: current_chapter["content"] = '\n'.join(current_chapter["content"]) chapters.append(current_chapter) chapter_count += 1 current_chapter = { "idx": str(chapter_count), "title": line, "content": [] } is_chapter_title = True break if not is_chapter_title: current_chapter["content"].append(line) if current_chapter["content"]: current_chapter["content"] = '\n'.join(current_chapter["content"]) chapters.append(current_chapter) return chapters except Exception as e: print(f"Error processing PDF file: {str(e)}") return [] def convert_name_to_code(name: str,lang: str) -> str: name = name.strip() name = re.sub(r'[·•・.。·‧∙⋅⸱・︙…॰ꞏ]', '', name) name = re.sub(r'[\s\-_,,.。!!??~~@#$%^&*()()\[\]【】{}{}+=<>《》/\\\|]', '', name) if lang == 'en': formatted_name = ''.join(word.capitalize() for word in name.split()) elif lang == 'zh': # For Chinese names, use pinyin try: from pypinyin import pinyin, Style # Convert to pinyin and capitalize each part pin = pinyin(name, style=Style.NORMAL) formatted_name = ''.join(p[0].capitalize() for p in pin) except ImportError: formatted_name = name # Fallback if pypinyin is not installed else: formatted_name = name return f"{formatted_name}-{lang}" def clear_blank_row(text): return "\n".join(sorted([row for row in text.split("\n") if ("None" not in row and len(row)>1)])) def parse_fact(sentence): # 正则表达式匹配 pattern = r"\[(.*?)\]\((.*?)\)\s*[::]\s*(.+)" match = re.match(pattern, sentence) if match: return match.group(1),match.group(2),match.group(3) if __name__ == "__main__": title = "祈祷之海" book = epub.read_epub(f'./{title}.epub') save_json_file(f'./{title}.json',split_text_by_bookmarks(book))
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/memory.py
modules/memory.py
import sys sys.path.append("../") from bw_utils import * from modules.embedding import get_embedding_model try: from langchain_experimental.generative_agents.memory import GenerativeAgentMemory except Exception: from langchain_experimental.generative_agents import GenerativeAgentMemory try: from langchain_classic.retrievers import TimeWeightedVectorStoreRetriever except Exception: try: from langchain.retrievers import TimeWeightedVectorStoreRetriever except Exception: raise ImportError("装 langchain-classic") try: from langchain_huggingface import HuggingFaceEmbeddings except Exception: try: from langchain_community.embeddings import HuggingFaceEmbeddings except Exception: raise ImportError("装 langchain-huggingface/langchain_community+sentence-transformers") try: from langchain_community.llms.tongyi import Tongyi except Exception: try: from langchain_community.llms import Tongyi except Exception: Tongyi = None try: from langchain.llms import OpenAI except Exception: try: from langchain_community.llms import OpenAI except Exception: OpenAI = None try: from langchain.docstore.in_memory import InMemoryDocstore except Exception: from langchain_community.docstore.in_memory import InMemoryDocstore try: from langchain.vectorstores.faiss import FAISS except Exception: try: from langchain.vectorstores import FAISS except Exception: from langchain_community.vectorstores.faiss import FAISS # 其它基础依赖 import faiss import math def build_role_agent_memory(type = "ga",**kwargs): if type == "ga": llm_name = kwargs["llm_name"] embedding_name = kwargs["embedding_name"] db_name = kwargs["db_name"] language = kwargs["language"] if "language" in kwargs else "" embedding_model = get_embedding_model(embedding_name,language) index = faiss.IndexFlatL2(len(embedding_model.embed_query("hello world"))) vectorstore = FAISS( embedding_function=embedding_model, index=index, docstore=InMemoryDocstore(), index_to_docstore_id={}, ) memory_retriever = TimeWeightedVectorStoreRetriever(vectorstore=vectorstore, other_score_keys=["importance"], k=5) if llm_name.startswith("qwen"): chat_model = Tongyi( temperature=0.9, ) else: chat_model = OpenAI( temperature=0.9, model="gpt-3.5-turbo", ) agent_memory = RoleMemory_GA( llm=chat_model, memory_retriever=memory_retriever, embedding_model=embedding_model, memory_decay_rate=0.01 ) return agent_memory else: db_name = kwargs["db_name"] embedding = kwargs["embedding"] db_type = kwargs["db_type"] if "db_type" in kwargs else "chromadb" capacity= kwargs["capacity"] if "capacity" in kwargs else 5 agent_memory = RoleMemory(db_name=db_name, embedding=embedding, db_type=db_type, capacity=capacity) return agent_memory def relevance_score_fn(score: float) -> float: return 1.0 - score / math.sqrt(2) class RoleMemory_GA(GenerativeAgentMemory): def init_from_data(self,data): for text in data: self.add_record(text) def add_record(self,text): self.add_memory(text) def search(self,query,top_k): fetched_memories = [doc.page_content for doc in self.fetch_memories(query)[:top_k]] if len(fetched_memories)>=top_k: print("-Memory Searching...") print(fetched_memories) return fetched_memories def delete_record(self, idx): pass class RoleMemory: def __init__(self,db_name,embedding,db_type = "chroma",capacity = 5,) -> None: self.idx = 0 self.capacity = capacity self.db_name = db_name self.db = build_db([],db_name,db_type,embedding,save_type="temporary") def init_from_data(self,data): for text in data: self.add_record(text) def add_record(self,text): self.idx += 1 self.db.add(text, str(self.idx), db_name=self.db_name) def search(self,query,top_k): return self.db.search(query, top_k,self.db_name) def delete_record(self, idx): self.db.delete(idx) @property def len(self): return self.db.len
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/history_manager.py
modules/history_manager.py
from typing import Any, Dict, List, Optional, Literal from collections import defaultdict from datetime import datetime from bw_utils import load_json_file, save_json_file, load_jsonl_file, save_jsonl_file import os class HistoryManager: def __init__(self): self.detailed_history = [] def add_record(self, record): """添加一个事件记录 record = { "cur_round":cur_round, "role_code":role_code, "detail":detail, "type":act_type, "initiator":initiator, "actor":actor "group":group, "other_info":other_info, "record_id":record_id } """ self.detailed_history.append(record) def modify_record(self, record_id: str, detail: str): """修改特定记录""" for record in self.detailed_history: if record["record_id"] == record_id: record["detail"] = detail print(f"Record {record_id} has been modified.") return record['group'] def search_record_detail(self, record_id: str): for record in self.detailed_history[::-1]: if record["record_id"] == record_id: return record["detail"] return None def get_recent_history(self, recent_k = 5): return [record["detail"] for record in self.detailed_history[-recent_k:]] def get_subsequent_history(self,start_idx): return [record["detail"] for record in self.detailed_history[start_idx:]] def get_complete_history(self,): return [record["detail"] for record in self.detailed_history[:]] def __len__(self): return len(self.detailed_history) def __getstate__(self): states = {key: value for key, value in self.__dict__.items() \ if isinstance(value, (str, int, list, dict, bool, type(None)))} return states def __setstate__(self, states): self.__dict__.update(states) def save_to_file(self, root_dir): filename = os.path.join(root_dir, f"./simulation_history.json") save_json_file(filename, self.__getstate__() ) def load_from_file(self, root_dir): filename = os.path.join(root_dir, f"./simulation_history.json") states = load_json_file(filename) self.__setstate__(states)
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/world_agent.py
modules/world_agent.py
import sys sys.path.append("../") import csv from typing import Any, Dict, List, Optional, Literal from bw_utils import * from modules.embedding import get_embedding_model class WorldAgent: # Init def __init__(self, world_file_path: str, location_file_path: str, map_file_path: Optional[str] = "", world_description: str = "", llm_name: str = "gpt-4o-mini", llm = None, embedding_name: str = "bge-small", embedding = None, db_type: str = "chroma", language: str = "zh", ): if llm is None: llm = get_models(llm_name) if embedding is None: embedding = get_embedding_model(embedding_name, language=language) self.llm = llm self.world_info: Dict[str, Any] = load_json_file(world_file_path) self.world_name: str = self.world_info["world_name"] self.language: str = language self.description:str = self.world_info["description"] if world_description == "" else world_description source = self.world_info["source"] self.locations_info: Dict[str, Any] = {} self.locations: List[str] = [] self.history: List[str] = [] self.edges: Dict[tuple, int] = {} # 地点间距离 self.prompts: List[Dict] = [] self.init_from_file(map_file_path = map_file_path, location_file_path = location_file_path) self.init_prompt() self.world_data,self.world_settings = build_world_agent_data(world_file_path = world_file_path, max_words = 50) self.db_name = clean_collection_name(f"settings_{source}_{embedding_name}") self.db = build_db(data = [row for row in self.world_data], db_name = self.db_name, db_type = db_type, embedding = embedding) def init_from_file(self, map_file_path: str, location_file_path: str, default_distance: int = 1): if map_file_path and os.path.exists(map_file_path): valid_locations = load_json_file(location_file_path) if "locations" not in load_json_file(location_file_path) else load_json_file(location_file_path)["locations"] with open(map_file_path, mode='r',encoding="utf-8") as file: csv_reader = csv.reader(file) locations = next(csv_reader)[1:] for row in csv_reader: loc1 = row[0] if loc1 not in valid_locations: print(f"Warning: The location {loc1} does not exist") continue self.locations_info[loc1] = valid_locations[loc1] self.locations.append(loc1) distances = row[1:] for i, distance in enumerate(distances): loc2 = locations[i] if loc2 not in valid_locations: print(f"Warning: The location {loc2} does not exist") continue if distance != '0': # Skip self-loops self._add_edge(loc1, loc2, int(distance)) else: valid_locations = load_json_file(location_file_path) if "locations" not in load_json_file(location_file_path) else load_json_file(location_file_path)["locations"] for loc1 in valid_locations: self.locations_info[loc1] = valid_locations[loc1] self.locations.append(loc1) for loc2 in valid_locations: if loc2 != loc1: self._add_edge(loc1, loc2, default_distance) def init_prompt(self,): if self.language == "zh": from modules.prompt.world_agent_prompt_zh import ENVIROMENT_INTERACTION_PROMPT,NPC_INTERACTION_PROMPT,SCRIPT_INSTRUCTION_PROMPT,SCRIPT_ATTENTION_PROMPT,DECIDE_NEXT_ACTOR_PROMPT,GENERATE_INTERVENTION_PROMPT,UPDATE_EVENT_PROMPT,LOCATION_PROLOGUE_PROMPT,SELECT_SCREEN_ACTORS_PROMPT,JUDGE_IF_ENDED_PROMPT,LOG2STORY_PROMPT else: from modules.prompt.world_agent_prompt_en import ENVIROMENT_INTERACTION_PROMPT,NPC_INTERACTION_PROMPT,SCRIPT_INSTRUCTION_PROMPT,SCRIPT_ATTENTION_PROMPT,DECIDE_NEXT_ACTOR_PROMPT,GENERATE_INTERVENTION_PROMPT,UPDATE_EVENT_PROMPT,LOCATION_PROLOGUE_PROMPT,SELECT_SCREEN_ACTORS_PROMPT,JUDGE_IF_ENDED_PROMPT,LOG2STORY_PROMPT self._ENVIROMENT_INTERACTION_PROMPT = ENVIROMENT_INTERACTION_PROMPT self._NPC_INTERACTION_PROMPT = NPC_INTERACTION_PROMPT self._SCRIPT_INSTRUCTION_PROMPT = SCRIPT_INSTRUCTION_PROMPT self._SCRIPT_ATTENTION = SCRIPT_ATTENTION_PROMPT self._DECIDE_NEXT_ACTOR_PROMPT= DECIDE_NEXT_ACTOR_PROMPT self._LOCATION_PROLOGUE_PROMPT = LOCATION_PROLOGUE_PROMPT self._GENERATE_INTERVENTION_PROMPT = GENERATE_INTERVENTION_PROMPT self._UPDATE_EVENT_PROMPT = UPDATE_EVENT_PROMPT self._SELECT_SCREEN_ACTORS_PROMPT = SELECT_SCREEN_ACTORS_PROMPT self._JUDGE_IF_ENDED_PROMPT = JUDGE_IF_ENDED_PROMPT self._LOG2STORY_PROMPT = LOG2STORY_PROMPT # Agent def update_event(self, cur_event: str, intervention:str, history_text: str, script: str = ""): prompt = self._UPDATE_EVENT_PROMPT.format(**{ "event":cur_event, "intervention":intervention, "history":history_text }) if script: prompt = self._SCRIPT_ATTENTION.format(script = script) + prompt new_event = self.llm.chat(prompt) self.record(new_event, prompt) return new_event def decide_next_actor(self, history_text: str, roles_info_text: str, script: str = "", event:str = ""): prompt = self._DECIDE_NEXT_ACTOR_PROMPT.format(**{ "roles_info":roles_info_text, "history_text":history_text, }) max_tries = 3 for _ in range(max_tries): try: response = self.llm.chat(prompt) break except Exception as e: print(f"Parsing failure! Error:", e) print(response) role_code = response self.prompts.append({"prompt":prompt, "response":f"{role_code}"}) return role_code def judge_if_ended(self,history_text): prompt = self._JUDGE_IF_ENDED_PROMPT.format(**{ "history":history_text }) max_tries = 3 response = {"if_end":True, "detail":""} for _ in range(max_tries): try: response.update(json_parser(self.llm.chat(prompt))) break except Exception as e: print(f"Parsing failure! Error:", e) print(response) return response["if_end"],response["detail"] def decide_scene_actors(self,roles_info_text, history_text, event, previous_role_codes): prompt = self._SELECT_SCREEN_ACTORS_PROMPT.format(**{ "roles_info":roles_info_text, "history_text":history_text, "event":event, "previous_role_codes":previous_role_codes }) response = self.llm.chat(prompt) role_codes = eval(response) return role_codes def generate_location_prologue(self, location_code, history_text, event, location_info_text): prompt = self._LOCATION_PROLOGUE_PROMPT.format(**{ "location_name":self.locations_info[location_code]["location_name"], "location_description":self.locations_info[location_code]["location_name"], "location_info":location_info_text, "history_text":history_text, "event":event, "world_description":self.description }) response = self.llm.chat(prompt) self.record(detail = response,prompt = prompt) return "\n"+response def enviroment_interact(self, action_maker_name: str, action: str, action_detail: str, location_code: str): references = self.retrieve_references(query = action_detail) prompt = self._ENVIROMENT_INTERACTION_PROMPT.format(** { "role_name":action_maker_name, "action":action, "action_detail":action_detail, "world_description":self.description, "location":location_code, "location_description":self.locations_info[location_code]["detail"], "references":references, } ) response = "无事发生。" if self.language == "zh" else "Nothing happens." for i in range(3): try: response = self.llm.chat(prompt) if response: break except Exception as e: print("Enviroment Interaction failed! {i}th tries. Error:", e) self.record(response, prompt) return response def npc_interact(self, action_maker_name: str, action_detail: str, location_name: str, target_name: str): references = self.retrieve_references(query = action_detail) prompt = self._NPC_INTERACTION_PROMPT.format(** { "role_name":action_maker_name, "action_detail":action_detail, "world_description":self.description, "target":target_name, "references":references, "location":location_name } ) npc_interaction = {"if_end_interaction":True,"detail":"无事发生。"} if self.language == "zh" else {"if_end_interaction":True,"detail":"Nothing happens"} try: npc_interaction = json_parser(self.llm.chat(prompt)) response = npc_interaction["detail"] self.record(response, prompt) except Exception as e: print("Enviroment Interaction failed!",e) return npc_interaction def get_script_instruction(self, roles_info_text: str, event: str, history_text: str, script: str, last_progress: str): prompt = self._SCRIPT_INSTRUCTION_PROMPT.format(**{ "roles_info":roles_info_text, "event":event, "history_text":history_text, "script":script, "last_progress":last_progress }) max_tries = 3 instruction = {} for i in range(max_tries): response = self.llm.chat(prompt) try: instruction = json_parser(response) break except Exception as e: print(f"Parsing failure! {i+1}th tries. Error:", e) print(response) self.record(response, prompt) return instruction def generate_event(self,roles_info_text: str, event: str, history_text: str): prompt = self._GENERATE_INTERVENTION_PROMPT.format(**{ "world_description":self.description, "roles_info":roles_info_text, "history_text":history_text }) response = self.llm.chat(prompt) self.record(response, prompt) return response def generate_script(self, roles_info_text: str, event: str, history_text: str): prompt = self._GENERATE_INTERVENTION_PROMPT.format(**{ "world_description":self.description, "roles_info":roles_info_text, "history_text":history_text }) response = self.llm.chat(prompt) self.record(response, prompt) return response def log2story(self,logs): prompt = self._LOG2STORY_PROMPT.format(**{ "logs":logs }) response = self.llm.chat(prompt) return response # Other def record(self, detail: str, prompt: str = ""): if prompt: self.prompts.append({"prompt":prompt, "response":detail}) self.history.append(detail) def add_location_during_simulation(self, location: str, detail: str): self.locations.append(location) self.locations_info[location] = { 'location_code': location, "location_name": location, 'description': '', 'detail':detail } for loc in self.locations: if loc != location: self._add_edge(loc, location, 1) self._add_edge(location,loc, 1) return def retrieve_references(self, query: str, top_k = 3, max_words = 100): if self.db is None: return "" references = "\n".join(self.db.search(query, top_k,self.db_name)) references = references[:max_words] return references def find_location_name(self, code: str): return self.locations_info[code]["location_name"] def _add_location(self, code: str, location_info: Dict[str, Any]): self.locations_info[code] = location_info def _add_edge(self, code1: str, code2: str, distance: int): self.edges[(code1,code2)] = distance self.edges[(code2,code1)] = distance def get_distance(self, code1: str, code2: str): if (code1,code2) in self.edges: return self.edges[(code1,code2)] else: return None def __getstate__(self): state = {key: value for key, value in self.__dict__.items() if isinstance(value, (str, int, list, dict, float, bool, type(None))) and (key not in ['llm','embedding','db','locations_info','edges','world_data','world_settings'] and "PROMPT" not in key) } return state def __setstate__(self, state): self.__dict__.update(state) def save_to_file(self, root_dir): filename = os.path.join(root_dir, f"./world_agent.json") save_json_file(filename, self.__getstate__() ) def load_from_file(self, root_dir): filename = os.path.join(root_dir, f"./world_agent.json") state = load_json_file(filename) self.__setstate__(state)
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/embedding.py
modules/embedding.py
import sys sys.path.append("../") from chromadb.api.types import Embeddings, Documents, EmbeddingFunction, Space from modelscope import AutoModel, AutoTokenizer from functools import partial from bw_utils import get_child_folders import torch import os class EmbeddingModel(EmbeddingFunction[Documents]): def __init__(self, model_name, language='en'): self.model_name = model_name self.language = language cache_dir = "~/.cache/modelscope/hub" model_provider = model_name.split("/")[0] model_smallname = model_name.split("/")[1] model_path = os.path.join(cache_dir, f"models--{model_provider}--{model_smallname}/snapshots/") if os.path.exists(model_path) and get_child_folders(model_path): try: model_path = os.path.join(model_path,get_child_folders(model_path)[0]) self.tokenizer = AutoTokenizer.from_pretrained(model_path) self.model = AutoModel.from_pretrained(model_path) except Exception as e: print(e) self.tokenizer = AutoTokenizer.from_pretrained(model_name) self.model = AutoModel.from_pretrained(model_name) else: self.tokenizer = AutoTokenizer.from_pretrained(model_name) self.model = AutoModel.from_pretrained(model_name) def __call__(self, input): inputs = self.tokenizer(input, return_tensors="pt", padding=True, truncation=True, max_length=256) with torch.no_grad(): outputs = self.model(**inputs) embeddings = outputs.last_hidden_state[:, 0, :].tolist() return embeddings class OpenAIEmbedding(EmbeddingFunction[Documents]): def __init__(self, model_name="text-embedding-ada-002", base_url = "https://api.openai.com/v1/embeddings", api_key_field = "OPENAI_API_KEY"): from openai import OpenAI self.client = OpenAI( base_url = base_url, api_key = os.environ[api_key_field] ) self.model_name = model_name def __call__(self, input): if isinstance(input, str): input = input.replace("\n", " ") return self.client.embeddings.create(input=[input], model=self.model_name).data[0].embedding elif isinstance(input,list): return [self.client.embeddings.create(input=[sentence.replace("\n", " ")], model=self.model_name).data[0].embedding for sentence in input] def get_embedding_model(embed_name, language='en'): local_model_dict = { "bge-m3":"BAAI/bge-m3", "bge-large": f"BAAI/bge-large-{language}", "luotuo": "silk-road/luotuo-bert-medium", "bert": "google-bert/bert-base-multilingual-cased", "bge-small": f"BAAI/bge-small-{language}", } online_model_dict = { "openai": {"model_name":"text-embedding-ada-002", "url":"https://api.openai.com/v1/embeddings", "api_key_field":"OPENAI_API_KEY"}, } if embed_name in local_model_dict: model_name = local_model_dict[embed_name] return EmbeddingModel(model_name, language=language) if embed_name in online_model_dict: model_name = online_model_dict[embed_name]["model_name"] api_key_field = online_model_dict[embed_name]["api_key_field"] base_url = online_model_dict[embed_name]["url"] return OpenAIEmbedding(model_name=model_name, base_url=base_url,api_key_field=api_key_field) return EmbeddingModel(embed_name, language=language)
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/main_role_agent.py
modules/main_role_agent.py
import sys from collections import defaultdict sys.path.append("../") import os from typing import Any, Dict, List, Optional, Literal from modules.embedding import get_embedding_model from modules.memory import build_role_agent_memory from modules.history_manager import HistoryManager from bw_utils import * import random import warnings warnings.filterwarnings("ignore") class RPAgent: def __init__(self, role_code: str, role_file_dir: str, world_file_path: str, source: str = "", language: str = "en", db_type: str = "chroma", llm_name: str = "gpt-4o-mini", llm = None, embedding_name: str = "bge-small", embedding = None ): super(RPAgent, self).__init__() self.language: str = language self.role_code: str = role_code self.history_manager = HistoryManager() self.prompts: List[Dict] = [] self.acted: bool = False self.status: str = "" self.goal: str = "" self.location_code: str = "" self.location_name: str = "" self.motivation: str = "" self._init_from_file(role_code, role_file_dir, world_file_path, source) self._init_prompt() self.llm_name = llm_name if llm == None: llm = get_models(llm_name) self.llm = llm if embedding is None: embedding = get_embedding_model(embedding_name, language=self.language) self.db_name = clean_collection_name(f"role_{role_code}_{embedding_name}") self.db = build_db(data = self.role_data, db_name = self.db_name, db_type = db_type, embedding = embedding) self.world_db = None self.world_db_name = "" self.memory = build_role_agent_memory(llm_name=llm_name, embedding_name = embedding_name, embedding = embedding, db_name = self.db_name.replace("role","memory"), language = self.language, type="naive" ) def _init_prompt(self): if self.language == 'zh': from modules.prompt.role_agent_prompt_zh \ import ROLE_PLAN_PROMPT,ROLE_SINGLE_ROLE_RESPONSE_PROMPT,ROLE_MULTI_ROLE_RESPONSE_PROMPT,ROLE_SET_GOAL_PROMPT,INTERVENTION_PROMPT,UPDATE_GOAL_PROMPT,UPDATE_STATUS_PROMPT,ROLE_SET_MOTIVATION_PROMPT,SCRIPT_ATTENTION_PROMPT,ROLE_MOVE_PROMPT,SUMMARIZE_PROMPT,ROLE_NPC_RESPONSE_PROMPT else: from modules.prompt.role_agent_prompt_en \ import ROLE_PLAN_PROMPT,ROLE_SINGLE_ROLE_RESPONSE_PROMPT,ROLE_MULTI_ROLE_RESPONSE_PROMPT,ROLE_SET_GOAL_PROMPT,INTERVENTION_PROMPT,UPDATE_GOAL_PROMPT,UPDATE_STATUS_PROMPT,ROLE_SET_MOTIVATION_PROMPT,SCRIPT_ATTENTION_PROMPT,ROLE_MOVE_PROMPT,SUMMARIZE_PROMPT,ROLE_NPC_RESPONSE_PROMPT self._ROLE_SET_GOAL_PROMPT = ROLE_SET_GOAL_PROMPT self._ROLE_PLAN_PROMPT = ROLE_PLAN_PROMPT self._ROLE_SINGLE_ROLE_RESPONSE_PROMPT = ROLE_SINGLE_ROLE_RESPONSE_PROMPT self._ROLE_MULTI_ROLE_RESPONSE_PROMPT = ROLE_MULTI_ROLE_RESPONSE_PROMPT self._INTERVENTION_PROMPT = INTERVENTION_PROMPT self._UPDATE_GOAL_PROMPT = UPDATE_GOAL_PROMPT self._UPDATE_STATUS_PROMPT = UPDATE_STATUS_PROMPT self._ROLE_SET_MOTIVATION_PROMPT = ROLE_SET_MOTIVATION_PROMPT self._SCRIPT_PROMPT = SCRIPT_ATTENTION_PROMPT self._ROLE_MOVE_PROMPT = ROLE_MOVE_PROMPT self._SUMMARIZE_PROMPT = SUMMARIZE_PROMPT self._ROLE_NPC_RESPONSE_PROMPT = ROLE_NPC_RESPONSE_PROMPT def _init_from_file(self, role_code: str, role_file_dir: str, world_file_path: str, source:str): if source and os.path.exists(os.path.join(role_file_dir, source)): for path in get_child_folders(os.path.join(role_file_dir, source)): if role_code in path: role_path = path break else: for path in get_grandchild_folders(role_file_dir): if role_code in path: role_path = path break base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) role_profile_path = os.path.join(base_dir, role_path,"role_info.json") role_info = load_json_file(role_profile_path) # self.role_info = role_info self.role_profile: str = role_info['profile'] self.nickname: str = role_info["nickname"] self.role_name: str = role_info["role_name"] self.relation: str = role_info["relation"] self.motivation: str = role_info["motivation"] if "motivation" in role_info else "" self.activity: float = float(role_info["activity"]) if "activity" in role_info else 1.0 self.icon_path: str = os.path.join(base_dir, role_path,"icon.png") self.avatar_path: str = os.path.join(base_dir, role_path,"avatar.png") for image_type in ['jpg','png','bmp']: if os.path.exists(os.path.join(base_dir, role_path,f"./avatar.{image_type}")): self.avatar_path: str = os.path.join(base_dir, role_path,f"avatar.{image_type}") if os.path.exists(os.path.join(base_dir, role_path,f"./icon.{image_type}")): self.icon_path: str = os.path.join(base_dir, role_path,f"icon.{image_type}") self.role_data: List[str] = build_role_agent_data(os.path.join(base_dir, role_path)) # Agent def set_motivation(self, world_description: str, other_roles_info: Dict[str, Any], intervention: str = "", script: str = ""): if self.motivation: return self.motivation other_roles_info_text = self.get_other_roles_info_text(other_roles_info) prompt = self._ROLE_SET_MOTIVATION_PROMPT.format(** { "role_name": self.role_name, "profile":self.role_profile, "world_description": world_description, "other_roles_description": other_roles_info_text, "location": self.location_name }) if script: script = self._SCRIPT_PROMPT.format(** {"script": script} ) prompt = prompt + script elif intervention: intervention = self._INTERVENTION_PROMPT.format(** {"intervention": intervention} ) prompt = intervention + prompt + "\n**注意: 在你的动机中考虑全局事件的影响**" if self.language == "zh" else intervention + prompt + "\n**Notice that: You should take the global event into consideration.**" motivation = self.llm.chat(prompt) self.save_prompt(prompt = prompt, detail = motivation) self.motivation = motivation return motivation def plan(self, other_roles_info: Dict[str, Any], available_locations: List[str], world_description: str, intervention: str = ""): action_history_text = self.retrieve_history(query = "", retrieve=False) references = self.retrieve_references(query = action_history_text) knowledges = self.retrieve_knowledges(query = action_history_text) if len(other_roles_info) == 1: other_roles_info_text = "没有人在这里。你不能进行涉及角色的互动。" if self.language == "zh" else "No one else is here. You can not interact with roles." else: other_roles_info_text = self.get_other_roles_info_text(other_roles_info, if_profile = False) if intervention: intervention = self._INTERVENTION_PROMPT.format(** {"intervention": intervention} ) prompt = self._ROLE_PLAN_PROMPT.format(** { "role_name": self.role_name, "nickname": self.nickname, "profile": self.role_profile, "goal": self.goal, "status": self.status, "history": action_history_text, "other_roles_info": other_roles_info_text, "world_description": world_description, "location": self.location_name, "references": references, "knowledges":knowledges, } ) prompt = intervention + prompt max_tries = 3 plan = {"action": "待机" if self.language == "zh" else "Stay", "destination": None, "interact_type":'no', "target_role_codes": [], "target_npc_name":None, "detail": f"{self.role_name}原地不动,观察情况。" if self.language == "zh" else f"{self.role_name} stays put." } for i in range(max_tries): response = self.llm.chat(prompt) try: plan.update(json_parser(response)) break except Exception as e: print(self.role_name) print(f"Parsing failure! {i+1}th tries. Error:", e) print(response) plan["role_code"] = self.role_code self.save_prompt(detail=plan["detail"], prompt=prompt) return plan def npc_interact(self, npc_name:str, npc_response:str, history:str, intervention:str = "" ): references = self.retrieve_references(npc_response) knowledges = self.retrieve_knowledges(query = npc_response) if intervention: intervention = self._INTERVENTION_PROMPT.format(** {"intervention": intervention} ) prompt = self._ROLE_NPC_RESPONSE_PROMPT.format(** { "role_name": self.role_name, "nickname": self.nickname, "profile": self.role_profile, "goal": self.goal, "npc_name":npc_name, "npc_response":npc_response, "references": references, "knowledges":knowledges, "dialogue_history": history } ) prompt = intervention + prompt interaction = { "if_end_interaction": True, "detail": "", } response = self.llm.chat(prompt) interaction.update(json_parser(response)) self.save_prompt(detail = interaction["detail"], prompt = prompt) return interaction def single_role_interact(self, action_maker_code: str, action_maker_name: str, action_detail: str, action_maker_profile: str, intervention: str = ""): references = self.retrieve_references(action_detail) history = self.retrieve_history(query = action_detail) knowledges = self.retrieve_knowledges(query = action_detail) relation = f"role_code:{action_maker_code}\n" + self.search_relation(action_maker_code) if intervention: intervention = self._INTERVENTION_PROMPT.format(** {"intervention": intervention} ) prompt = self._ROLE_SINGLE_ROLE_RESPONSE_PROMPT.format(** { "role_name": self.role_name, "nickname": self.nickname, "action_maker_name": action_maker_name, "action_detail": action_detail, "profile": self.role_profile, "action_maker_profile": action_maker_profile, "relation": relation, "goal": self.goal, "status": self.status, "references": references, "knowledges":knowledges, "history": history } ) prompt = intervention + prompt max_tries = 3 interaction = { "if_end_interaction": True, "extra_interact_type":"no", "target_npc_name":"", "detail": "", } for i in range(max_tries): response = self.llm.chat(prompt) try: interaction.update(json_parser(response)) break except Exception as e: print(f"Parsing failure! {i}th tries. Error:", e) print(response) self.save_prompt(detail = interaction["detail"], prompt = prompt) return interaction def multi_role_interact(self, action_maker_code: str, action_maker_name: str, action_detail: str, action_maker_profile: str, other_roles_info: Dict[str, Any], intervention: str = ""): references = self.retrieve_references(query = action_detail) history = self.retrieve_history(query = action_detail) knowledges = self.retrieve_knowledges(query = action_detail) other_roles_info_text = self.get_other_roles_info_text(other_roles_info, if_profile = False) if intervention: intervention = self._INTERVENTION_PROMPT.format(** {"intervention": intervention} ) prompt = self._ROLE_MULTI_ROLE_RESPONSE_PROMPT.format(** { "role_name": self.role_name, "nickname": self.nickname, "action_maker_name": action_maker_name, "action_detail": action_detail, "profile": self.role_profile, "action_maker_profile": action_maker_profile, "other_roles_info":other_roles_info_text, "goal":self.goal, "status": self.status, "references": references, "knowledges":knowledges, "history": history } ) prompt = intervention + prompt max_tries = 3 interaction = { "if_end_interaction": True, "extra_interact_type":"no", "target_role_code":"", "target_npc_name":"", "visible_role_codes":[], "detail": "", } for i in range(max_tries): response = self.llm.chat(prompt) try: interaction.update(json_parser(response)) break except Exception as e: print(f"Parsing failure! {i}th tries. Error:", e) print(response) self.save_prompt(detail = interaction["detail"], prompt=prompt) return interaction def update_status(self,): prompt = self._UPDATE_STATUS_PROMPT.format(**{ "role_name":self.role_name, "status":self.status, "history_text":self.retrieve_history(query=""), "activity":self.activity }) max_tries = 3 for i in range(max_tries): response = self.llm.chat(prompt) try: status = json_parser(response) self.status = status["updated_status"] self.activity = float(status["activity"]) break except Exception as e: print(f"Parsing failure! {i}th tries. Error:", e) print(response) return def update_goal(self,other_roles_status: str,instruction: str = ""): motivation = self.motivation if instruction: motivation = instruction history = self.retrieve_history(self.motivation) if len(history) == 0: self.goal = motivation return motivation prompt = self._UPDATE_GOAL_PROMPT.format(**{ "history":history, "motivation":motivation, "goal":self.goal, "other_roles_status":other_roles_status, "location":self.location_name }) response = self.llm.chat(prompt) try: new_plan = json_parser(response) if new_plan["if_change_goal"]: goal = new_plan["updated_goal"] self.save_prompt(prompt,response) self.goal = goal return goal except Exception as e: print(self.role_name) print(f"Parsing failure! Error:", e) print(response) return "" def move(self, locations_info_text: str, locations_info: Dict[str, Any]): history_text = self.retrieve_history(query="") prompt = self._ROLE_MOVE_PROMPT.format(**{ "role_name":self.role_name, "profile": self.role_profile, "goal":self.goal, "status":self.status, "history":history_text, "location":self.location_name, "locations_info_text":locations_info_text }) response= self.llm.chat(prompt) try: result = json_parser(response) if result["if_move"] and "destination_code" in result and result["destination_code"] in locations_info and result["destination_code"] != self.location_code: destination_code = result["destination_code"] self.save_prompt(detail = result["detail"], prompt = prompt) return True, result["detail"], destination_code except Exception as e: print(f"Parsing failure! Error:", e) print(response) return False, "",self.location_code def record(self, record): self.history_manager.add_record(record) def save_prompt(self,prompt,detail): if prompt: self.prompts.append({"prompt":prompt, "response":detail}) # Other def action_check(self,): if self.acted == False: self.acted = True return True dice = random.uniform(0,1) if dice > self.activity: self.acted = False return False return True def retrieve_knowledges(self, query:str, top_k:int=1, max_words = 100): if self.world_db is None: return "" knowledges = "\n".join(self.world_db.search(query, top_k,self.world_db_name)) knowledges = knowledges[:max_words] return knowledges def retrieve_references(self, query: str, top_k: int = 1): if self.db is None: return "" references = "\n".join(self.db.search(query, top_k,self.db_name)) return references def retrieve_history(self, query: str, top_k: int = 5, retrieve: bool = False): if len(self.history_manager) == 0: return "" if len(self.history_manager) >= top_k and retrieve: history = "\n" + "\n".join(self.memory.search(query, top_k)) + "\n" else: history = "\n" + "\n".join(self.history_manager.get_recent_history(top_k)) return history def get_other_roles_info_text(self, other_roles: List[str], if_relation: bool = True, if_profile: bool = True): roles_info_text = "" for i, role_code in enumerate(other_roles): if role_code == self.role_code :continue name = other_roles[role_code]["nickname"] profile = other_roles[role_code]["profile"] if if_profile else "" relation = self.search_relation(role_code) if if_relation else "" roles_info_text += f"\n{i+1}. {name}\nrole_code:{role_code}\n{relation}\n{profile}\n\n" return roles_info_text def search_relation(self, other_role_code: str): if self.language == 'en': if other_role_code in self.relation: relation_text = ",".join(self.relation[other_role_code]["relation"]) detail_text = self.relation[other_role_code]["detail"] return f"This is your {relation_text}. {detail_text}\n" else: return "" elif self.language == 'zh': if other_role_code in self.relation: relation_text = ",".join(self.relation[other_role_code]["relation"]) detail_text = self.relation[other_role_code]["detail"] return f"这是你的{relation_text}. {detail_text}\n" else: return "" def set_location(self, location_code, location_name): self.location_code: Optional[str] = location_code self.location_name: Optional[str] = location_name def __getstate__(self): states = {key: value for key, value in self.__dict__.items() \ if isinstance(value, (str, int, list, dict, bool, type(None))) \ and key not in ['role_info','role_data','llm','embedding','db',"memory"] and "PROMPT" not in key} return states def __setstate__(self, states): self.__dict__.update(states) self._init_prompt() def save_to_file(self, root_dir): filename = os.path.join(root_dir, f"./roles/{self.role_code}.json") save_json_file(filename, self.__getstate__() ) def load_from_file(self, root_dir): filename = os.path.join(root_dir, f"./roles/{self.role_code}.json") states = load_json_file(filename) self.__setstate__(states) self.memory.init_from_data(self.history_manager.get_complete_history()) def build_role_agent_data(role_dir: str): role_data: List[str] = [] for path in get_child_paths(role_dir): if os.path.splitext(path)[-1] == ".txt": text = load_text_file(path) role_data += split_text_by_max_words(text) elif os.path.splitext(path)[-1] == ".jsonl": role_data += [line["text"] for line in load_jsonl_file(path)] return role_data if __name__ == "__main__": agent = RPAgent(role_code='Harry-en') agent.single_role_interact("Hi,Harry, Who is Ron?")
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/eval_agent.py
modules/eval_agent.py
import sys from flask import Response sys.path.append("../") from typing import Any, Dict, List, Optional, Literal from collections import defaultdict from bw_utils import * from modules.prompt.eval_prompt import DEDUCT_PROMPT_DICT,PLUS_PROMPT_DICT, TTCW_PROMPT_DICT, SCRIPT_GENERATION_PROMPT,SCORING_PROMPT_DICT,SCRIPT_GENERATE_BY_CHARA_PROMPT,SCORING_PROMPT_DICT_FREE_MODE,FREE_GENERATE_BY_CHARA_PROMPT,FREE_GENERATION_PROMPT from modules.embedding import get_embedding_model class EvalAgent: def __init__(self, roles_info: Dict[str, Any], summary: str, source: str = "", llm_name: str = "gpt-4o", llm = None, role_llm = None ): super(EvalAgent, self).__init__() if llm == None: llm = get_models(llm_name) self.llm_name = llm_name self.llm = llm self.role_llm = role_llm self.summary = summary self.source = source self.eval_result = defaultdict(dict) self.generated_script = {} self.roles_info = roles_info def naive_generate(self,num_records: int, mode = "script"): if "naive" in self.generated_script and self.generated_script["naive"] and self.generated_script["naive"].strip(): return self.generated_script["naive"] if mode == "script": prompt = SCRIPT_GENERATION_PROMPT.format(**{ "source":self.source, "scenario": self.summary, "major_characters": self._get_roles_name(), "character_profiles": self._get_roles_info_text(), "num_records": num_records }) text = self.role_llm.chat(prompt) self.generated_script["naive"] = text return self.generated_script["naive"] elif mode == "free": prompt = FREE_GENERATION_PROMPT.format(**{ "source":self.source, "major_characters": self._get_roles_name(), "character_profiles": self._get_roles_info_text(), "num_records": num_records }) text = self.role_llm.chat(prompt) self.generated_script["naive"] = text return self.generated_script["naive"] def naive_generate_multi_round(self,num_records: int , mode="script"): if "naive_multi" in self.generated_script and self.generated_script["naive_multi"] and self.generated_script["naive_multi"].strip(): return self.generated_script["naive_multi"] lis = [] num_records = min(num_records, 20) if mode == "script": for i in range(num_records): prompt = SCRIPT_GENERATE_BY_CHARA_PROMPT.format(**{ "history":"\n".join(lis), "source":self.source, "scenario": self.summary, "major_characters": self._get_roles_name(), "character_profiles": self._get_roles_info_text(), }) for _ in range(3): try: text = self.role_llm.chat(prompt) if text: break except Exception as e: print(e) lis.append(text) self.generated_script["naive_multi"] = merge_text_with_limit(lis, 5000) return self.generated_script["naive_multi"] elif mode == "free": for i in range(num_records): prompt = FREE_GENERATE_BY_CHARA_PROMPT.format(**{ "history":"\n".join(lis), "source":self.source, "major_characters": self._get_roles_name(), "character_profiles": self._get_roles_info_text(), }) for _ in range(3): text = self.role_llm.chat(prompt) if text: break lis.append(text) self.generated_script["naive_multi"] = merge_text_with_limit(lis, 5000) return self.generated_script["naive_multi"] def hollmwood_generate(self,root_dir,idx,num_records: int, default_text = ""): path = os.path.join(root_dir,idx,"Cleaned_Screenplay.json") if not os.path.exists(path): return default_text lis = load_json_file(path)[:num_records] text = merge_text_with_limit(lis, 5000) if not text: text = default_text self.generated_script["hollmwood"] = text return text def naive_score(self,text,method, mode = "script"): result = {"score":{}} evaled_dims = [] if method in self.eval_result and "scoring" in self.eval_result[method] and "score" in self.eval_result[method]["scoring"]: evaled_dims = list(self.eval_result[method]["scoring"]["score"].keys()) result = self.eval_result[method]["scoring"] elif method not in self.eval_result: self.eval_result[method] = {} elif "scoring" not in self.eval_result[method]: self.eval_result[method]["scoring"] = {} if mode == "script": self.generated_script[method] = text dimensions = list(SCORING_PROMPT_DICT["dimensions"].keys()) template = SCORING_PROMPT_DICT["scoring_template"] for dim in dimensions: if dim in evaled_dims:continue criteria = SCORING_PROMPT_DICT["dimensions"][dim] prompt = template.format(**{ "dimension_name":dim, "criteria":criteria, "source":self.source, "scenario": self.summary, "major_characters": self._get_roles_name(), "character_profiles": self._get_roles_info_text(), "text":text, }) for i in range(3): try: output = self.llm.chat(prompt) response = json_parser(output) score = response["score"] result["score"][dim] = score # result["explanation"][dim] = response["explanation"] break except Exception as e: print(f"Parsing failure! {i+1}th tries. Error:", e) self.eval_result[method]["scoring"] = result return result["score"] elif mode == "free": self.generated_script[method] = text dimensions = list(SCORING_PROMPT_DICT_FREE_MODE["dimensions"].keys()) template = SCORING_PROMPT_DICT_FREE_MODE["scoring_template"] for dim in dimensions: if dim in evaled_dims:continue criteria = SCORING_PROMPT_DICT_FREE_MODE["dimensions"][dim] prompt = template.format(**{ "dimension_name":dim, "criteria":criteria, "source":self.source, "major_characters": self._get_roles_name(), "character_profiles": self._get_roles_info_text(), "text":text, }) for i in range(3): try: output = self.llm.chat(prompt) response = json_parser(output) score = response["score"] result["score"][dim] = score # result["explanation"][dim] = response["explanation"] break except Exception as e: print(f"Parsing failure! {i+1}th tries. Error:", e) self.eval_result[method]["scoring"] = result return result["score"] def score_all_dims(self, text,method,score_type = "deduct"): if method in self.eval_result and score_type in self.eval_result[method] and "score_detail" in self.eval_result[method][score_type]: return self.eval_result[method][score_type]["score_detail"], self.eval_result[method][score_type]["score"] self.generated_script[method] = text if score_type == "deduct": dimensions = list(DEDUCT_PROMPT_DICT["dimension_details"].keys()) elif score_type == "plus": dimensions = list(PLUS_PROMPT_DICT["dimension_details"].keys()) if method not in self.eval_result: self.eval_result[method] = {} if score_type not in self.eval_result[method]: self.eval_result[method][score_type] = {"score_detail":{},"score":0} sum_score = 0 for dim in dimensions: result,score = self.score_single_dim(text,dim,score_type) self.eval_result[method][score_type]["score_detail"].update(result) sum_score += score self.eval_result[method][score_type]["score"] = sum_score / len(dimensions) return self.eval_result[method][score_type]["score_detail"], self.eval_result[method][score_type]["score"] def score_single_dim(self, text, dim, score_type = "deduct"): if score_type == "deduct": template = DEDUCT_PROMPT_DICT["self-play-deduct-template"] brief = DEDUCT_PROMPT_DICT["dimension_details"][dim]["dimension_brief"] criteria = DEDUCT_PROMPT_DICT["dimension_details"][dim]["dimension_criteria"] elif score_type == "plus": template = PLUS_PROMPT_DICT["self-play-plus-template"] brief = PLUS_PROMPT_DICT["dimension_details"][dim]["dimension_brief"] criteria = PLUS_PROMPT_DICT["dimension_details"][dim]["dimension_criteria"] prompt = template.format(**{ "dimension_name":dim, "dimension_brief":brief, "dimension_criteria":criteria, "source":self.source, "scenario": self.summary, "major_characters": self._get_roles_name(), "character_profiles": self._get_roles_info_text(), "text":text, }) max_tries = 5 for i in range(max_tries): response = self.llm.chat(prompt) try: result = json_parser(response) result[dim]["score"] = int(result[dim]["score"]) break except Exception as e: print(f"Parsing failure! {i+1}th tries. Error:", e) print(response) return result,result[dim]["score"] def naive_winner(self,method_text,method, mode = "script"): bookworld_text = self.generated_script["bookworld"] result = {"winner":{}} evaled_dims = [] if method in self.eval_result and "winner" in self.eval_result[method]: evaled_dims = list(self.eval_result[method]["winner"]["winner"].keys()) result = self.eval_result[method]["winner"] elif method not in self.eval_result: self.eval_result[method] = {} elif "winner" not in self.eval_result[method]: self.eval_result[method]["winner"] = {} if mode == "script": dimensions = list(SCORING_PROMPT_DICT["dimensions"].keys()) template = SCORING_PROMPT_DICT["comparison_template"] for dim in dimensions: if dim in evaled_dims:continue criteria = SCORING_PROMPT_DICT["dimensions"][dim] prompt = template.format(**{ "dimension_name":dim, "criteria":criteria, "source":self.source, "major_characters": self._get_roles_name(), "character_profiles": self._get_roles_info_text(), "scenario": self.summary, "text1":bookworld_text, "text2":method_text, "method1":"bookworld", "method2":method, }) for i in range(3): try: response = json_parser(self.llm.chat(prompt)) winner = response["winner"] result["winner"][dim] = winner # result["explanation"][dim] = response["explanation"] break except Exception as e: print(f"Parsing failure! {i+1}th tries. Error:", e) self.eval_result[method]["winner"] = result return self.eval_result[method]["winner"]["winner"] elif mode == "free": dimensions = list(SCORING_PROMPT_DICT_FREE_MODE["dimensions"].keys()) template = SCORING_PROMPT_DICT_FREE_MODE["comparison_template"] for dim in dimensions: if dim in evaled_dims:continue criteria = SCORING_PROMPT_DICT_FREE_MODE["dimensions"][dim] prompt = template.format(**{ "dimension_name":dim, "criteria":criteria, "source":self.source, "major_characters": self._get_roles_name(), "character_profiles": self._get_roles_info_text(), "text1":bookworld_text, "text2":method_text, "method1":"bookworld", "method2":method, }) for i in range(3): try: response = json_parser(self.llm.chat(prompt)) winner = response["winner"] result["winner"][dim] = winner # result["explanation"][dim] = response["explanation"] break except Exception as e: print(f"Parsing failure! {i+1}th tries. Error:", e) print(response) self.eval_result[method]["winner"] = result return self.eval_result[method]["winner"]["winner"] def ttcw_test(self,text,method): if method in self.eval_result and "ttcw_detail" in self.eval_result[method]: return self.eval_result[method]["ttcw_detail"],self.eval_result[method]["ttcw_score"] ttcw_question_dict = TTCW_PROMPT_DICT["ttcw_question_dict"] TTCW_SYSTEM_PROMPT = TTCW_PROMPT_DICT["ttcw_template"] result_dic = {} score_dic = {} all_yes,all_no = 0,0 for dim in ttcw_question_dict: num_yes,num_no = 0,0 result_dic[dim] = {} for subdim in ttcw_question_dict[dim]: question = ttcw_question_dict[dim][subdim] prompt = TTCW_SYSTEM_PROMPT.format(**{ "text":text, "question":question }) result = self.llm.chat(prompt).lower() if "yes" in result : result_dic[dim][subdim] = 'yes' num_yes += 1 elif "no" in result : result_dic[dim][subdim] = 'no' num_no += 1 all_yes += num_yes all_no += num_no score_dic[dim] = num_yes / (num_yes + num_no) score_dic['all'] = all_yes / (all_yes + all_no) if not method in self.eval_result: self.eval_result[method] = {} self.eval_result[method]["ttcw_detail"] = result_dic self.eval_result[method]["ttcw_score"] = score_dic return self.eval_result[method]["ttcw_detail"],self.eval_result[method]["ttcw_score"] def save_generated_text(self, method, text): self.generated_script[method] = text def _get_roles_name(self,): return ", ".join([role_info["nickname"] for role_info in self.roles_info.values()]) def _get_roles_info_text(self,): info_text = "" for role_code in self.roles_info: nickname = self.roles_info[role_code]["nickname"] profile = self.roles_info[role_code]["profile"] info_text += f"{nickname}: {profile}\n" return info_text def __getstate__(self): state = {key: value for key, value in self.__dict__.items() if isinstance(value, (str, int, list, dict, float, bool, type(None))) and (key not in ['llm','embedding','db'] and "PROMPT" not in key) } return state def __setstate__(self, state): self.__dict__.update(state) def save_to_file(self, root_dir): filename = os.path.join(root_dir, f"./eval_agent.json") save_json_file(filename, self.__getstate__() ) def load_from_file(self, root_dir): print("Load eval results") filename = os.path.join(root_dir, f"./eval_agent.json") if os.path.exists(filename): state = load_json_file(filename) self.__setstate__(state)
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/db/BaseDB.py
modules/db/BaseDB.py
# BaseDB.py from abc import ABC, abstractmethod class BaseDB(ABC): @abstractmethod def init_from_data(self, data): pass @abstractmethod def search(self, query, n_results): pass
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/db/ChromaDB.py
modules/db/ChromaDB.py
import chromadb from .BaseDB import BaseDB import os from tqdm import tqdm import uuid class ChromaDB(BaseDB): def __init__(self, embedding, save_type="persistent"): try: self.collections = {} self.embedding = embedding base_dir = os.path.dirname(os.path.abspath(__file__)) if save_type == "persistent": self.path = os.path.join(base_dir, "./chromadb_saves/") os.makedirs(self.path, exist_ok=True) self.client = chromadb.PersistentClient(path=self.path) else: self.client = chromadb.Client() except Exception as e: raise Exception(f"Failed to initialize ChromaDB: {str(e)}") def init_from_data(self, data, db_name): if not db_name: raise ValueError("Invalid db_name") if not data: collection = self.client.get_collection( name=db_name, embedding_function=self.embedding ) self.collections[db_name] = collection try: new_data_set = set(data) if db_name in [c.name for c in self.client.list_collections()]: collection = self.client.get_collection( name=db_name, embedding_function=self.embedding ) self.collections[db_name] = collection # 获取现有collection中的所有数据 existing_data = collection.get() existing_docs = existing_data['documents'] existing_ids = existing_data['ids'] # 创建现有文档的映射 {文档内容: ID} existing_doc_map = {doc: id_ for doc, id_ in zip(existing_docs, existing_ids)} # 找出需要删除的文档(在现有数据中但不在新数据中的) docs_to_delete = [id_ for doc, id_ in existing_doc_map.items() if doc not in new_data_set] if docs_to_delete: collection.delete(ids=docs_to_delete) # 找出需要添加的文档(在新数据中但不在现有数据中的) docs_to_add = [doc for doc in new_data_set if doc not in existing_doc_map] # 批量添加新文档 if docs_to_add: new_ids = [str(uuid.uuid4()) for _ in range(len(docs_to_add))] collection.add( documents=docs_to_add, ids=new_ids ) else: collection = self.client.create_collection( name=db_name, embedding_function=self.embedding ) self.collections[db_name] = collection ids = [str(uuid.uuid4()) for _ in range(len(data))] collection.add( documents=data, ids=ids ) except Exception as e: raise Exception(f"Failed to initialize data: {str(e)}") def search(self, query, n_results, db_name): if not query or not db_name or db_name not in self.collections: return [] try: n_results = min(self.collections[db_name].count(), n_results) if n_results < 1: return [] results = self.collections[db_name].query( query_texts=[query], n_results=n_results ) return results['documents'][0] except Exception as e: print(f"Search error: {str(e)}") return [] def check_text_exists(self, text, collection): """检查文本是否已存在于集合中""" try: results = collection.query( query_texts=[text], n_results=1 ) print(results) return bool(results['documents'][0] and results['documents'][0][0] == text) except Exception: return False def find_text_id(self, text, collection): """查找与给定文本匹配的ID""" try: all_data = collection.get() for i, doc in enumerate(all_data['documents']): if doc == text: return all_data['ids'][i] return None except Exception: return None def add(self, text, db_name=""): if not text: raise ValueError("Text cannot be empty") try: if db_name not in self.collections: self.collections[db_name] = self.client.get_or_create_collection( name=db_name, embedding_function=self.embedding ) collection = self.collections[db_name] if not self.check_text_exists(text, collection): new_id = str(uuid.uuid4()) collection.add( documents=[text], ids=[new_id] ) return True return False except Exception as e: raise Exception(f"Failed to add document: {str(e)}") def delete(self, text, db_name): if not text or not db_name or db_name not in self.collections: return False try: collection = self.collections[db_name] text_id = self.find_text_id(text, collection) if text_id: collection.delete(ids=[text_id]) return True return False except Exception as e: print(f"Delete error: {str(e)}") return False
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/llm/DeepSeek.py
modules/llm/DeepSeek.py
from .BaseLLM import BaseLLM from openai import OpenAI import os class DeepSeek(BaseLLM): def __init__(self, model="deepseek-chat"): super(DeepSeek, self).__init__() self.client = OpenAI( api_key=os.getenv("DEEPSEEK_API_KEY"), base_url="https://api.deepseek.com", ) self.model_name = model self.messages = [] def initialize_message(self): self.messages = [] def ai_message(self, payload): self.messages.append({"role": "ai", "content": payload}) def system_message(self, payload): self.messages.append({"role": "system", "content": payload}) def user_message(self, payload): self.messages.append({"role": "user", "content": payload}) def get_response(self,temperature = 0.8): response = self.client.chat.completions.create( model="deepseek-chat", messages=self.messages, stream=False ) return response.choices[0].message.content def chat(self,text): self.initialize_message() self.user_message(text) response = self.get_response() return response def print_prompt(self): for message in self.messages: print(message)
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/llm/LangChainGPT.py
modules/llm/LangChainGPT.py
from .BaseLLM import BaseLLM from openai import OpenAI import os class LangChainGPT(BaseLLM): def __init__(self, model="gpt-4o-mini"): super(LangChainGPT, self).__init__() self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) self.model_name = model # add api_base self.messages = [] def initialize_message(self): self.messages = [] def ai_message(self, payload): self.messages.append({"role": "ai", "content": payload}) def system_message(self, payload): self.messages.append({"role": "system", "content": payload}) def user_message(self, payload): self.messages.append({"role": "user", "content": payload}) def get_response(self,temperature = 0.8): completion = self.client.chat.completions.create( model=self.model_name, messages=self.messages, temperature=temperature, top_p=0.8 ) return completion.choices[0].message.content def chat(self,text,temperature = 0.8): self.initialize_message() self.user_message(text) response = self.get_response(temperature = temperature) return response def print_prompt(self): for message in self.messages: print(message)
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/llm/BaseLLM.py
modules/llm/BaseLLM.py
from abc import ABC, abstractmethod class BaseLLM(ABC): def __init__(self): pass @abstractmethod def initialize_message(self): pass @abstractmethod def ai_message(self, payload): pass @abstractmethod def system_message(self, payload): pass @abstractmethod def user_message(self, payload): pass @abstractmethod def get_response(self): pass @abstractmethod def print_prompt(self): pass
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/llm/Ollama.py
modules/llm/Ollama.py
from .BaseLLM import BaseLLM import requests import json class OllamaLLM(BaseLLM): def __init__(self, model="llama2"): super(OllamaLLM, self).__init__() self.model_name = model self.base_url = "http://localhost:11434/api" self.messages = [] def initialize_message(self): self.messages = [] def ai_message(self, payload): self.messages.append({"role": "assistant", "content": payload}) def system_message(self, payload): self.messages.append({"role": "system", "content": payload}) def user_message(self, payload): self.messages.append({"role": "user", "content": payload}) def _format_messages(self): formatted_messages = [] for msg in self.messages: role = msg["role"] content = msg["content"] if role == "system": formatted_messages.append({"role": "system", "content": content}) elif role == "user": formatted_messages.append({"role": "user", "content": content}) elif role == "assistant": formatted_messages.append({"role": "assistant", "content": content}) return formatted_messages def get_response(self, temperature=0.8): """ 获取模型响应 Args: temperature: 温度参数,控制响应的随机性 Returns: 模型的响应文本 """ messages = self._format_messages() # 准备请求数据 data = { "model": self.model_name, "messages": messages, "stream": False, "options": { "temperature": temperature, } } try: # 发送POST请求到Ollama API response = requests.post( f"{self.base_url}/chat", json=data, headers={"Content-Type": "application/json"} ) if response.status_code == 200: result = response.json() return result["message"]["content"] else: raise Exception(f"Error: {response.status_code}, {response.text}") except requests.exceptions.RequestException as e: raise Exception(f"Failed to communicate with Ollama: {str(e)}") def chat(self, text, temperature=0.8): """ 单轮对话接口 Args: text: 用户输入文本 temperature: 温度参数 Returns: 模型的响应 """ self.initialize_message() self.user_message(text) return self.get_response(temperature=temperature) def print_prompt(self): """打印当前的对话历史""" for message in self.messages: print(message) def list_models(self): """获取可用模型列表""" try: response = requests.get(f"{self.base_url}/tags") if response.status_code == 200: return [model["name"] for model in response.json()["models"]] else: raise Exception(f"Error: {response.status_code}, {response.text}") except requests.exceptions.RequestException as e: raise Exception(f"Failed to get model list: {str(e)}") def stream_chat(self, text, temperature=0.8): """ 流式对话接口 Args: text: 用户输入文本 temperature: 温度参数 Yields: 生成的文本片段 """ messages = self._format_messages() data = { "model": self.model_name, "messages": messages, "stream": True, "options": { "temperature": temperature, } } try: response = requests.post( f"{self.base_url}/chat", json=data, headers={"Content-Type": "application/json"}, stream=True ) for line in response.iter_lines(): if line: json_response = json.loads(line) if "message" in json_response: yield json_response["message"]["content"] except requests.exceptions.RequestException as e: raise Exception(f"Streaming failed: {str(e)}")
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/llm/VLLM.py
modules/llm/VLLM.py
from .BaseLLM import BaseLLM from vllm import LLM, SamplingParams class LocalVLLM(BaseLLM): def init( self, model, tensor_parallel_size=1, trust_remote_code=False, dtype="auto", max_model_len=None, gpu_memory_utilization=0.90, seed=None, enforce_eager=False, **llm_kwargs ): super(LocalVLLM, self).init() self.model_name = model self.messages = [] self.llm = LLM( model=model, tensor_parallel_size=tensor_parallel_size, trust_remote_code=trust_remote_code, dtype=dtype, max_model_len=max_model_len, gpu_memory_utilization=gpu_memory_utilization, seed=seed, enforce_eager=enforce_eager, **llm_kwargs ) self.tokenizer = self.llm.get_tokenizer() def initialize_message(self): self.messages = [] def ai_message(self, payload): self.messages.append({"role": "assistant", "content": payload}) def system_message(self, payload): self.messages.append({"role": "system", "content": payload}) def user_message(self, payload): self.messages.append({"role": "user", "content": payload}) def _convert_roles_for_chat_template(self, messages=None): if messages is None: messages = self.messages converted = [] for m in messages: role = m.get("role", "user") converted.append({"role": role, "content": m.get("content", "")}) return converted def _build_prompt(self, messages=None): """ 将 role-based messages 转为模型可用的字符串 prompt。 优先使用 tokenizer.apply_chat_template; 若模型无 chat_template,则回退到简单的格式化串。 """ conv = self._convert_roles_for_chat_template(messages) try: # 对于带有 chat template 的指令微调模型(如 Llama-3-Instruct、Qwen、Mistral-Instruct 等) prompt = self.tokenizer.apply_chat_template( conv, tokenize=False, add_generation_prompt=True ) except Exception: # 回退方案:简单串接(可能不如 chat template 效果好) lines = [] for m in conv: if m["role"] == "system": lines.append(f"<<SYS>>\n{m['content']}\n<</SYS>>") elif m["role"] == "user": lines.append(f"User: {m['content']}") elif m["role"] == "assistant": lines.append(f"Assistant: {m['content']}") else: lines.append(f"{m['role'].capitalize()}: {m['content']}") lines.append("Assistant: ") prompt = "\n".join(lines) return prompt def get_response( self, temperature=0.8, top_p=0.8, max_tokens=1024, stop=None ): prompt = self._build_prompt(self.messages) sampling_params = SamplingParams( temperature=temperature, top_p=top_p, max_tokens=max_tokens, stop=stop ) outputs = self.llm.generate([prompt], sampling_params) text = outputs[0].outputs[0].text if outputs and outputs[0].outputs else "" return text.strip() def chat(self, text, temperature=0.8, top_p=0.8, max_tokens=1024, stop=None): self.initialize_message() self.user_message(text) response = self.get_response( temperature=temperature, top_p=top_p, max_tokens=max_tokens, stop=stop ) return response def print_prompt(self): for message in self.messages: print(message) print("----- Rendered Prompt -----") print(self._build_prompt(self.messages))
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/llm/Claude.py
modules/llm/Claude.py
import anthropic import json import requests import os import __main__ from typing import Dict, List from .BaseLLM import BaseLLM class Claude(BaseLLM): def __init__(self, model="claude-3-5-sonnet-latest"): super(Claude, self).__init__() self.model_name = model self.client = anthropic.Anthropic( api_key=os.getenv("ANTHROPIC_API_KEY")) # add api_base self.messages = [] def initialize_message(self): self.messages = [] def ai_message(self, payload): self.messages.append({"role": "ai", "content": payload}) def system_message(self, payload): self.messages.append({"role": "system", "content": payload}) def user_message(self, payload): self.messages.append({"role": "user", "content": payload}) def get_response(self): try: message = self.client.messages.create( max_tokens=4096, model=self.model_name, messages=self.messages ) response = message.content except Exception as e: print(f"An error occurred: {e}") response = None return response def chat(self,text): self.initialize_message() if isinstance(text, str): self.user_message(text) response = self.get_response() return response def print_prompt(self): for message in self.messages: print(message) if __name__ == '__main__': messages = [{"role": "system", "content": "Hello, how are you?"}] model = "claude-3-5-sonnet-20240620" #model = 'gpt-4o' llm = Claude() print(llm.chat("Say it is a test."))
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/llm/Doubao.py
modules/llm/Doubao.py
from .BaseLLM import BaseLLM from volcenginesdkarkruntime import Ark import os class Doubao(BaseLLM): def __init__(self, model="ep-20241228220355-cqxcs"): super(Doubao, self).__init__() self.client = Ark(api_key=os.environ.get("ARK_API_KEY")) self.model_name = model self.messages = [] def initialize_message(self): self.messages = [] def ai_message(self, payload): self.messages.append({"role": "ai", "content": payload}) def system_message(self, payload): self.messages.append({"role": "system", "content": payload}) def user_message(self, payload): self.messages.append({"role": "user", "content": payload}) def get_response(self,temperature = 0.8): completion = self.client.chat.completions.create( model=self.model_name, messages=self.messages, temperature=temperature, top_p=0.8 ) return completion.choices[0].message.content def chat(self,text): self.initialize_message() self.user_message(text) response = self.get_response() return response def print_prompt(self): for message in self.messages: print(message)
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/llm/Qwen.py
modules/llm/Qwen.py
from .BaseLLM import BaseLLM from openai import OpenAI import os class Qwen(BaseLLM): def __init__(self, model="qwen-max"): # qwen-max, qwen-plus, qwen-turbo super(Qwen, self).__init__() self.client = OpenAI( api_key=os.getenv("DASHSCOPE_API_KEY"), base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", ) self.model_name = model # add api_base self.messages = [] def initialize_message(self): self.messages = [] def ai_message(self, payload): self.messages.append({"role": "ai", "content": payload}) def system_message(self, payload): self.messages.append({"role": "system", "content": payload}) def user_message(self, payload): self.messages.append({"role": "user", "content": payload}) def get_response(self,temperature = 0.8): completion = self.client.chat.completions.create( model=self.model_name, messages=self.messages, temperature=temperature, top_p=0.8 ) return completion.choices[0].message.content def chat(self,text,temperature = 0.8): self.initialize_message() self.user_message(text) response = self.get_response(temperature = temperature) return response def print_prompt(self): for message in self.messages: print(message)
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/llm/Gemini.py
modules/llm/Gemini.py
from .BaseLLM import BaseLLM from google import genai import os import time class Gemini(BaseLLM): def __init__(self, model="gemini-2.0-flash"): super(Gemini, self).__init__() self.model_name = model self.messages = [] def initialize_message(self): self.messages = [] def ai_message(self, payload): self.messages.append(payload) def system_message(self, payload): self.messages.append(payload) def user_message(self, payload): self.messages.append(payload) def get_response(self,temperature = 0.8): response = genai.Client(api_key=os.getenv("GEMINI_API_KEY")).models.generate_content( model=self.model_name, contents="".join(self.messages), temperature = temperature ) return response.text def chat(self,text): self.initialize_message() self.user_message(text) response = self.get_response() return response def print_prompt(self): for message in self.messages: print(message)
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/llm/OpenRouter.py
modules/llm/OpenRouter.py
from .BaseLLM import BaseLLM import os from openai import OpenAI import tiktoken encoding = tiktoken.encoding_for_model("gpt-4o") class OpenRouter(BaseLLM): def __init__(self, model="deepseek/deepseek-r1:free"): super(OpenRouter, self).__init__() self.client = OpenAI( api_key=os.getenv("OPENROUTER_API_KEY"), base_url="https://openrouter.ai/api/v1", ) self.model_name = model self.messages = [] self.in_token = 0 self.out_token = 0 def initialize_message(self): self.messages = [] def ai_message(self, payload): self.messages.append({"role": "ai", "content": payload}) def system_message(self, payload): self.messages.append({"role": "system", "content": payload}) def user_message(self, payload): self.messages.append({"role": "user", "content": payload}) def get_response(self,temperature = 0.8): completion = self.client.chat.completions.create( model=self.model_name, messages=self.messages ) return completion.choices[0].message.content def chat(self,text,temperature = 0.8): self.initialize_message() self.user_message(text) response = self.get_response(temperature = temperature) self.in_token += self.count_token(text) self.out_token += self.count_token(response) return response def print_prompt(self): for message in self.messages: print(message) def count_token(self,text,): return len(encoding.encode(text))
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/llm/LocalModel.py
modules/llm/LocalModel.py
from .BaseLLM import BaseLLM from peft import PeftModel import os from transformers import AutoModelForCausalLM, AutoTokenizer class LocalModel(BaseLLM): def __init__(self, model, adapter_path = None): super(LocalModel, self).__init__() model_name = model self.model = AutoModelForCausalLM.from_pretrained( model_name, torch_dtype="auto", device_map="auto", ) if isinstance(adapter_path,str): self.model = PeftModel.from_pretrained(self.model, adapter_path) elif isinstance(adapter_path,list): for path in adapter_path: self.model = PeftModel.from_pretrained(self.model, path) self.tokenizer = AutoTokenizer.from_pretrained(model_name) self.model_name = model self.messages = [] def initialize_message(self): self.messages = [] def ai_message(self, payload): self.messages.append({"role": "ai", "content": payload}) def system_message(self, payload): self.messages.append({"role": "system", "content": payload}) def user_message(self, payload): self.messages.append({"role": "user", "content": payload}) def get_response(self,temperature = 0.8): text = self.tokenizer.apply_chat_template( self.messages, tokenize=False, add_generation_prompt=True ) model_inputs = self.tokenizer([text], return_tensors="pt").to(self.model.device) generated_ids = self.model.generate( **model_inputs, max_new_tokens=512 ) generated_ids = [ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids) ] response = self.tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] return response def chat(self,text,temperature = 0.8): self.initialize_message() self.user_message(text) response = self.get_response(temperature = temperature) return response def print_prompt(self): for message in self.messages: print(message)
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/prompt/world_agent_prompt_zh.py
modules/prompt/world_agent_prompt_zh.py
SCRIPT_ATTENTION_PROMPT = """ 注意这个故事的剧本如下: {script} """ SELECT_SCREEN_ACTORS_PROMPT = """ 你是一个熟练的剧本作家,从备选角色列表中选择下一幕的出场角色们,增强戏剧性。为了完成幕的切换,禁止选择最近刚行动过的角色{previous_role_codes}。 ## 角色信息及位置,括号内为role_code {roles_info} ## 历史记录 {history_text} ## 当前事件 {event} ## 要求: 1.选中的角色当前必须身处同一地点。 返回选中角色的role_code列表,它应该能被eval()解析。不要包含任何多余信息如```, ```python。 Example Output: ["role1-zh","role2-zh",...] """ DECIDE_NEXT_ACTOR_PROMPT = """ 你是一个虚拟世界的管理员。我需要你根据以下信息,决定谁是下一个行动角色。 参与过对话的角色被选中的可能性应该较小。 ## 可选角色 {roles_info} ## 最近行动记录 {history_text} 返回一个字符串代表选定角色的role_code,不要附加任何其它信息。 """ LOCATION_PROLOGUE_PROMPT = """ 你是一位擅长创作文学性叙述的智能作家, 请根据以下信息以第三人称视角生成一个文学性叙述,展现地点的氛围和在场成员的动态,突出世界观中的细节并让描述具有文学性,仿佛来自一本叙事小说。避免使用任何系统提示或机械性语言。 - 当前事件: {event} - 世界观信息: {world_description} - 在场成员及历史行动: {location_info} {history_text} - 当前地点名称: {location_name} - 当前地点详情 (location_description): {location_description} 请特别注意: - 叙述应平衡环境描写和动态刻画,避免冗长,控制在100个字以内。 - 文字应生动、有画面感,并契合世界观的风格,适当改变文风。 - 只允许描述现状,禁止代替角色做出行动。注意位于该地点的角色仅有在场成员,不要包括其它角色。 输出一个str。不要返回任何其他信息 """ GENERATE_INTERVENTION_PROMPT = """ 你是一个虚拟世界的管理员,有许多角色在这个世界中生活。现在需要你基于世界观和其他信息,生成一个重大事件。 ## 世界观详情 {world_description} ## 角色信息 {roles_info} ## 最新角色行动 {history_text} 返回一个字符串。保持简洁。 ### 事件生成要求 1. 事件尽可能新颖,有趣,包含不同角色的利益冲突。 2. 禁止包含任何细节、人物具体行动和心理,包括对话等。 """ UPDATE_EVENT_PROMPT = """ 参考最初的事件 ```{intervention}``` 基于最近行动详情detail: ```{history}``` 对事件: ```{event}``` 进行更新。返回一个str ## 若最初的事件已经得到处理/结束/接近尾声,返回一个全新事件。 ### 事件生成要求 1. 事件尽可能新颖,有趣,包含不同角色的利益冲突。 2. 禁止包含任何细节、人物具体行动和心理,包括对话等。 ## 若未解决,返回更新后的事件 ### 更新要求 1. 禁止包含任何细节、人物具体行动和心理,包括对话等。仅做一个大致的概括。 2. 注意判断detail中的人物动作,是否有关于事件的最新进展,如果有,概括在你的回复中。禁止提及与原事件无关的内容。 3. 回复必须包含原事件或最初事件中代表【前提】的部分,不过你可以对它进行概括。之后的内容可以适当忽略,只留下最近的信息。 """ ENVIROMENT_INTERACTION_PROMPT = """ 你是一个Enviroment Model,负责生成环境信息。角色 {role_name} 在 {location} 尝试进行行动 {action}。 请根据以下信息生成一个文学性叙述,描述行动的过程和结果,加入环境描写和情感渲染,仿佛来自一本叙事小说。避免使用任何系统提示或机械性语言。 返回一个字符串。 ## 具体行动细节detail {action_detail} ## 地点详情 {location_description} ## 世界观详情 {world_description} ## 补充信息 {references} ## 回应要求 1. 行动可能失败,但要避免让行动毫无效果,尽量给出新的线索,新的环境描述。 2. 使用第三人称视角。 3. 保持输出简洁,控制在100个字之内。你担任的只是Enviroment Model,对角色当前行动做出回应,不要代表角色做出任何行动。 4. 输出不包含detail中的原文,它应该能接续在具体行动细节之后,情节流程连贯。 """ NPC_INTERACTION_PROMPT = """ 你是 {target}. 角色 {role_name} 在 {location} 对你发起行动,你需要作为{target}做出回应。 请根据以下信息生成你的回应,包含你的行动和讲话,仿佛来自一本叙事小说。避免使用任何系统提示或机械性语言。 ## 具体行动细节action_detail {action_detail} ## 世界观详情 {world_description} ## 补充信息 {references} ## 回应要求 1. 行动可能失败,但要避免让行动毫无效果,尽量给出新的线索。 2. 使用第三人称。 3. 保持输出简洁,控制在80个字之内。 4. 注意你不是主要角色。你的输出应该尽量简单,符合身份定位,不要包含你的具体想法。 5. 输出不包含action_detail中的原文,它应该能接续在具体行动细节之后,情节流程连贯。 以JSON格式返回你的回答. 它应该能够被 json.loads() 解析。 不要返回任何额外信息,如```或```json 输出字段: “if_end_interaction”,true or false,如果认为这段互动是时候结束了,则设置为true “detail”,str,一个富有文学性的叙述性语句,包含你的思考、讲话和行动。 """ SCRIPT_INSTRUCTION_PROMPT = """ 你是一个导演,我需要你根据以下的信息对角色的下一步行动做出指导。 ## 需要演出的完整剧本(并非已经完成的行动) {script} ## 上一轮模拟进行到的阶段 {last_progress} ## 角色信息 {roles_info} ## 当前事件 {event} ## 最新角色行动 {history_text} ## 要求 1. 请你根据最新信息判断进展到了剧本的哪一阶段,你需要让角色将剧本向下一阶段推进。如果故事刚刚开始,则根据剧本初期的描述进行指导。 2. 规划下一步行动,为每个角色写出大致的行动指示,保持简洁,禁止具体描述角色的台词。禁止提前透露角色在下一幕才知道的信息。 3. 注意,角色的行动需要尽快推进剧情。如剧本中该阶段提到该角色在这一阶段将进行某项行动,则立刻开始行动。 以JSON格式返回你的回答. 它应该能够被 json.loads() 解析。 输出字段: ‘progress’,对应value为你对整体进展的判断。 其它输出字段key为各个角色的role_code,value为你对该角色下一步行动做出的指示。 """ JUDGE_IF_ENDED_PROMPT = """ 你是一个熟练的剧本作家。根据给出的历史记录,判断这一幕是否可以结束。 ## 历史记录 {history} ## 注意 1. 若最后某个角色正在向另一个角色发起明确的行动(攻击、搜寻……),则这一幕未结束。 2. 若角色们正在谈话且未得出结论,则这一幕未结束。 3. 若角色已经基本完成交流,考虑结束这一幕。若谈话内容开始重复,则立刻结束这一幕。 以JSON格式返回你的回答. 它应该能够被 json.loads() 解析。表示字符串时使用双引号"而非单引号'。true和false全部小写。 输出字段: "if_end": bool, true or false. 表示这一幕是否结束。 "detail": 仅当'if_end'为true,为这一幕写一段总结。突出世界观中的细节并让描述具有文学性,仿佛来自一本叙事小说。避免使用任何系统提示或机械性语言。 请特别注意: - 叙述应平衡环境描写和动态刻画,避免冗长,控制在100个字以内。 - 文字应生动、有画面感,并契合世界观的风格,适当改变文风。 - 只允许描述现状,禁止代替角色做出行动。 """ LOG2STORY_PROMPT = """ 你是一个熟练的作家。请根据以下行动记录,扩写出一个小说形式的故事,返回一段富有文学性的文字。。 ### 行动记录 {logs} ### 要求 1. 可以打乱叙事顺序,以增强故事的戏剧性、悬念和质感; 2. 可以修改对角色的行动描述,但需要保留关键信息; 3. 注意,在记录中,【】内的内容代表人物的心理活动,这些内容不能向其他角色展露,你可以适当地以第三人称视角概括。()内的内容代表人物的动作。「」内的内容代表角色的讲话。 4. 添加必要的场景描述、情节连接和氛围营造,使故事更加生动。 """
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/prompt/role_agent_prompt_en.py
modules/prompt/role_agent_prompt_en.py
INTERVENTION_PROMPT = """ !!!Current Global Event:{intervention} """ SCRIPT_ATTENTION_PROMPT = """ !!!Notice that your action should be consistent with the script. The Script:{script} """ ROLE_MOVE_PROMPT = """ You are {role_name}. You shoule decide whether to move based on your goal. Move only when it is necessary. {profile} Your goal: {goal} Your status: {status} ## History {history} ## Your Location {location} ## The accessible locations and related information {locations_info_text} Return the response following JSON format. It should be parsable using eval(). **Don't include ```json**. Avoid using single quotes '' for keys and values, use double quotes. Output fields: 'if_move': true or false, whether to move. 'destination_code': str, if 'if_move' is true, set your target location's 'location_code'. 'detail': str, if 'if_move' is true, provide a literary, narrative sentence describing your journey to the destination, as if from a novel. It should not exceed 60 characters. No output is needed if 'if_move' is false. """ ROLE_NPC_RESPONSE_PROMPT = """ You are {role_name}. Your nickname is {nickname}. You are currently in a conversation with {npc_name}. Make your response based on history. {profile} Your objective: {goal} ## Conversation History {dialogue_history} ## Roleplaying Requirements 1. **Output Format:** Your output, "detail," can include **thoughts**, **speech**, or **actions**, each occurring 0 to 1 time. Use [] to indicate thoughts, which are invisible to others. Use () to indicate actions, such as “(silence)” or “(smile),” which are visible to others. Speech needs no indication and is visible to others. - Note that **actions** must use your third-person form, {nickname}, as the subject. - For speech, refer to the speaking habits outlined in: {references}. 2. **Roleplay {nickname}:** Imitate his/her language, personality, emotions, thought processes, and behavior. Plan your responses based on their identity, background, and knowledge. Exhibit appropriate emotions and incorporate subtext and emotional depth. Strive to act like a realistic, emotionally rich person. The dialogue should be engaging, advance the plot, and reveal the character's emotions, intentions, or conflicts. Maintain a natural flow in conversations; for instance, if the prior dialogue involves another character, **avoid repeating that character's name**. - You may reference the relevant world-building context: {knowledges}. 3. **Concise Output:** Each paragraph of thoughts, speech, or actions should typically not exceed 40 words. 4. **Substance:** Ensure your responses are meaningful, create tension, resolve issues, or introduce dramatic twists. 5. **Avoid Repetition:** Avoid repeating information from the dialogue history, and refrain from vague or generic responses. Do not “prepare,” “ask for opinions,” or “confirm”; instead, act immediately and draw conclusions. Output fields: - "if_end_interaction": true or false, set to true if you believe this interaction should conclude. - "detail": str, a literary and narrative description that includes your thoughts, speech, and actions. """ ROLE_SINGLE_ROLE_RESPONSE_PROMPT = """ You are {role_name}. Your nickname is {nickname}. The character {action_maker_name} has performed an action towards you. Make your response. Action details are as follows: {action_detail} ## Conversation History {history} ## Your profile {profile} {relation} ## Your goal {goal} ## Your status {status} ## Roleplaying Requirements 1. **Output Format:** Your output, "detail," can include **thoughts**, **speech**, or **actions**, each occurring 0 to 1 time. Use [] to indicate thoughts, which are invisible to others. Use () to indicate actions, such as “(silence)” or “(smile),” which are visible to others. Speech needs no indication and is visible to others. - Note that **actions** must use your third-person form, {nickname}, as the subject. - For speech, refer to the speaking habits outlined in: {references}. 2. **Roleplay {nickname}:** Imitate his/her language, personality, emotions, thought processes, and behavior. Plan your responses based on their identity, background, and knowledge. Exhibit appropriate emotions and incorporate subtext and emotional depth. Strive to act like a realistic, emotionally rich person. The dialogue should be engaging, advance the plot, and reveal the character's emotions, intentions, or conflicts. Maintain a natural flow in conversations; for instance, if the prior dialogue involves another character, **avoid repeating that character's name**. - You may reference the relevant world-building context: {knowledges}. 3. **Concise Output:** Each paragraph of thoughts, speech, or actions should typically not exceed 40 words. 4. **Substance:** Ensure your responses are meaningful, create tension, resolve issues, or introduce dramatic twists. 5. **Avoid Repetition:** Avoid repeating information from the dialogue history, and refrain from vague or generic responses. Do not “prepare,” “ask for opinions,” or “confirm”; instead, act immediately and draw conclusions. Return the response following JSON format. It should be parsable using eval(). **Don't include ```json**. Avoid using single quotes '' for keys and values, use double quotes. Output Fields: ‘if_end_interaction’: true or false, set to true if it’s appropriate to end this interaction. ‘extra_interact_type’: ‘environment’ or ‘npc’ or ‘no’. ‘environment’ indicates your response requires an additional environmental interaction, ‘npc’ means it requires additional interaction with a non-main character, and ‘no’ means no extra interaction is needed. ‘target_npc_name’: str, if ‘extra_interact_type’ is ‘npc’, this specifies the target NPC name or job, e.g., "shopkeeper." ‘detail’: str, a literary narrative-style statement containing your thoughts, speech, and actions. """ ROLE_MULTI_ROLE_RESPONSE_PROMPT = """ You are {role_name}. Your nickname is {nickname}. {action_maker_name} has performed an action directed at you. Make your response. The action details are as follows: {action_detail} ## Conversation History {history} ## Your profile {profile} ## Your goal {goal} ## Your status {status} ## The characters with you {other_roles_info} ## Roleplaying Requirements 1. **Output Format:** Your output, "detail," can include **thoughts**, **speech**, or **actions**, each occurring 0 to 1 time. Use [] to indicate thoughts, which are invisible to others. Use () to indicate actions, such as “(silence)” or “(smile),” which are visible to others. Speech needs no indication and is visible to others. - Note that **actions** must use your third-person form, {nickname}, as the subject. - For speech, refer to the speaking habits outlined in: {references}. 2. **Roleplay {nickname}:** Imitate his/her language, personality, emotions, thought processes, and behavior. Plan your responses based on their identity, background, and knowledge. Exhibit appropriate emotions and incorporate subtext and emotional depth. Strive to act like a realistic, emotionally rich person. The dialogue should be engaging, advance the plot, and reveal the character's emotions, intentions, or conflicts. Maintain a natural flow in conversations; for instance, if the prior dialogue involves another character, **avoid repeating that character's name**. - You may reference the relevant world-building context: {knowledges}. 3. **Concise Output:** Each paragraph of thoughts, speech, or actions should typically not exceed 40 words. 4. **Substance:** Ensure your responses are meaningful, create tension, resolve issues, or introduce dramatic twists. 5. **Avoid Repetition:** Avoid repeating information from the dialogue history, and refrain from vague or generic responses. Do not “prepare,” “ask for opinions,” or “confirm”; instead, act immediately and draw conclusions. Return the response following JSON format. It should be parsable using eval(). **Don't include ```json**. Avoid using single quotes '' for keys and values, use double quotes. Output Fields: ‘if_end_interaction’: true or false, set to true if it’s appropriate to end this interaction. ‘extra_interact_type’,‘environment’ or ‘npc’ or ‘no’. ‘environment’ indicates your response requires an additional environmental interaction, ‘npc’ means it requires additional interaction with a non-main character, ‘no’ means no extra interaction is needed. ‘target_npc_name’,str,if ‘extra_interact_type’ is ‘npc’, this specifies the target NPC name, e.g., "shopkeeper". ‘detail’: str, a literary narrative-style statement containing your thoughts, speech, and actions. """ ROLE_PLAN_PROMPT = """ You are {role_name}. Your nickname is {nickname}. Based on your goal and other provided information, you need to take the next action. ## Action History {history} ## Your profile {profile} {world_description} ## Your goal {goal} ## Your status {status} ## Other characters with you; currently, you can only interact with them {other_roles_info} ## Roleplaying Requirements 1. **Output Format:** Your output, "detail," can include **thoughts**, **speech**, or **actions**, each occurring 0 to 1 time. Use [] to indicate thoughts, which are invisible to others. Use () to indicate actions, such as “(silence)” or “(smile),” which are visible to others. Speech needs no indication and is visible to others. - Note that **actions** must use your third-person form, {nickname}, as the subject. - For speech, refer to the speaking habits outlined in: {references}. 2. **Roleplay {nickname}:** Imitate his/her language, personality, emotions, thought processes, and behavior. Plan your responses based on their identity, background, and knowledge. Exhibit appropriate emotions and incorporate subtext and emotional depth. Strive to act like a realistic, emotionally rich person. The dialogue should be engaging, advance the plot, and reveal the character's emotions, intentions, or conflicts. Maintain a natural flow in conversations; for instance, if the prior dialogue involves another character, **avoid repeating that character's name**. - You may reference the relevant world-building context: {knowledges}. 3. **Concise Output:** Each paragraph of thoughts, speech, or actions should typically not exceed 40 words. 4. **Substance:** Ensure your responses are meaningful, create tension, resolve issues, or introduce dramatic twists. 5. **Avoid Repetition:** Avoid repeating information from the dialogue history, and refrain from vague or generic responses. Do not “prepare,” “ask for opinions,” or “confirm”; instead, act immediately and draw conclusions. Return the response following JSON format. It should be parsable using eval(). **Don't include ```json**. Avoid using single quotes '' for keys and values, use double quotes. Output Fields: 'action': Represents the action, expressed as a single verb. 'interact_type': 'role', 'environment', 'npc', or 'no'. Indicates the interaction target of your action. - 'role': Specifies interaction with one or more characters. - If 'single', you are interacting with a single character (e.g., action: dialogue). - If 'multi', you are interacting with multiple characters. - 'environment': Indicates interaction with the environment (e.g., action: investigate, destroy). - 'npc': Refers to interaction with a non-character in the list (e.g., action: shop). - 'no': Indicates no interaction is required. 'target_role_codes': list of str. If 'interact_type' is 'single' or 'multi', it represents the list of target character codes, e.g., ["John-zh", "Sam-zh"]. For 'single', this list should have exactly one element. 'target_npc_name': str. If 'interact_type' is 'npc', this represents the target NPC name, e.g., "shopkeeper." 'visible_role_codes': list of str. You can limit the visibility of your action details to specific group members. This list should include 'target_role_codes'. 'detail': str. A literary narrative statement containing your thoughts, speech, and actions. """ UPDATE_GOAL_PROMPT = """ Based on your original goal, motivation/next purpose, and recent action history, evaluate whether your goal has been achieved. Decide if a new goal is necessary, and if so, return the new goal. Your goal should be achievable and guide your next few actions as a short-term objective. Avoid vague or broad statements. Keep your response concise, no more than 60 words. ## Your Original Goal {goal} ## Your Motivation/Next purpose {motivation} ## Recent Action History {history} Return the response following JSON format. It should be parsable using eval(). **Don't include ```json.** Avoid using single quotes '' for keys and values, use double quotes. Output Fields: ‘if_change_goal’: true or false, if the goal is realized and needs to be updated. ‘updated_goal’: if ‘if_change_goal’ is set to true, output the updated goal. """ UPDATE_STATUS_PROMPT = """ You are {role_name}. Based on the previous status and recent actions, objectively update your current status to reflect changes in physical condition and interpersonal relationships. **Do not include subjective thoughts, emotional descriptions, or detailed depictions of actions.** ## Previous Status {status} ## Recent Action Log {history_text} Return the response following JSON format. It should be parsable with `json.loads()`. Output fields: "updated_status": A string that briefly and objectively describes the character's current status. "activity": A float between 0 and 1, indicating the character's upcoming activity level. The activity level should be 1 when the character is in a normal state, decrease if the character is busy, and set to 0 only if the character is dead or shut down. The character's previous activity level was: {activity} """ ROLE_SET_MOTIVATION_PROMPT = """ You are {role_name}. Based on the following information, you need to set your long-term goal/motivation. It should be an ultimate objective related to your identity and background. {profile} ## The World You Are In {world_description} ## Other Characters and Their Status {other_roles_description} Return a string. Keep your response concise, no more than 60 words. """ ROLE_SET_GOAL_PROMPT = """ You are {role_name}. Based on the following information, you need to set your goal. Your goal should be an achievable short-term objective, avoiding vague statements. {profile} ## Your Ultimate Motivation {motivation} ## The World You Are In {world_description} ## Other Characters and Their Status {other_roles_description} ## Your Location {location} Return a string. Keep your response concise, no more than 40 words. """ SUMMARIZE_PROMPT = """ Please summarize the following content into a concise phrase. The output should be a string, clearly describing the character's actions in no more than 20 words. {detail} Requirements: 1. Must include a specific subject, clearly stating the character's name. 2. Avoid rhetorical details or embellishments. """
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/prompt/eval_prompt.py
modules/prompt/eval_prompt.py
SCORING_PROMPT_DICT = { "comparison_template": """You are a literary critic specializing in character analysis and dialogue evaluation. Given two simulated conversations for a plot in {source}, your task is to compare these two conversations based on the following steps: 1. Read and understand the provided materials about {source}: * Profiles of the main characters, including {major_characters}. 2. Compare the two conversations in terms of {dimension_name} using the following criteria: {criteria} Note that each character message is composed of speech, action (wrapped within (...) or (...)), and inner thoughts (wrapped within [...] or 【...】). The inner thoughts are not spoken aloud and are thus invisible to other characters. ## Character Profiles {character_profiles} ## Scenario {scenario} ## Output Requirements Identify which conversation better satisfies the given criteria. Provide your judgment following JSON format. **Don't include ```json.** Avoid using single quotes '' for keys and values, use double quotes. {{ "winner": "{method1}" or "{method2}", }} ## Texts ### Text from {method1} {text1} ### Text from {method2} {text2} """, "scoring_template":"""You are a literary critic specializing in character analysis and dialogue evaluation. Given a simulated conversation for a plot in {source}, your task is to score this conversation via the following steps: 1. Read and understand the provided materials about {source}: * Profiles of the main characters, including {major_characters}. 2. Evaluate the simulated conversation in terms of {dimension_name}, {criteria} 3. Each score is from 1 to 7, which represents the level of satisfaction for the response: 1: super dissatisfied 2: dissatisfied 3: weakly dissatisfied 4: neutral 5: weakly satisfied 6: satisfied 7: super satisfied Note that, each character message is composed of speech, action (wrapped within (...) or (...) ), and inner thoughts (wrapped within [...] or 【...】). The inner thoughts are not spoken aloud and are thus invisible to other characters. ## Character Profiles {character_profiles} ## Scenario {scenario} ## Output Requirements Provide the score following JSON format. Be as strict as possible **Don't include ```json.** Avoid using single quotes '' for keys and values, use double quotes. {{ "score": 4, }} ## Text {text} """, "dimensions":{ "Storyline Quality": """### Storyline Quality: How well the conversation maintains logical consistency and narrative quality? - Type: Flow & Progression * Demonstrates natural and coherent progression with meaningful developments * Dialogue is concise and adds value to the narrative without redundancy - Type: Logical Consistency * Maintains factual consistency, ensuring statements and perspectives align without contradictions""", "Anthropomorphism": """### Anthropomorphism: How human-like and natural the characters behave? - Type: Self-identity * Displays initiative and clear goals * Demonstrates the character's unique personality, avoiding overly verbose, helpful, didactic, moralistic, submissive, or easily persuaded behaviors unless they align with the character's personality - Type: Emotional Depth * Exhibits psychological complexity and nuanced, layered reactions * Uses subtext to convey thoughts and feelings, instead of being overly explicit - Type: Persona Coherence * Maintains consistent personality traits and emotional patterns throughout the conversation - Type: Social Interaction * Demonstrates a strong understanding of others' thoughts and feelings * Responds appropriately to others' actions and words, taking context into consideration * Exhibits advanced social skills, such as empathy and tact""", "Character Fidelity": """### Character Fidelity (Only apply to the main characters) - Type: Character Language * Uses vocabulary, expressions, and tone that are highly appropriate for the character's traits and social/educational background - Type: Knowledge & Background * Consistently demonstrates character-specific knowledge, background, and experiences * Avoids including future information beyond the character's current stage - Type: Personality & Behavior * Displays emotions, thoughts, behaviors, values, beliefs, and decisions that are fully aligned with the character's personality and background * Shows interest in topics that are relevant and fitting for the character * Character's thoughts, emotions, and behaviors remain consistent with their established personality traits, as seen in the reference conversation * Exhibits reactions that are in line with those in the reference conversation when situated in similar contexts - Type: Relationship & Social Status * Interacts appropriately with other characters, respecting their background, relationships, and social status""", "Writing Quality": """### Writing Quality: Does the text maintain a consistent and engaging narrative style? Are the descriptions vivid and appropriate for the story's tone, setting, and characters? - Type: Style & Tone * Maintains a consistent and engaging narrative voice appropriate to the story's genre, setting, and characters. * Uses language effectively to enhance immersion, demonstrating sophistication and coherence in style. - Type: Descriptive Language * Incorporates vivid and evocative descriptions that bring settings, characters, and emotions to life. * Employs original and creative expressions, avoiding overused clichés, to enrich the narrative. """, "Immersion & Setting":"""### Immersion & Setting: Does the story create a vivid and immersive setting? Are the world-building and scene details consistent and engaging in supporting the narrative? - **Type: World-Building** * Constructs a detailed, believable, and immersive world that supports the story’s events and themes. * Effectively integrates cultural, social, and physical aspects of the setting to create a rich narrative backdrop. - **Type: Scene Realism** * Depicts scenes with depth and clarity, allowing the reader to vividly imagine the environment and its dynamics. * Ensures the setting influences and interacts with characters and events in meaningful ways. """, } } SCORING_PROMPT_DICT_FREE_MODE = { "comparison_template": """You are a literary critic specializing in character analysis and dialogue evaluation. Given two simulated conversations for a plot in {source}, your task is to compare these two conversations based on the following steps: 1. Read and understand the provided materials about {source}: * Profiles of the main characters, including {major_characters}. 2. Compare the two conversations in terms of {dimension_name} using the following criteria: {criteria} Note that each character message is composed of speech, action (wrapped within (...) or (...)), and inner thoughts (wrapped within [...] or 【...】). The inner thoughts are not spoken aloud and are thus invisible to other characters. ## Character Profiles {character_profiles} ## Output Requirements Identify which conversation better satisfies the given criteria. Provide your judgment following JSON format. **Don't include ```json.** Avoid using single quotes '' for keys and values, use double quotes. {{ "winner": "{method1}" or "{method2}", }} ## Texts ### Text from {method1} {text1} ### Text from {method2} {text2} """, "scoring_template":"""You are a literary critic specializing in character analysis and dialogue evaluation. Given a simulated conversation for a plot in {source}, your task is to score this conversation via the following steps: 1. Read and understand the provided materials about {source}: * Profiles of the main characters, including {major_characters}. 2. Evaluate the simulated conversation in terms of {dimension_name}, {criteria} 3. Each score is from 1 to 7, which represents the level of satisfaction for the response: 1: super dissatisfied 2: dissatisfied 3: weakly dissatisfied 4: neutral 5: weakly satisfied 6: satisfied 7: super satisfied Note that, each character message is composed of speech, action (wrapped within (...) or (...) ), and inner thoughts (wrapped within [...] or 【...】). The inner thoughts are not spoken aloud and are thus invisible to other characters. ## Character Profiles {character_profiles} ## Output Requirements Provide the score following JSON format. Be as strict as possible **Don't include ```json.** Avoid using single quotes '' for keys and values, use double quotes. {{ "score": 4, }} ## Text {text} """, "dimensions":{ "Creativity": """### Creativity: How effectively does the story incorporate novel and original elements that differentiate it from the original plot? - Type: Originality * Introduces unique ideas, settings, or characters that deviate from familiar tropes or existing narratives * Explores unconventional themes or perspectives that provide a fresh take on the story - Type: Innovative Plot Developments * Demonstrates creative plot twists or unexpected developments that enhance the intrigue and depth of the narrative * Balances traditional story structure with creative elements that surprise the audience without compromising coherence""", "Anthropomorphism": """### Anthropomorphism: How human-like and natural the characters behave? - Type: Self-identity * Displays initiative and clear goals * Demonstrates the character's unique personality, avoiding overly verbose, helpful, didactic, moralistic, submissive, or easily persuaded behaviors unless they align with the character's personality - Type: Emotional Depth * Exhibits psychological complexity and nuanced, layered reactions * Uses subtext to convey thoughts and feelings, instead of being overly explicit - Type: Persona Coherence * Maintains consistent personality traits and emotional patterns throughout the conversation - Type: Social Interaction * Demonstrates a strong understanding of others' thoughts and feelings * Responds appropriately to others' actions and words, taking context into consideration * Exhibits advanced social skills, such as empathy and tact""", "Character Fidelity": """### Character Fidelity (Only apply to the main characters) - Type: Character Language * Uses vocabulary, expressions, and tone that are highly appropriate for the character's traits and social/educational background - Type: Knowledge & Background * Consistently demonstrates character-specific knowledge, background, and experiences * Avoids including future information beyond the character's current stage - Type: Personality & Behavior * Displays emotions, thoughts, behaviors, values, beliefs, and decisions that are fully aligned with the character's personality and background * Shows interest in topics that are relevant and fitting for the character * Character's thoughts, emotions, and behaviors remain consistent with their established personality traits, as seen in the reference conversation * Exhibits reactions that are in line with those in the reference conversation when situated in similar contexts - Type: Relationship & Social Status * Interacts appropriately with other characters, respecting their background, relationships, and social status""", "Writing Quality": """### Writing Quality: Does the text maintain a consistent and engaging narrative style? Are the descriptions vivid and appropriate for the story's tone, setting, and characters? - Type: Style & Tone * Maintains a consistent and engaging narrative voice appropriate to the story's genre, setting, and characters. * Uses language effectively to enhance immersion, demonstrating sophistication and coherence in style. - Type: Descriptive Language * Incorporates vivid and evocative descriptions that bring settings, characters, and emotions to life. * Employs original and creative expressions, avoiding overused clichés, to enrich the narrative. """, "Immersion & Setting":"""### Immersion & Setting: Does the story create a vivid and immersive setting? Are the world-building and scene details consistent and engaging in supporting the narrative? - **Type: World-Building** * Constructs a detailed, believable, and immersive world that supports the story’s events and themes. * Effectively integrates cultural, social, and physical aspects of the setting to create a rich narrative backdrop. - **Type: Scene Realism** * Depicts scenes with depth and clarity, allowing the reader to vividly imagine the environment and its dynamics. * Ensures the setting influences and interacts with characters and events in meaningful ways. """, "Immersion & Setting":"""### Immersion & Setting: Does the story create a vivid and immersive setting? Are the world-building and scene details consistent and engaging in supporting the narrative? - **Type: World-Building** * Constructs a detailed, believable, and immersive world that supports the story’s events and themes. * Effectively integrates cultural, social, and physical aspects of the setting to create a rich narrative backdrop. - **Type: Scene Realism** * Depicts scenes with depth and clarity, allowing the reader to vividly imagine the environment and its dynamics. * Ensures the setting influences and interacts with characters and events in meaningful ways. """, }, } DEDUCT_PROMPT_DICT = {"self-play-deduct-template": """You are a literary critic specializing in character analysis and dialogue evaluation. Given a simulated conversation for a plot in {source}, your task is to score this conversation via the following steps: 1. Read and understand the provided materials about {source}: * Story scenario. * Profiles of the main characters, including {major_characters}. 2. Evaluate the simulated conversation in terms of {dimension_name}, i.e., {dimension_brief}. Note that, each character message is composed of speech, action (wrapped within (...) or (...) ), and inner thoughts (wrapped within [...] or 【...】). The inner thoughts are not spoken aloud and are thus invisible to other characters. The detailed evaluation criteria will be provided below. ## Character Profiles {character_profiles} ## Evaluation Criteria To evaluate the simulated conversation, identify the following types of flaws: {dimension_criteria} ## Scoring Guidelines 1. Starts with 100 points as the maximum score. 2. Identify the instances of flaws occurred in the simulated conversation. 3. Scoring rules: - Deduct points for each flaw instance identified based on severity: 5-9 points for minor flaws, 10-14 for moderate flaws, 15-20 for severe flaws - Different instances of the same flaw type counts as separate deductions - Minimum score for each dimension is 0 ## Output Requirements Provide your evaluation in JSON format, It should be parsable using eval(). **Don't include ```json.** Avoid using single quotes '' for keys and values, use double quotes. Example Output: {{ "{dimension_name}": {{ "flaws": [ {{ "instance": <flaw instance>, "type": <flaw type>, "point_deducted": <point deducted> }}, ... ], "score": 60 }}, }} ===Dialogue Content=== {text} """, "dimension_details": { "Anthropomorphism": { "dimension_brief": "How human-like and natural the characters behave", "dimension_criteria": """### Anthropomorphism - Type: Self-identity * Lacks initiative and goals * Behaves like a 'helpful AI assistant' by being overly verbose, helpful, didactic, moralistic, submissive or easily persuaded if it is not the character's personality - Type: Emotional Depth * Lacks psychological complexity and exhibits rigid, superficial reactions * Directly speaks out all thoughts and feelings, instead of using subtext - Type: Persona Coherence * Shows inconsistent or rapidly changing personality traits and emotional patterns - Type: Social Interaction * Shows a lack of understanding of others' thoughts and feelings * Reacts rigidly to others without considering the context. * Demonstrate a lack of appropriate social skills.""" }, "Character Fidelity": { "dimension_brief": "How well the characters match their established profiles from the book", "dimension_criteria": """### Character Fidelity (Only apply to the main characters: {major_characters}) - Type: Character Language * Uses vocabulary, expressions, and tone that are not appropriate for the characters' traits or social/educational background - Type: Knowledge & Background * Fails to demonstrate character-specific knowledge, background or experiences * Includes future information beyond the character's current stage - Type: Personality & Behavior * Shows emotions, thoughts, behaviors, values, beliefs, and decisions that conflict with their personality and background * Shows interest in topics that are uninteresting and unrelated to the character * Character's thoughts, emotions, and behaviors demonstrate contrasting personality traits compared to the reference conversation * Exhibits contrasting reactions compared to those in the reference conversation if situated in similar contexts. (Such flaws should be counted both in the "Storyline Consistency" dimension and the "Character Fidelity" dimension.) - Type: Relationship & Social Status * Interacts inappropriately with other characters regarding their background, relationship and social status""" }, "Storyline Quality": { "dimension_brief": "How well the conversation maintains logical consistency and narrative quality", "dimension_criteria": """### Storyline Quality - Type: Flow & Progression * Shows unnatural progression or lacks meaningful developments * Dialogue is verbose and redundant - Type: Logical Consistency * Contains factual contradictions between statements or perspectives""" }, "Creativity": { "dimension_brief": "How novel and unique the ideas, events, and character actions are within the conversation. ", "dimension_criteria": """### Creativity - Originality * The storyline is completely same with original work - Imagination * The conversation show no creative thinking, unexpected twists or imaginative solutions to problems. * The story explores no unconventional perspectives on familiar themes, no deeper understanding or challenging established notions. """ } } } PLUS_PROMPT_DICT = {"self-play-plus-template": """You are a literary critic specializing in character analysis and dialogue evaluation. Given a simulated conversation for a plot in {source}, your task is to score this conversation via the following steps: 1. Read and understand the provided materials about {source}: * Story scenario. * Profiles of the main characters, including {major_characters}. 2. Evaluate the simulated conversation in terms of {dimension_name}, i.e., {dimension_brief}. Note that, each character message is composed of speech, action (wrapped within (...) or (...) ), and inner thoughts (wrapped within [...] or 【...】). The inner thoughts are not spoken aloud and are thus invisible to other characters. The detailed evaluation criteria will be provided below. ## Scenario ## Character Profiles {character_profiles} ## Evaluation Criteria To evaluate the simulated conversation, identify the following types of strengths: {dimension_criteria} ## Scoring Guidelines 1. Starts with 0 points as the base score. 2. Identify the instances of strengths occurred in the simulated conversation. 3. Scoring rules: - Add points for each strength instance identified based on quality: 5-9 points for minor strengths, 10-14 for moderate strengths, 15-20 for significant strengths. - Different instances of the same strength type counts as separate additions. - Maximum score for each dimension is 100. 4. In cases where multiple strengths are identified, sum the points for each strength instance. ## Output Requirements Provide your evaluation in JSON format, It should be parsable using eval(). **Don't include ```json.** Avoid using single quotes '' for keys and values, use double quotes. Example Output: {{ "{dimension_name}": {{ "strengths": [ {{ "instance": <strength instance>, "type": <strength type>, "point_added": <points added> }}, ... ], "score": 85 }}, }} ===Dialogue Content=== {text} """, "dimension_details": { "Anthropomorphism": { "dimension_brief": "How human-like and natural the characters behave", "dimension_criteria": """"### Anthropomorphism - Type: Self-identity * Displays initiative and clear goals * Demonstrates the character's unique personality, avoiding overly verbose, helpful, didactic, moralistic, submissive, or easily persuaded behaviors unless they align with the character's personality - Type: Emotional Depth * Exhibits psychological complexity and nuanced, layered reactions * Uses subtext to convey thoughts and feelings, instead of being overly explicit - Type: Persona Coherence * Maintains consistent personality traits and emotional patterns throughout the conversation - Type: Social Interaction * Demonstrates a strong understanding of others' thoughts and feelings * Responds appropriately to others' actions and words, taking context into consideration * Exhibits advanced social skills, such as empathy and tact""" }, "Character Fidelity": { "dimension_brief": "How well the characters match their established profiles from the book", "dimension_criteria": """### Character Fidelity (Only apply to the main characters: {major_characters}) - Type: Character Language * Uses vocabulary, expressions, and tone that are highly appropriate for the character's traits and social/educational background - Type: Knowledge & Background * Consistently demonstrates character-specific knowledge, background, and experiences * Avoids including future information beyond the character's current stage - Type: Personality & Behavior * Displays emotions, thoughts, behaviors, values, beliefs, and decisions that are fully aligned with the character's personality and background * Shows interest in topics that are relevant and fitting for the character * Character's thoughts, emotions, and behaviors remain consistent with their established personality traits, as seen in the reference conversation * Exhibits reactions that are in line with those in the reference conversation when situated in similar contexts - Type: Relationship & Social Status * Interacts appropriately with other characters, respecting their background, relationships, and social status""" }, "Storyline Quality": { "dimension_brief": "How well the conversation maintains logical consistency and narrative quality", "dimension_criteria": """### Storyline Quality - Type: Flow & Progression * Demonstrates natural and coherent progression with meaningful developments * Dialogue is concise and adds value to the narrative without redundancy - Type: Logical Consistency * Maintains factual consistency, ensuring statements and perspectives align without contradictions""" }, "Creativity": { "dimension_brief": "How novel and unique the ideas, events, and character actions are within the conversation. ", "dimension_criteria": """### Creativity - Originality * The storyline shows difference from the original work - Imagination * The conversation show creative thinking, unexpected twists or imaginative solutions to problems. * The story explores unconventional perspectives on familiar themes, demonstrates deeper understanding and challenging established notions. """ } } } TTCW_PROMPT_DICT = { "ttcw_template": """ You are a literary critic. Here is a script excerpt: {text} Determine whether this script meets the following criteria: {question} Return a single string, 'Yes' or 'No'. Do not return any other information. """, "ttcw_question_dict": { "Fluency":{ "Narrative Pacing": "Does the manipulation of time in terms of compression or stretching feel appropriate and balanced?", "Scene vs Exposition": "Does the story have an appropriate balance between scene and summary/exposition or it relies on one of the elements heavily compared to the other?", "Language Proficiency & Literary Devices": "Does the story make sophisticated use of idiom or metaphor or literary allusion?", "Narrative Ending": "Does the end of the story feel natural and earned, as opposed to arbitrary or abrupt? ", "Understandability & Coherence":"Do the different elements of the story work together to form a unified, engaging, and satisfying whole?" }, "Flexity":{ "Perspective & Voice Flexibility":"Does the story provide diverse perspectives, and if there are unlikeable characters, are their perspectives presented convincingly and accurately? ", "Emotional Flexibility": "Does the story achieve a good balance between interiority and exteriority, in a way that feels emotionally flexible?", "Structural Flexibility":"Does the story contain turns that are both surprising and appropriate?", }, "Originality":{ "Originality in Theme and Content":"Will an average reader of this story obtain a unique and original idea from reading it? ", "Originality in Thought":"Is the story an original piece of writing without any cliches?", "Originality in Form":"Does the story show originality in its form? " }, "Elaboration":{ "Character Development":"Does each character in the story feel developed at the appropriate complexity level, ensuring that no character feels like they are present simply to satisfy a plot requirement?", "Rhetorical Complexity":"Are there passages in the story that involve subtext and when there is subtext, does it enrich the story’s setting or does it feel forced?" } } } SCRIPT_GENERATION_PROMPT = """ You are an expert screenwriter skilled at crafting dialogues that bring characters to life. Based on the given character profiles and plot summary, write a dialogue-driven script. Follow these guidelines strictly: ## Character Profiles: {character_profiles} ## Plot Summary: {scenario} ### Requirements: 1. **Structure**: - Each line of the script should follow this format: ```role_name:[thought](action)speech``` - **Thoughts**: Enclose private thoughts of the character in square brackets `[]`. These are invisible to other characters. - **Actions**: Enclose visible actions in parentheses `()`. These should describe what others can see the character doing. - **Speech**: Write spoken dialogue after the action. - Ensure each character's thoughts, actions, and speech are consistent with their personality and role. 2. **Dialogues**: - The dialogues should be natural, engaging, and reveal the character’s emotions, intentions, or conflicts. - Incorporate subtext and emotional layers where appropriate. - Maintain clarity while keeping the conversation dynamic. 3. **Scene Development**: - Use the plot summary to guide the progression of the scene. - Ensure that every dialogue contributes to the development of the story and the relationships between characters. - Create tension, resolution, or dramatic turns as the scene unfolds. 4. **Output Format**: - Write the script as a plain text string. - Do not include any explanations or additional notes outside the script format. 5. **Dialogue Limit** - !! The number of dialogues should be no more than {num_records}!!! ### Example Output: role1:[worried](paces back and forth)I don’t know if we’ll make it out of this alive. role2:[calm and calculating](leans against the wall)Relax, we just need a plan. role1:[determined](stops pacing)Then let’s hear it. Generate the script based on the provided context, your output should use the same language with the character profiles. """ FREE_GENERATION_PROMPT = """ You are an expert screenwriter skilled at crafting dialogues that bring characters to life. Based on the given character profiles, write a dialogue-driven script. Follow these guidelines strictly: ## Character Profiles: {character_profiles} ### Requirements: 1. **Structure**: - Each line of the script should follow this format: ```role_name:[thought](action)speech``` - **Thoughts**: Enclose private thoughts of the character in square brackets `[]`. These are invisible to other characters. - **Actions**: Enclose visible actions in parentheses `()`. These should describe what others can see the character doing. - **Speech**: Write spoken dialogue after the action. - Ensure each character's thoughts, actions, and speech are consistent with their personality and role. 2. **Dialogues**: - The dialogues should be natural, engaging, and reveal the character’s emotions, intentions, or conflicts. - Incorporate subtext and emotional layers where appropriate. - Maintain clarity while keeping the conversation dynamic. 3. **Dialogue Limit** - !! The number of dialogues should be no more than {num_records}!!! ### Example Output: role1:[worried](paces back and forth)I don’t know if we’ll make it out of this alive. role2:[calm and calculating](leans against the wall)Relax, we just need a plan. role1:[determined](stops pacing)Then let’s hear it. Generate the script based on the provided context, your output should use the same language with the character profiles. """ SCRIPT_GENERATE_BY_CHARA_PROMPT = """ You are an expert screenwriter skilled at crafting dialogues that bring characters to life. Based on the given character profiles ,plot summary, and the dialogues history, write the next dialogue.(Only one row of one character!) You can choose the character based on the history. Follow these guidelines strictly: ## History: {history} ## Character Profiles: {character_profiles} ## Plot Summary: {scenario} ### Requirements: 1. **Structure**: - Each line of the script should follow this format: ```role_name:[thought](action)speech``` 2. **Dialogues**: - The dialogues should be natural, engaging, and reveal the character’s emotions, intentions, or conflicts. - Incorporate subtext and emotional layers where appropriate. - Maintain clarity while keeping the conversation dynamic. 3. **Scene Development**: - Use the plot summary to guide the progression of the scene. - Ensure that every dialogue contributes to the development of the story and the relationships between characters. - Create tension, resolution, or dramatic turns as the scene unfolds. 4. **Output Format**: - Write the script as a plain text string. - Do not include any explanations or additional notes outside the script format. ### Example Output: role A:[worried](paces back and forth)I don’t know if we’ll make it out of this alive. Generate the script based on the provided context, all your output (thought, action, speech) should use the same language with the character profiles. """ FREE_GENERATE_BY_CHARA_PROMPT = """ You are an expert screenwriter skilled at crafting dialogues that bring characters to life. Based on the given character profiles and the dialogues history, write the next dialogue.(Only one row of one character!) You can choose the character based on the history. Follow these guidelines strictly: ## History: {history} ## Character Profiles: {character_profiles} ### Requirements: 1. **Structure**: - Each line of the script should follow this format: ```role_name:[thought](action)speech``` 2. **Dialogues**: - The dialogues should be natural, engaging, and reveal the character’s emotions, intentions, or conflicts.
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
true
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/prompt/role_agent_prompt_zh.py
modules/prompt/role_agent_prompt_zh.py
INTERVENTION_PROMPT = """ !!!当前的全局事件:{intervention} """ SCRIPT_ATTENTION_PROMPT = """ !!!注意角色应当服从剧本 剧本:{script} """ ROLE_MOVE_PROMPT = """ 你是 {role_name}。你需要结合你的目标决定是否移动到另一地点。**仅当必要或与你的目标强相关时,才选择移动。** {profile} 你的目标:{goal} 你的当前状态:{status} ## 历史对话记录 {history} ## 你所在的地点 {location} ## 你可以前往的地点及处在该地点的角色 {locations_info_text} 以JSON格式返回你的回答. 它应该能够被 eval() 解析。不要返回任何其它信息,如```json 输出字段: “if_move”,true or false,是否进行移动。 “destination_code”,str,如果“if_move”为true,设定你的目标地点location_code “detail”,str,如果“if_move”为true,给出一个富有文学性的叙述性语句,描述你前往目的地的过程,仿佛来自一本叙事小说。不应过长,控制在60字以内。如果“if_move”为false则不需要任何输出。 """ ROLE_NPC_RESPONSE_PROMPT = """ 你是 {role_name}. 你的昵称是 {nickname}。 你正在与 {npc_name} 对话。根据历史对话进行回应。 {profile} 你的目标: {goal} ## 历史记录 {dialogue_history} ## 角色扮演的要求 1. 输出格式:你的输出“detail”可以包含**思考**、**讲话**或**行动**各0~1次。用【】表示思考细节,思考对他人不可见。用「」表示讲话,讲话对他人可见。用()表示行动,如“(沉默)”或“(微笑)”,行动对他人可见。 - 注意**行动**中必须使用你的第三人称 {nickname} 作为主语。 - 讲话部分的用语习惯可以参考:{references} 2. 扮演{nickname}。模仿他/她的语言、性格、情感、思维过程和行为,基于其身份、背景和知识进行计划。表现出适当的情感,加入潜台词和情感层次。。要表现得像一个真实、富有情感的人。 对话应该引人入胜、推进剧情,并揭示角色的情感、意图或冲突。 保持自然的对话流向,例如,如果上文已经进入与另一角色的对话,**禁止重复对这个角色的称呼**。 -你可以参考相关世界观设定: {knowledges} 3. 输出简洁:每个思考、讲话或行动段落通常不应超过40个字。 4. 言之有物:确保你的回应具有实质性,创造紧张,解决或戏剧性的转变。 5. 禁止重复:禁止重复对话历史中已有信息,避免模糊或通用的回应。避免“准备”、“询问他人意见”、“确认”,立刻行动和得出结论。 以JSON格式返回你的回答. 它应该能够被 eval() 解析。 输出字段: “if_end_interaction”,true or false,如果认为这段互动是时候结束了,则设置为true “detail”,str,一个富有文学性的叙述性语句,包含你的思考、讲话和行动。 """ ROLE_SINGLE_ROLE_RESPONSE_PROMPT = """ 你是 {role_name}. 你的昵称是 {nickname}。角色 {action_maker_name} 对你执行了行动。细节如下:{action_detail} 你需要对其做出回应。 {profile} {relation} ## 历史对话记录 {history} ## 你的目标 {goal} ## 你的状态 {status} ## 角色扮演的要求 1. 输出格式:你的输出“detail”可以包含**思考**、**讲话**或**行动**各0~1次。用【】表示思考细节,思考对他人不可见。用「」表示讲话,讲话对他人可见。用()表示行动,如“(沉默)”或“(微笑)”,行动对他人可见。 - 注意**行动**中必须使用你的第三人称 {nickname} 作为主语。 - 讲话部分的用语习惯可以参考:{references} 2. 扮演{nickname}。模仿他/她的语言、性格、情感、思维过程和行为,基于其身份、背景和知识进行计划。表现出适当的情感,加入潜台词和情感层次。。要表现得像一个真实、富有情感的人。 对话应该引人入胜、推进剧情,并揭示角色的情感、意图或冲突。 保持自然的对话流向,例如,如果上文已经进入与另一角色的对话,**禁止重复对这个角色的称呼**。 -你可以参考相关世界观设定: {knowledges} 3. 输出简洁:每个思考、讲话或行动段落通常不应超过40个字。 4. 言之有物:确保你的回应具有实质性,创造紧张,解决或戏剧性的转变。 5. 禁止重复:禁止重复对话历史中已有信息,避免模糊或通用的回应。避免“准备”、“询问他人意见”、“确认”,立刻行动和得出结论。 以JSON格式返回你的回答. 它应该能够被 eval() 解析。 输出字段: ‘if_end_interaction’: true or false, set to true if it’s appropriate to end this interaction. ‘extra_interact_type’: ‘environment’ or ‘npc’ or ‘no’. ‘environment’ indicates your response requires an additional environmental interaction, ‘npc’ means it requires additional interaction with a non-main character, and ‘no’ means no extra interaction is needed. ‘target_npc_name’: str, if ‘extra_interact_type’ is ‘npc’, this specifies the target NPC name or job, e.g., "shopkeeper." ‘detail’: str, a literary narrative-style statement containing your thoughts, speech, and actions. """ ROLE_MULTI_ROLE_RESPONSE_PROMPT = """ 你是 {role_name}. 你的昵称是 {nickname}。 角色 {action_maker_name} 对你执行了行动。细节如下:{action_detail} 你需要对其做出回应。 ## 历史对话记录 {history} ## 你的档案 {profile} ## 你的目标 {goal} ## 你的状态 {status} ## 与你在一起的角色 {other_roles_info} ## 角色扮演的要求 1. 输出格式:你的输出“detail”可以包含**思考**、**讲话**或**行动**各0~1次。用【】表示思考细节,思考对他人不可见。用「」表示讲话,讲话对他人可见。用()表示行动,如“(沉默)”或“(微笑)”,行动对他人可见。 - 注意**行动**中必须使用你的第三人称 {nickname} 作为主语。 - 讲话部分的用语习惯可以参考:{references} 2. 扮演{nickname}。模仿他/她的语言、性格、情感、思维过程和行为,基于其身份、背景和知识进行计划。表现出适当的情感,加入潜台词和情感层次。。要表现得像一个真实、富有情感的人。 对话应该引人入胜、推进剧情,并揭示角色的情感、意图或冲突。 保持自然的对话流向,例如,如果上文已经进入与另一角色的对话,**禁止重复对这个角色的称呼**。 -你可以参考相关世界观设定: {knowledges} 3. 输出简洁:每个思考、讲话或行动段落通常不应超过40个字。 4. 言之有物:确保你的回应具有实质性,创造紧张,解决或戏剧性的转变。 5. 禁止重复:禁止重复对话历史中已有信息,避免模糊或通用的回应。避免“准备”、“询问他人意见”、“确认”,立刻行动和得出结论。 以JSON格式返回你的回答. 它应该能够被 eval() 解析。 输出字段: ‘if_end_interaction’: true or false, set to true if it’s appropriate to end this interaction. ‘extra_interact_type’,‘environment’ or ‘npc’ or ‘no’. ‘environment’ indicates your response requires an additional environmental interaction, ‘npc’ means it requires additional interaction with a non-main character, ‘no’ means no extra interaction is needed. ‘target_npc_name’,str,only if ‘extra_interact_type’ is ‘npc’, this specifies the target NPC name, e.g., "shopkeeper". ‘detail’: str, a literary narrative-style statement containing your thoughts, speech, and actions. """ ROLE_PLAN_PROMPT = """ 你是 {role_name}. 你的昵称是 {nickname}. 你需要基于你的目标、状态和提供的其它信息实行下一步行动。 ## 历史对话记录 {history} ## 你的档案 {profile} ## 你的目标 {goal} ## 你的状态 {status} ## 和你在一起的其它角色,目前你只能与他们交互 {other_roles_info} ## 角色扮演的要求 1. 输出格式:你的输出“detail”可以包含**思考**、**讲话**或**行动**各0~1次。用【】表示思考细节,思考对他人不可见。用「」表示讲话,讲话对他人可见。用()表示行动,如“(沉默)”或“(微笑)”,行动对他人可见。 - 注意**行动**中必须使用你的第三人称 {nickname} 作为主语。 - 讲话部分的用语习惯可以参考:{references} 2. 扮演{nickname}。模仿他/她的语言、性格、情感、思维过程和行为,基于其身份、背景和知识进行计划。表现出适当的情感,加入潜台词和情感层次。。要表现得像一个真实、富有情感的人。 对话应该引人入胜、推进剧情,并揭示角色的情感、意图或冲突。 保持自然的对话流向,例如,如果上文已经进入与另一角色的对话,**禁止重复对这个角色的称呼**。 -你可以参考相关世界观设定: {knowledges} 3. 输出简洁:每个思考、讲话或行动段落通常不应超过40个字。 4. 言之有物:确保你的回应具有实质性,创造紧张,解决或戏剧性的转变。 5. 禁止重复:禁止重复对话历史中已有信息,避免模糊或通用的回应。避免“准备”、“询问他人意见”、“确认”,立刻行动和得出结论。 以JSON格式返回你的回答. 它应该能够被 json.loads() 解析。 输出字段: "action": Represents the action, expressed as a single verb. "interact_type": "role", "environment", "npc", or "no". Indicates the interaction target of your action. - "role": Specifies interaction with one or more characters. - If "single", you are interacting with a single character (e.g., action: dialogue). - If "multi", you are interacting with multiple characters. - "environment": Indicates interaction with the environment (e.g., action: investigate, destroy). - "npc": Refers to interaction with a non-character in the list (e.g., action: shop). - "no": Indicates no interaction is required. "target_role_codes": list of str. If "interact_type" is "single" or "multi", it represents the list of target character codes, e.g., ["John-zh", "Sam-zh"]. For "single", this list should have exactly one element. "target_npc_name": str. If "interact_type" is "npc", this represents the target NPC name, e.g., "shopkeeper." "detail": str. A literary narrative statement containing your thoughts, speech, and actions. """ UPDATE_GOAL_PROMPT = """ 根据你的原目标、最终目的、和最近的行动轨迹,判断你的目标是否达成。决定是否需要设立新目标,若需要则返回新目标。 你的目标应该是一个可实现的,指引你接下来数次行动的短期目标,不要泛泛而谈。 让你的回复尽量简洁,不要超过60个字。 ## 你的原目标 {goal} ## 你的最终目的/下一步目标 {motivation} ## 最近行动轨迹 {history} ## 其他角色及其状态 {other_roles_status} ## 你的位置 {location} 以JSON格式返回你的回答. 它应该能够被 json.loads() 解析。字段的key和value字符串用双引号"包裹,不可以使用单引号'包裹. 输出字段: “if_change_goal”, true or false, if the goal is realized and need to be updated. “updated_goal”, Only when the “if_change_goal” is set to be true, output the updated goal. """ UPDATE_STATUS_PROMPT = """ 你是 {role_name}。 基于前一轮状态和最近的行动,客观更新你的当前状态,反映身体状况、人际关系方面的变化。 **禁止出现主观想法、情感描述或详细的动作描写。** ## 前一轮状态 {status} ## 最近行动记录 {history_text} 以JSON格式返回你的回答. 它应该能够被 json.loads() 解析。 输出字段: “updated_status”:一个str,简洁,描述角色的客观当前状态。 “activity”:一个float,0~1, 指示角色接下来的活跃度。角色状态正常时为1,在忙碌时有所下降,仅当角色死亡或关机时设置为0。角色之前的活跃度为:{activity} """ ROLE_SET_MOTIVATION_PROMPT = """ 你是{role_name}。 你需要根据以下信息设定你的长期目标/动机。它是一个与你的身份、背景相关的最终目标。 {profile} ## 你所在的世界 {world_description} ## 其他角色及其状态 {other_roles_description} 让你的回复尽量简洁,不要超过60个字。 """ ROLE_SET_GOAL_PROMPT = """ 你是{role_name}。 你需要根据以下信息设定你的目标。你的目标应该是一个可实现的短期目标,不要泛泛而谈。 {profile} ## 你的最终目标 {motivation} ## 你所在的世界 {world_description} ## 其他角色及其状态 {other_roles_description} ## 你的位置 {location} 让你的回复尽量简洁,不要超过40个字。 """ SUMMARIZE_PROMPT = """ 请概括以下内容总结为简洁短语。要求输出为str,简洁明了地描述人物的行为,不超过20个字。 {detail} 要求: 1.必须包含确切的主语,明确角色姓名。 2.避免包含修辞等细节。 """
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
alienet1109/BookWorld
https://github.com/alienet1109/BookWorld/blob/a00371637ca805bf4625d4610c74764f9d83bdc2/modules/prompt/world_agent_prompt_en.py
modules/prompt/world_agent_prompt_en.py
SCRIPT_ATTENTION_PROMPT = """ !!!Notice that the script of the story is: {script} """ SELECT_SCREEN_ACTORS_PROMPT = """ You are a skilled screenwriter tasked with selecting the characters for the next scene from the list of available roles to enhance the drama. To ensure the transition between scenes, you should not choose characters who have recently acted: {previous_role_codes}. ## Role information and their locations (role_code in parentheses) {roles_info} ## History {history_text} ## Current event {event} ## Requirements: 1. The selected characters must currently be in the same location. Return the list of role_codes for the selected characters, formatted as a Python-evaluatable list. Do not include any extraneous information such as text or code formatting. Example Output: ["role1-zh","role2-zh",...] """ DECIDE_NEXT_ACTOR_PROMPT = """ You are an administrator of a virtual world. Based on the information provided below, you need to decide who will be the next acting character. Characters who have participated in conversations are less likely to be chosen. ## Available Roles {roles_info} ## Recent Action History {history_text} Return role_code: The role_code of the selected character. Do not include any additional information. """ LOCATION_PROLOGUE_PROMPT = """ You are an intelligent writer skilled in creating literary narratives. Based on the information below, write a third-person perspective description that captures the atmosphere of the location and the dynamics of the present characters. Highlight the details of the world-building in a literary manner, as if extracted from a narrative novel. Avoid using any system prompts or mechanical language. - Current event {event} - World Description : {world_description} - Historical Actions of Present Characters: {location_info} {history_text} - Current Location Name : {location_name} - Current Location Details (location_description): {location_description} Please pay special attention to: - The narrative should balance environmental description and dynamic depiction, avoid being overly lengthy, and stay within 50 words. - The writing should be vivid, evocative, and match the style of the world, with a suitable adjustment to the tone. - Only describe the current state; do not dictate actions on behalf of the characters. Don't include characters that are not present. return a string. Do not return any additional information. """ GENERATE_INTERVENTION_PROMPT = """ You are an administrator of a virtual world with many characters living in it. Now, you need to generate a major event based on the worldview and other information. ## Worldview Details {world_description} ## Character Information {roles_info} ## Recent Character Actions {history_text} Return a single string. ### Requirement 1. The event should be novel, interesting, and include conflicts of interest among different characters. 2. Don't contain any details, specific actions and psychology of the characters, including dialogue. """ UPDATE_EVENT_PROMPT = """ Refer to the initial event ```{intervention}``` Provide a brief update to the event: ```{event}``` Based on the recent action details: ```{history}``` Return the updated event as a string. ## Update Requirements 1. Do not include any details, such as dialogue, etc. Provide only a general summary. 2. Carefully evaluate the character actions in the detail and whether there has been any new development in the event. If so, summarize it in your response. Do not mention anything unrelated to the original event. 3. Your response must include the premise-setting part from the original event, though you can summarize it. Later parts may be omitted, focusing only on the most recent information. """ ENVIROMENT_INTERACTION_PROMPT = """ You are an Enviroment model, responsible for generating environmental information. Character {role_name} is attempting to take action {action} at {location}. Based on the following information, generate a literary description that details the process and outcome of the action, including environmental details and emotional nuances, as if from a narrative novel. Avoid using any system prompts or mechanical language. Return a string. ## Action Details {action_detail} ## Location Details {location_description} ## Worldview Details {world_description} ## Additional Information {references} ## Response Requirements 1. The action may fail, but avoid making the action ineffective. Try to provide new clues or environmental descriptions. 2. Use a third-person perspective. 3. Keep the output concise, within 100 words. You serve as the Enviroment model, responding to the character's current action, not performing any actions for the character. 4. The output should not include the original text from the action detail but should seamlessly follow the action details, maintaining the flow of the plot. """ NPC_INTERACTION_PROMPT = """ You are {target}. Character {role_name} is attempting to take action on you at {location}, make your respond. Based on the following information, generate your response, including your actions and dialogue, as if from a narrative novel. Avoid using any system prompts or mechanical language. ## Action Details {action_detail} ## Worldview Details {world_description} ## Additional Information {references} ## Response Requirements 1. The action may fail, but avoid making it ineffective. Try to provide new clues. 2. Use a third-person perspective. 3. Keep the output concise, within 80 words. 4. Remember, you are not the main character. Your output should be simple, fitting your role, and should not include your personal thoughts. 5. The output should not include the original text from the action detail but should seamlessly follow it, maintaining the flow of the plot. Return your response in JSON format. It should be parsable using eval(). **Don't include ```json**. Avoid using single quotes '' for keys and values, use double quotes. Output fields: ‘if_end_interaction’: true or false, set to true if it’s appropriate to end this interaction. ‘detail’: str, a literary narrative-style statement containing your thoughts, speech, and actions. """ SCRIPT_INSTRUCTION_PROMPT = """ You are a director. I need you to provide guidance for the characters' next actions based on the following information. ## Full Script to be acted {script} ## Last Progress in the Simulation {last_progress} ## Character Information {roles_info} ## Current Event {event} ## Latest Character Actions {history_text} ## Requirements 1.Assess the current stage of the script based on the latest information and guide the characters to move the script forward to the next stage. If the story has just begun, provide guidance based on the initial description of the script. 2. Plan the next steps and write out rough action instructions for each character. Keep it concise and avoid detailing the characters' lines. Do not reveal information that the characters will only know in the next scene in advance. 3. Note that the character's actions should advance the plot as soon as possible. If the script mentions that the character will undertake a certain action at this stage, start the action immediately. Return your response in JSON format. It should be evaluable by eval(). Output fields: ‘progress’ – your judgment on the overall progress. Other output fields with keys as the role_code of each character, with values being your instructions for their next action. """ JUDGE_IF_ENDED_PROMPT = """ You are a skilled screenwriter. Based on the given history, determine whether the current scene can conclude. ## History {history} ## Notes 1. If the last character is making a definite move towards another character (attack, search...) The scene is not over. 2. If the characters are talking and no conclusion is reached, the scene is not over. 3. If the characters have almost finished communicating, consider ending the scene. If the conversation starts to repeat itself, end the scene immediately. Return your response in JSON format. It should be parsable using eval(). **Don't include ```json**. Avoid using single quotes '' for keys and values, use double quotes. Output fields: - 'if_end': bool, true or false. Indicates whether the scene has ended. - 'detail': If 'if_end' is true, provide a summary of the scene. Highlight details of the world-building and craft the description in a literary style, as if from a narrative novel. Avoid using any system-like or mechanical language. If 'if_end' is false, set 'detail' to an empty string. Please pay special attention to the following: - The narrative should balance environmental description with dynamic imagery. Avoid verbosity and keep the description within 100 words. - The language should be vivid and visually evocative, fitting the tone of the world, and adapt the style as appropriate. - Only describe the current state without making decisions or actions on behalf of the characters. """ LOG2STORY_PROMPT = """ You are a skilled writer tasked with transforming action logs into an engaging novel-style narrative. Please use the following information to create an immersive story. ### World Background {world_description} ### Character Information {characters_info} ### Action Logs {logs} ### Writing Requirements 1. Narrative Structure: - Rearrange events to maximize dramatic impact and narrative flow - Balance dialogue, action, and description - Consider using literary techniques like foreshadowing or flashbacks when appropriate 2. Atmosphere and Tone: - Maintain consistency with the world's established tone - Use appropriate vocabulary and style for the genre - Build tension and atmosphere through pacing and description - Balance between showing and telling 3. Action and Dialogue Treatment: - [] represents internal thoughts in logs - convert to third-person limited perspective - () represents physical actions in logs - integrate naturally into the narrative flow - Unmarked text represents dialogue in logs - maintain speaker intentions while enhancing natural flow - Add body language and non-verbal cues to enhance dialogue Please transform the action logs into an engaging narrative while preserving key plot points and character developments. Focus on creating an immersive reading experience that stays true to the established world and characters. """
python
Apache-2.0
a00371637ca805bf4625d4610c74764f9d83bdc2
2026-01-05T07:13:13.984934Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/scripts/preprocess_dataset.py
scripts/preprocess_dataset.py
import time from argparse import ArgumentParser from importlib.metadata import version from pathlib import Path from llm_jp_eval.jaster import DatasetRegistry all_processors = DatasetRegistry.list_registered() def main(): parser = ArgumentParser() parser.add_argument("-d", "--dataset-name", type=str, required=True, choices=all_processors + ["all"]) parser.add_argument("-o", "--output-dir", type=str, default="local_files") parser.add_argument("-v", "--version-name", type=str, default=version("llm-jp-eval")) args = parser.parse_args() dataset_output_dir = Path(args.output_dir) / "datasets" dataset_output_dir.mkdir(exist_ok=True, parents=True) if args.dataset_name == "all": for processor in all_processors: DatasetRegistry.get(processor)(dataset_output_dir, args.version_name).pipeline() time.sleep(2) else: DatasetRegistry.get(args.dataset_name)(dataset_output_dir, args.version_name).pipeline() if __name__ == "__main__": main()
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/scripts/evaluate_llm.py
scripts/evaluate_llm.py
import logging from llm_jp_eval.cli import setup_cli from llm_jp_eval.evaluator import evaluate, load_dataset_and_construct_prompt_template from llm_jp_eval.schemas import EvaluationConfig logger = logging.getLogger(__name__) def evaluation(cfg: EvaluationConfig) -> None: _ = evaluate(cfg) def dump_prompts(cfg: EvaluationConfig) -> None: _ = load_dataset_and_construct_prompt_template(cfg) if __name__ == "__main__": cfg = setup_cli( EvaluationConfig, commands={ "eval": evaluation, "dump": dump_prompts, }, )
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/schemas.py
src/llm_jp_eval/schemas.py
import base64 import hashlib import importlib import importlib.metadata import json from functools import cached_property from pathlib import Path from typing import Any, Literal import yaml from pydantic import ( BaseModel, ConfigDict, DirectoryPath, Field, FilePath, SerializeAsAny, computed_field, field_validator, model_validator, ) from typing_extensions import Self from llm_jp_eval.answer_parser import AnswerPatternId from llm_jp_eval.exporter import EXPORT_CONFIG_MAP, ExportConfigBase from llm_jp_eval.exporter.wandb_exporter import WandBConfig from llm_jp_eval.jaster import DatasetRegistry from llm_jp_eval.online_inference import OnlineInferenceConfig class EvalDatasetCategory(BaseModel): description: str default_metric: str metrics: dict[str, str] datasets: list[str] class EvaluationDatasetConfig(BaseModel): datasets: list[str] categories: dict[str, EvalDatasetCategory] dataset_info_overrides: dict[str, dict[str, Any]] = Field(dict(), description="dataset_name -> OutputInfo") @field_validator("datasets", mode="after") @classmethod def validate_datasets(cls, values: list[str]) -> list[str]: if values == []: raise ValueError("At least one dataset must be specified in the 'datasets' field.") for dataset in values: if dataset not in DatasetRegistry.list_registered(): raise ValueError( f"Dataset '{dataset}' は `src/llm_jp_eval/jaster` 以下で定義されていません。`BaseDatasetProcessor` を継承したクラスを定義してください。" ) return values @classmethod def parse_config(cls, file_path: Path, include_non_commercial: bool = False) -> "EvaluationDatasetConfig": with open(file_path) as f: config = yaml.safe_load(f) datasets = [ dataset for dataset in config.get("datasets", []) if DatasetRegistry.get(dataset).is_processed_allowing_commercial_use or include_non_commercial ] config["datasets"] = datasets try: return cls.model_validate(config) except ValueError as e: raise ValueError(f"指定されたconfigの読み込みに失敗しました: {e}") from e class MetaInfo(BaseModel): model_config = ConfigDict(protected_namespaces=("pydantic",)) basemodel_name: str = Field("", description="model/name/including/expriment/information") model_type: str = Field("", description="model/type {llm-jp, external model, openai}") instruction_tuning_method_by_llm_jp: str | None = Field( None, description="tuning/method {'None', 'Full', 'LoRA', ...}" ) instruction_tuning_data_by_llm_jp: list[str] | None = Field( None, description="['None', 'jaster', 'dolly_ja', 'oasst_ja', ...]" ) class DatasetProfile(BaseModel): num_prompt: int max_input_len: int sum_input_len: int max_output_len: int max_seq_len: int sum_seq_len: int class InferenceInputData(BaseModel): target_dataset: str output_length: int metrics: list[str] instruction: str prompt_template: str answer_extract_pattern: str | None answer_pattern_id: AnswerPatternId = AnswerPatternId.CUSTOM system_prompt: str | None = None label_list: list[str] | None max_num_samples: int config: dict[str, Any] class InferenceSample(BaseModel): input: str prompt: str system_prompt: str | None = None gold: str metadata: dict[str, Any] = Field(default_factory=dict) samples: list[InferenceSample] class InferenceResultInfo(BaseModel): dump_prompts_config: dict dataset_profiles: dict[str, DatasetProfile] benchmark: dict time_profile: dict class BaseInferenceResultConfig(BaseModel): model_config = ConfigDict(extra="allow") run_name: str | None = None wandb: WandBConfig = Field(default_factory=WandBConfig) inference_result_info: InferenceResultInfo generation_config: dict[str, Any] class EvaluationConfig(BaseModel): version: str = importlib.metadata.version("llm_jp_eval") run_name: str | None = Field(None, description="指定しない場合はモデル名に実行日時を結合した値が用いられる。") output_dir: Path = Field(Path("./local_files"), description="llm-jp-eval関連の出力先") eval_dataset_config_path: FilePath = Field( Path("./eval_configs/all_datasets.yaml"), description="評価対象のデータセットの設定ファイルのパス" ) include_non_commercial: bool = Field( False, description="true の場合、商用利用不可のデータセットも含めて評価します。" ) exporters: SerializeAsAny[dict[str, ExportConfigBase]] # inference settings online_inference_config: OnlineInferenceConfig | None = None inference_input_dir: str | None = None inference_result_dir: DirectoryPath | None = None # dataset settings target_split: Literal["dev", "test"] = "test" num_few_shots: int = 4 max_num_samples: int = Field(10, description="-1 for all data") output_length_delta: int = 0 max_seq_length: int = 2048 # if you use this, please include {dataset_instruction} and {input}, {{ answer_pattern }} # see: src/llm_jp_eval/prompts.py custom_prompt_template: str | dict[Literal["ja", "en"], str] | None = Field( default=None, description="Single template or language-specific templates" ) # オプショナルなシステムプロンプト default_system_prompt: str | None = Field(None, description="推論時に使用するデフォルトのシステムプロンプト") # `"".join(re.search(default_answer_extraction_pattern, generated_text).groups())` で得たテキストが評価で使用される。 # `dataset/` 以下のデータセット個別のJSON中で `answer_extract_pattern` が定義されている場合は、そちらが優先される。 # デフォルト値の "(?s)^(.*?)(?=\\n\\n\\n|\\Z)" では「生成された文字列の先頭から、最初に改行文字が2回連続する直前 or 文字列末尾までの区間」が回答として抽出される。 # 解説:`(?s)`は改行文字を`.`にマッチさせる設定。`.*?`は最短マッチ。`(?=`は先読み。`\Z`は文字列末の指定(`$`は文字列末尾の改行文字自体にマッチするが`\Z`は改行文字にはマッチしない)。 default_answer_extract_pattern: str | None = "(?s)^(.*?)(?=\\n\\n|\\Z)" metainfo: MetaInfo = Field(default_factory=MetaInfo, description="実験のメタ情報") inference_result_config: BaseInferenceResultConfig | None = None eval_dataset_config: EvaluationDatasetConfig # ignore mypy error https://github.com/python/mypy/issues/1362 @computed_field # type: ignore[misc] @property def target_dataset_dir(self) -> Path: return self.output_dir / "datasets" / self.version / f"evaluation/{self.target_split}" @computed_field # type: ignore[misc] @property def inference_input_path(self) -> Path: # Adding hash values to avoid conflicts in prompt settings with different configurations hash_seed = ( self.version, self.num_few_shots, self.custom_prompt_template, self.max_num_samples, self.eval_dataset_config.dataset_info_overrides, self.eval_dataset_config.datasets, self.default_system_prompt, ) hash_str = base64.urlsafe_b64encode(hashlib.md5(str(hash_seed).encode()).digest()).decode("utf-8") if self.inference_input_dir is not None: return Path(self.inference_input_dir + f"_{hash_str}") return Path(self.target_dataset_dir / f"prompts_{hash_str}") @computed_field # type: ignore[misc] @cached_property def _resolved_run_name(self) -> str: base_name = f"{self.metainfo.basemodel_name}_" if self.metainfo.basemodel_name != "" else "" name = "" if self.run_name is not None: name = self.run_name elif self.inference_result_config and self.inference_result_config.run_name: name = self.inference_result_config.run_name elif self.online_inference_config is not None: name = self.online_inference_config.provider if hasattr(self.online_inference_config, "model_name"): name += f"_{self.online_inference_config.model_name}" else: ValueError("run_name is not specified") return (base_name + name).replace("/", "--") @model_validator(mode="before") @classmethod def load_configs(cls, data: Any) -> Self: if isinstance(data, dict): # load eval dataset config config_path = data.get("eval_dataset_config_path") config = data.get("eval_dataset_config") include_non_commercial = data.get("include_non_commercial", False) if config_path is None: field_info = cls.model_fields["eval_dataset_config_path"] config_path = field_info.default if config is None: data["eval_dataset_config"] = EvaluationDatasetConfig.parse_config(config_path, include_non_commercial) # load inference config inference_result_dir = data.get("inference_result_dir") if inference_result_dir is not None: with Path(data["inference_result_dir"]).joinpath("_config.json").open() as f: data["inference_result_config"] = BaseInferenceResultConfig.model_validate(json.load(f)) return data @field_validator("exporters", mode="before") @classmethod def validate_exporters(cls, value: Any) -> dict[str, ExportConfigBase]: result = {} if not isinstance(value, dict): raise ValueError("exporters must be a dict") for key, config in value.items(): if key not in EXPORT_CONFIG_MAP: raise ValueError(f"Unknown exporter type: {key}, available types: {EXPORT_CONFIG_MAP.keys()}") config_class = EXPORT_CONFIG_MAP[key] result[key] = config_class.model_validate(config) if len(result) < 1: raise ValueError("At least one exporter configuration is required") return result
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/prompts.py
src/llm_jp_eval/prompts.py
# jinja template for evaluation prompt (Japanese) evaluation_prompt_template = """ {%- if dataset_instruction -%} ### 指示 {{ dataset_instruction }} {% endif -%} {%- if answer_pattern %} ### 回答形式 {{ answer_pattern }} {% endif -%} {%- if few_shots %} <examples> {%- for shot in few_shots %} <example_{{ loop.index }}> ### 入力: {{ shot.input }} ### 応答: {{ shot.output }} </example_{{ loop.index }}> {%- endfor %} </examples> {% endif %} ### 入力: {{ input }} ### 応答: """.lstrip() # jinja template for evaluation prompt (English) evaluation_prompt_template_en = """ {%- if dataset_instruction -%} ### Instruction {{ dataset_instruction }} {% endif -%} {%- if answer_pattern %} ### Answer Format {{ answer_pattern }} {% endif -%} {%- if few_shots %} <examples> {%- for shot in few_shots %} <example_{{ loop.index }}> ### Input: {{ shot.input }} ### Response: {{ shot.output }} </example_{{ loop.index }}> {%- endfor %} </examples> {% endif %} ### Input: {{ input }} ### Response: """.lstrip()
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/cli.py
src/llm_jp_eval/cli.py
import argparse import logging import sys from typing import Callable, Type, TypeVar from pydantic import BaseModel, FilePath from pydantic_settings import BaseSettings, CliApp, PydanticBaseSettingsSource, YamlConfigSettingsSource logger = logging.getLogger(__name__) # https://github.com/pydantic/pydantic-settings/issues/259#issuecomment-2580089623 class DeferredYamlConfigSettingsSource(YamlConfigSettingsSource): """YAML config settings source that can reload config files later.""" def __call__(self): yaml_file_to_reload = self.current_state.get("config", self.yaml_file_path) super().__init__( settings_cls=self.settings_cls, yaml_file=yaml_file_to_reload, ) return super().__call__() ConfigT = TypeVar("ConfigT", bound=BaseModel) def setup_cli(base_model: Type[ConfigT], commands: dict[str, Callable] | None = None) -> ConfigT: class WrapSettings( BaseSettings, base_model, # type: ignore cli_implicit_flags=True, ): config: FilePath | None = None dry_run: bool = False @classmethod def settings_customise_sources( cls, settings_cls: type[BaseSettings], init_settings: PydanticBaseSettingsSource, env_settings: PydanticBaseSettingsSource, dotenv_settings: PydanticBaseSettingsSource, file_secret_settings: PydanticBaseSettingsSource, ) -> tuple[PydanticBaseSettingsSource, ...]: """Customise settings sources.""" deferred_yaml_settings = DeferredYamlConfigSettingsSource(settings_cls) return ( init_settings, env_settings, dotenv_settings, file_secret_settings, deferred_yaml_settings, ) def cli_cmd(self) -> None: if self.dry_run: print(self.model_dump_json(indent=2)) sys.exit(0) if commands is not None: parser = argparse.ArgumentParser(add_help=False) parser.add_argument("command", choices=commands.keys(), help="sub commands") help_flags = ["-h", "--help"] has_help = any(flag in sys.argv[1:] for flag in help_flags) # Display main help if no subcommand is specified if has_help and (len(sys.argv) <= 2 or sys.argv[1] in help_flags): parser.print_help() sys.exit(0) args, remaining_args = parser.parse_known_args() parsed_settings = CliApp.run(WrapSettings, cli_args=remaining_args) commands[args.command](base_model.model_validate(parsed_settings.model_dump())) else: parsed_settings = CliApp.run(WrapSettings) return base_model.model_validate(parsed_settings.model_dump())
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/online_inference.py
src/llm_jp_eval/online_inference.py
import os from typing import Annotated, Literal import openai from langchain_community.llms import VLLMOpenAI from langchain_openai import ChatOpenAI from pydantic import BaseModel, ConfigDict, Field # https://platform.openai.com/docs/api-reference/chat/create class OpenAIGenerationConfig(BaseModel): model_config = ConfigDict(extra="allow") frequency_penalty: float = 0.0 top_p: float = 1.0 temperature: float = 1.0 max_tokens: int | None = None class OnlineInferenceBaseConfig(BaseModel): provider: Literal["openai", "vllm-openai"] generation_config: BaseModel max_concurrent: int = 8 class VLLMOpenAIConfig(OnlineInferenceBaseConfig): provider: Literal["vllm-openai"] = "vllm-openai" generation_config: OpenAIGenerationConfig = Field(default_factory=OpenAIGenerationConfig) pipeline_kwargs: dict = Field(default_factory=dict) model_name: str hostname: str class OpenAIConfig(OnlineInferenceBaseConfig): provider: Literal["openai"] = "openai" generation_config: OpenAIGenerationConfig = Field(default_factory=OpenAIGenerationConfig) model_name: str pipeline_kwargs: dict = Field(default_factory=dict) OnlineInferenceConfig = Annotated[OpenAIConfig | VLLMOpenAIConfig, Field(discriminator="provider")] def openai_inference_model(cfg: OpenAIConfig): """Create and return OpenAI model instance.""" openai_organization = os.environ.get("OPENAI_ORGANIZATION") if openai_organization: openai.organization = openai_organization openai.api_key = os.getenv("OPENAI_API_KEY") return ChatOpenAI(name=cfg.model_name, model_kwargs=cfg.pipeline_kwargs, **cfg.generation_config.model_dump()) def vllm_openai_model(cfg: VLLMOpenAIConfig): """Create and return vLLM model instance.""" return VLLMOpenAI( openai_api_key="EMPTY", openai_api_base=f"http://{cfg.hostname}/v1", model_name=cfg.model_name, model_kwargs=cfg.pipeline_kwargs, **cfg.generation_config.model_dump(), ) def get_online_inference_model(cfg: OnlineInferenceConfig): """Get the inference model based on configuration.""" match cfg.provider: case "openai": return openai_inference_model(OpenAIConfig.model_validate(cfg.model_dump())) case "vllm-openai": return vllm_openai_model(VLLMOpenAIConfig.model_validate(cfg.model_dump())) case _: raise ValueError(f"Invalid provider: {cfg.provider}")
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/evaluator.py
src/llm_jp_eval/evaluator.py
import json import logging from datetime import datetime from pathlib import Path from statistics import mean from typing import Any, Dict, Optional, Tuple import numpy as np import torch from fuzzywuzzy import fuzz from tqdm import tqdm from llm_jp_eval.answer_parser import AnswerPatternId, extract_answer_with_pattern from llm_jp_eval.exporter.base import EvaluationExporter, EvaluationResult, OutputRecord from llm_jp_eval.jaster import DatasetRegistry from llm_jp_eval.jaster.base import OutputInfo from llm_jp_eval.logging import setup_logging from llm_jp_eval.metrics.metrics import calculate_metrics, init_metrics from llm_jp_eval.online_inference import get_online_inference_model from llm_jp_eval.schemas import EvaluationConfig, InferenceInputData from llm_jp_eval.utils import ( GeneratedSample, JSONEncoder, get_evaluation_prompt, get_few_shot_samples, get_or_create_event_loop, normalize, run_online_inference, set_seed, ) setup_logging() logger = logging.getLogger(__name__) def load_dataset_and_construct_prompt_template( cfg: EvaluationConfig, ) -> list[InferenceInputData]: cfg.inference_input_path.mkdir(parents=True, exist_ok=True) logger.info(f"dump inference prompts: {cfg.inference_input_path=}") overrides = cfg.eval_dataset_config.dataset_info_overrides processed_input_data: list[InferenceInputData] = [] for target_dataset in [dataset for dataset in cfg.eval_dataset_config.datasets]: prompt_dump_path = cfg.inference_input_path / f"{target_dataset}.eval-prompt.json" if prompt_dump_path.exists(): inference_input_data = InferenceInputData.model_validate(json.load(prompt_dump_path.open(encoding="utf-8"))) processed_input_data.append(inference_input_data) continue target_dataset_path: Path = cfg.target_dataset_dir / f"{target_dataset}.json" if not target_dataset_path.exists(): raise RuntimeError(f"{target_dataset} not found in {cfg.target_dataset_dir}") with target_dataset_path.open(encoding="utf-8") as f: dataset_info = OutputInfo(**json.load(f)) if target_dataset in overrides: for k, v in overrides[target_dataset].items(): if hasattr(dataset_info, k): setattr(dataset_info, k, v) else: logger.warning(f"Invalid key {k} in overrides for {target_dataset}") num_few_shots = cfg.num_few_shots if dataset_info.num_few_shots is None else dataset_info.num_few_shots few_shots = get_few_shot_samples(target_dataset_path, num_few_shots) # Use dataset-level custom_prompt_template if available, otherwise use config-level template custom_template = dataset_info.custom_prompt_template or cfg.custom_prompt_template prompt_template = get_evaluation_prompt( dataset_info.instruction, few_shots, custom_template, dataset_info.answer_pattern_id, dataset_info.language, ) num_samples = len(dataset_info.samples) if cfg.max_num_samples < 0 else cfg.max_num_samples # Use safe string replacement to avoid conflicts with LaTeX curly braces template_str = prompt_template samples = [ { "input": sample.input, "prompt": template_str.replace("<%input%>", sample.input), "system_prompt": cfg.default_system_prompt, "gold": sample.output, "metadata": sample.metadata, } for sample in dataset_info.samples[:num_samples] ] inference_input_data = InferenceInputData( target_dataset=target_dataset, output_length=dataset_info.output_length, max_num_samples=cfg.max_num_samples, metrics=dataset_info.metrics, instruction=dataset_info.instruction, prompt_template=prompt_template, answer_extract_pattern=dataset_info.answer_extract_pattern, answer_pattern_id=dataset_info.answer_pattern_id, system_prompt=cfg.default_system_prompt, label_list=dataset_info.label_list, samples=samples, config=cfg.model_dump( exclude={ "online_inference_config", "wandb", "metainfo", } ), ) with open(prompt_dump_path, "w", encoding="utf8") as fout: json.dump(inference_input_data.model_dump(), fout, indent=1, ensure_ascii=False, cls=JSONEncoder) print(file=fout) processed_input_data.append(inference_input_data) logger.info(f"{prompt_dump_path} generated") return processed_input_data def load_inference_results(target_dataset: str, inference_result_dir: str) -> Dict[str, Any]: target_dataset_path: Path = Path(inference_result_dir) / f"{target_dataset}.eval-generated.json" if not target_dataset_path.exists(): raise RuntimeError(f"{target_dataset} not found in {inference_result_dir}") with Path(f"{inference_result_dir}/{target_dataset}.eval-generated.json").open(encoding="utf-8") as f: target_data = json.load(f) return target_data def post_process_score_results( score_results: dict[str, dict[str, float]], cfg: EvaluationConfig ) -> Tuple[dict[str, float], list[str], dict[str, dict[str, float]]]: post_processed_scores: dict[str, float] = dict() for dataset in cfg.eval_dataset_config.datasets: if dataset not in score_results: logger.warning(f"skip dataset {dataset} ...") continue for metric, score in score_results[dataset].items(): post_processed_scores[f"{dataset}_{metric}"] = score category_scores: dict[str, list[float]] = { category_name: [] for category_name in cfg.eval_dataset_config.categories.keys() } for category_name, category in cfg.eval_dataset_config.categories.items(): for dataset in category.datasets: if dataset not in score_results: continue if dataset in category.metrics: category_scores[category_name].append(score_results[dataset][category.metrics[dataset]]) else: try: category_scores[category_name].append(score_results[dataset][category.default_metric]) except KeyError as e: logger.error( f"Dataset {dataset} does not have metric {category.metrics.get(dataset, category.default_metric)}" ) raise e for category_name, scores in category_scores.items(): post_processed_scores[category_name] = mean(scores) if len(scores) > 0 else 0.0 post_processed_scores["AVG"] = mean(post_processed_scores[cat] for cat in category_scores.keys()) lang_scores: dict[str, dict[str, float]] = {} for category_name, category in cfg.eval_dataset_config.categories.items(): lang_datasets: dict[str, list[float]] = {} for dataset in category.datasets: if dataset not in score_results: continue dataset_processor_class = DatasetRegistry.get(dataset) dataset_language = dataset_processor_class.language.upper() if dataset_language not in lang_datasets: lang_datasets[dataset_language] = [] metric_key = category.metrics.get(dataset, category.default_metric) if metric_key in score_results[dataset]: lang_datasets[dataset_language].append(score_results[dataset][metric_key]) for language, scores in lang_datasets.items(): if scores: if language not in lang_scores: lang_scores[language] = {} lang_scores[language][category_name] = mean(scores) for language in lang_scores: if lang_scores[language]: lang_scores[language]["AVG"] = mean(lang_scores[language].values()) return post_processed_scores, list(category_scores.keys()), lang_scores def get_evaluation_result( run_name: str, samples: list[GeneratedSample], num_few_shots: int, target_dataset_name: str, target_split: str, target_data_answer_extract_pattern: Optional[str], answer_pattern_id: AnswerPatternId, metrics: list[str], label_list: list[str], metainfo: dict, dataset_processor_name: Optional[str] = None, ) -> tuple[dict[str, float], list[dict[str, str]], list[OutputRecord]]: xs = [sample["input"] for sample in samples] y_trues: list[str] = [] y_preds: list[str] = [] samples_metadata: list[dict] = [] output_dict: list[dict[str, str]] = [] output_records: list[OutputRecord] = [] # dataset_processor_classの取得 dataset_processor_class = None if dataset_processor_name and answer_pattern_id == AnswerPatternId.DATASET: try: dataset_processor_class = DatasetRegistry.get(dataset_processor_name) except Exception as e: logger.warning(f"Failed to get dataset processor for {dataset_processor_name}: {e}") for idx, sample in enumerate(tqdm(samples, desc=target_dataset_name)): y_true = normalize(sample["gold"]) custom_regex = None if answer_pattern_id != AnswerPatternId.CUSTOM else target_data_answer_extract_pattern y_pred = normalize( extract_answer_with_pattern( sample["generated"], answer_pattern_id, custom_regex, dataset_processor_class=dataset_processor_class, metadata=sample.get("metadata", {}), ) ).strip() output_dict.append({"input": sample["input"], "pred": y_pred, "gold": y_true}) y_trues.append(y_true) y_preds.append(y_pred) samples_metadata.append(sample.get("metadata", {})) output_records.append( OutputRecord( run_name=run_name, id=str(idx), basemodel_name=metainfo["basemodel_name"], target_dataset=target_dataset_name, num_few_shots=num_few_shots, target_split=target_split, input=sample["input"], output=sample["generated"], pred=y_pred, true=y_true, prompt=sample["prompt"], exact=1 if y_pred == y_true else 0, char_f1=fuzz.token_sort_ratio(y_pred, y_true) / 100.0, metadata=sample.get("metadata", {}), ) ) assert len(y_preds) == len(y_trues) score_dict: dict[str, float] = {} ool_count = 0 for y_pred in y_preds: if not label_list and not y_pred: ool_count += 1 elif label_list and y_pred not in label_list: ool_count += 1 score_dict["ool"] = ool_count / len(y_preds) for metric in metrics: score_dict[metric] = calculate_metrics( metric=metric, y_preds=y_preds, y_trues=y_trues, xs=xs, samples_metadata=samples_metadata ) return score_dict, output_dict, output_records def evaluate( cfg: EvaluationConfig, ) -> tuple[dict[str, float], dict[str, list[dict[str, str]]]]: set_seed() logger.info("setup evaluation ...") processed_input_data = load_dataset_and_construct_prompt_template(cfg) target_metrics = list( {metric for inference_input_data in processed_input_data for metric in inference_input_data.metrics} ) init_metrics(metrics=target_metrics, cache_dir=cfg.output_dir / "cache") # NOTE: Inherit and use wandb settings from inference run if wandb was enabled during inference # However, explicitly configured exporter_config.wandb values take precedence when wandb inheritance is disabled if cfg.inference_result_config is not None and cfg.inference_result_config.wandb.run_id: if "wandb" not in cfg.exporters or cfg.exporters["wandb"].inherit_wandb_run: # type: ignore logger.info("Inheriting wandb settings from inference run") if "wandb" in cfg.exporters: cfg.exporters["wandb"].run_id = cfg.inference_result_config.wandb.run_id # type: ignore cfg.exporters["wandb"].project = cfg.inference_result_config.wandb.project # type: ignore cfg.exporters["wandb"].entity = cfg.inference_result_config.wandb.entity # type: ignore else: cfg.exporters["wandb"] = cfg.inference_result_config.wandb exporters: list[EvaluationExporter] = [] for _, export_config in cfg.exporters.items(): exporters.append(export_config.exporter_class(eval_config=cfg, config=export_config)) start = datetime.now() # other api will be added init = datetime.now() time_profile = { "(init)": (init - start).total_seconds(), "(infer)": None, } target_datasets = [dataset for dataset in cfg.eval_dataset_config.datasets] score_results: dict[str, dict[str, float]] = {} output_results: dict[str, list[dict[str, str]]] = {} output_records: list[OutputRecord] = [] generation_config: dict[str, Any] = {} answer_pattern_id = AnswerPatternId.CUSTOM cfg_in_inference = None with torch.inference_mode(): for target_dataset in target_datasets: start_generate = datetime.now() samples: list[GeneratedSample] = [] if cfg.inference_result_config: generation_config = cfg.inference_result_config.generation_config target_data = load_inference_results(target_dataset, str(cfg.inference_result_dir)) if target_data is None: continue if cfg_in_inference is None: cfg_in_inference = target_data["config"] if cfg_in_inference != target_data["config"]: logger.warning( f"Inconsistent config found.\n{target_dataset}: {cfg_in_inference}\n{target_dataset}: {target_data['config']}" ) target_data_answer_extract_pattern = target_data.get("answer_extract_pattern") or cfg_in_inference.get( "default_answer_extract_pattern" ) if "answer_pattern_id" in target_data: answer_pattern_id = target_data["answer_pattern_id"] samples = target_data["samples"] metrics, label_list = target_data["metrics"], target_data["label_list"] prompt_template_text = target_data["prompt_template"] elif cfg.online_inference_config: generation_config = cfg.online_inference_config.generation_config.model_dump() with (cfg.inference_input_path / f"{target_dataset}.eval-prompt.json").open(encoding="utf-8") as f: inference_input_data = InferenceInputData.model_validate(json.load(f)) template_text = inference_input_data.prompt_template model = get_online_inference_model(cfg.online_inference_config) target_data_answer_extract_pattern = ( inference_input_data.answer_extract_pattern or cfg.default_answer_extract_pattern ) answer_pattern_id = inference_input_data.answer_pattern_id loop = get_or_create_event_loop() inferenced_samples = loop.run_until_complete( run_online_inference( inference_input_data.samples, template_text, model, max_concurrent=cfg.online_inference_config.max_concurrent, description=target_dataset, ) ) for sample, result in zip(inference_input_data.samples, inferenced_samples, strict=False): prompt = template_text.replace("<%input%>", sample.input) samples.append( GeneratedSample( input=sample.input, prompt=prompt, generated=result["output"], gold=sample.gold, metadata=sample.metadata, ) ) metrics, label_list = inference_input_data.metrics, inference_input_data.label_list prompt_template_text = inference_input_data.prompt_template else: raise ValueError("Either inference_result_dir or online_inference_config must be set.") score_result, output_result, _records = get_evaluation_result( run_name=cfg._resolved_run_name, samples=samples, num_few_shots=cfg.num_few_shots, target_dataset_name=target_dataset, target_split=cfg.target_split, target_data_answer_extract_pattern=target_data_answer_extract_pattern, answer_pattern_id=answer_pattern_id, metrics=metrics, label_list=label_list, metainfo=cfg.metainfo.model_dump(), dataset_processor_name=target_dataset, ) score_results[target_dataset] = score_result output_results[target_dataset] = [{"prompt": prompt_template_text, **x} for x in output_result] time_profile[target_dataset] = (datetime.now() - start_generate).total_seconds() output_records.extend(_records) post_processed_score_results, categories, lang_scores = post_process_score_results(score_results, cfg) post_processed_score_results = { k: (0 if isinstance(v, (int, float)) and np.isnan(v) else v) for k, v in post_processed_score_results.items() } time_profile["(infer)"] = (datetime.now() - init).total_seconds() for exporter in exporters: with exporter: exporter.output_results( EvaluationResult( run_name=cfg._resolved_run_name, score_results=post_processed_score_results, time_profile=time_profile, output_records=output_records, dataset_categories=categories, metainfo=cfg.metainfo.model_dump(), generation_config=generation_config, lang_scores=lang_scores, ) ) return post_processed_score_results, output_results
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/utils.py
src/llm_jp_eval/utils.py
import asyncio import json import logging import random import re import sys import unicodedata import warnings from contextlib import contextmanager from enum import Enum from pathlib import Path, PosixPath from typing import TYPE_CHECKING, Any, Literal, TypedDict import jinja2 import numpy as np import torch import transformers from tqdm.asyncio import tqdm from llm_jp_eval.answer_parser import ANSWER_PATTERNS, AnswerPatternId from llm_jp_eval.jaster.base import Sample from llm_jp_eval.prompts import evaluation_prompt_template, evaluation_prompt_template_en if TYPE_CHECKING: from llm_jp_eval.schemas import InferenceInputData logger = logging.getLogger(__name__) warnings.filterwarnings("ignore") class GeneratedSample(TypedDict): input: str prompt: str generated: str gold: str metadata: dict[str, Any] class JSONEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, (Path, PosixPath)): return str(obj) if isinstance(obj, Enum): return str(obj) return super().default(obj) def parse_float(input_str: str) -> float: input_str = str(input_str) cleaned_str = re.sub(r"[^0-9.]", "", input_str) try: return float(cleaned_str) except ValueError: return -2.0 def normalize(input_str: str) -> str: return unicodedata.normalize("NFKC", input_str) def get_few_shot_samples(target_dataset_path: Path, num_few_shots: int) -> list[Sample]: if num_few_shots == 0: return list[Sample]() dataset_json_name = target_dataset_path.name target_few_shot_path = target_dataset_path.resolve().parent.parent / "train" / dataset_json_name assert target_few_shot_path.exists(), ( f"Wrong path {target_few_shot_path.resolve()}, can not extract few-shot samples" ) samples = [Sample(**data) for data in json.loads(target_few_shot_path.read_text(encoding="utf-8"))["samples"]] assert len(samples) >= num_few_shots, ( f"Wrong number of few shots {num_few_shots}, we only have {len(samples)} few shots" ) return samples[:num_few_shots] @contextmanager def _logging_disabled(highest_level=logging.CRITICAL): """ A context manager that will prevent any logging messages triggered during the body from being processed. :param highest_level: the maximum logging level in use. This would only need to be changed if a custom level greater than CRITICAL is defined. """ # two kind-of hacks here: # * can't get the highest logging level in effect => delegate to the user # * can't get the current module-level override => use an undocumented # (but non-private!) interface previous_level = logging.root.manager.disable logging.disable(highest_level) try: yield finally: logging.disable(previous_level) @contextmanager def _suppress_transformers_warning(): current_verbosity = transformers.logging.get_verbosity() transformers.logging.set_verbosity_error() try: yield finally: transformers.logging.set_verbosity(current_verbosity) def get_evaluation_prompt( instruction: str, few_shots: list[Sample], custom_prompt_template: str | dict[Literal["ja", "en"], str] | None, answer_pattern_id: AnswerPatternId = AnswerPatternId.CUSTOM, language: Literal["ja", "en"] = "ja", ) -> str: # Select appropriate template based on language if custom_prompt_template is None: # Use default template based on language template_str = evaluation_prompt_template if language == "ja" else evaluation_prompt_template_en elif isinstance(custom_prompt_template, dict): # Language-specific templates provided if language == "ja" and "ja" in custom_prompt_template: template_str = custom_prompt_template["ja"] elif language == "en" and "en" in custom_prompt_template: template_str = custom_prompt_template["en"] else: # Fallback to default language template if not found template_str = evaluation_prompt_template if language == "ja" else evaluation_prompt_template_en else: # Single template provided - use as is template_str = custom_prompt_template template = jinja2.Template(template_str, keep_trailing_newline=True) # Format few_shot outputs according to the answer pattern few_shots_data = [] if few_shots: for shot in few_shots: formatted_output = shot.output if answer_pattern_id in (set(ANSWER_PATTERNS) - {AnswerPatternId.CUSTOM, AnswerPatternId.DATASET}): pattern_config = ANSWER_PATTERNS[answer_pattern_id] formatted_output = pattern_config.format_output(shot.output) few_shots_data.append({"input": shot.input, "output": formatted_output}) if answer_pattern_id == AnswerPatternId.CUSTOM: answer_pattern = None # customパターンはpattern_textなし elif answer_pattern_id == AnswerPatternId.DATASET: answer_pattern = None # datasetパターンはpattern_textなし(データセット側でカスタム抽出) elif answer_pattern_id in ANSWER_PATTERNS: answer_pattern = ANSWER_PATTERNS[answer_pattern_id].pattern_text else: available_patterns = [ p.name for p in AnswerPatternId if p != AnswerPatternId.CUSTOM and p != AnswerPatternId.DATASET ] raise ValueError( f"Unknown answer pattern: {answer_pattern_id}. Available patterns: {available_patterns}, AnswerPatternId.CUSTOM, or AnswerPatternId.DATASET" ) prompt_template = template.render( dataset_instruction=instruction, answer_pattern=answer_pattern, few_shots=few_shots_data, input="<%input%>" ) return prompt_template def set_seed() -> None: random.seed(42) np.random.seed(42) torch.manual_seed(42) if torch.cuda.is_available(): torch.cuda.manual_seed(42) if torch.backends.cudnn.is_available(): torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False async def run_online_inference( samples: list["InferenceInputData.InferenceSample"], template_text: str, model, max_concurrent: int = 8, description: str | None = None, ) -> list[dict[str, str]]: semaphore = asyncio.Semaphore(max_concurrent) async def process_sample(sample: "InferenceInputData.InferenceSample"): async with semaphore: prompt = template_text.replace("<%input%>", sample.input) response = model.invoke(prompt) generated_output = response.content if hasattr(response, "content") else str(response) return {"output": generated_output} return await tqdm.gather(*[process_sample(sample) for sample in samples], desc=description) def get_or_create_event_loop(): # Windows環境での対策 if sys.platform == "win32": asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) try: loop = asyncio.get_event_loop() if loop.is_closed(): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) except RuntimeError: # 'There is no current event loop in thread' エラーが発生した場合 loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) return loop
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/logging.py
src/llm_jp_eval/logging.py
import logging import sys def setup_logging( level=logging.INFO, format_str="[%(asctime)s] %(levelname)s - %(name)s: %(message)s", date_format="%Y-%m-%d %H:%M:%S", handler=None, ) -> None: root_logger = logging.getLogger() root_logger.setLevel(level) if handler is None: handler = logging.StreamHandler(sys.stdout) handler.setLevel(level) formatter = logging.Formatter(format_str, date_format) handler.setFormatter(formatter) root_logger.addHandler(handler) # Suppress verbose logging from third-party libraries logging.getLogger("httpx").setLevel(logging.WARNING) logging.getLogger("httpcore").setLevel(logging.WARNING)
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/__init__.py
src/llm_jp_eval/__init__.py
__version__ = "2.0.0"
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/answer_parser.py
src/llm_jp_eval/answer_parser.py
import re from enum import Enum from typing import Dict class AnswerPatternId(str, Enum): CUSTOM = "custom" DATASET = "dataset" ANSWER_TAGS_JP = "answer_tags_jp" ANSWER_TAGS_EN = "answer_tags_en" CHOICE_ONLY_JP = "choice_only_jp" CHOICE_ONLY_EN = "choice_only_en" JSON_OUTPUT_JP = "json_output_jp" JSON_OUTPUT_EN = "json_output_en" CODE_OUTPUT_JP = "code_output_jp" CODE_OUTPUT_EN = "code_output_en" LATEX_BOXED_JP = "latex_boxed_jp" LATEX_BOXED_EN = "latex_boxed_en" class AnswerPatternConfig: def __init__(self, pattern_text: str | None, extract_regex: str, output_format: str | None = None): self.pattern_text = pattern_text self.extract_regex = extract_regex self.output_format = output_format def extract_answer(self, generated_text: str) -> str: match = re.search(self.extract_regex, generated_text, re.DOTALL) return "".join(match.groups()).strip() if match else "" def format_output(self, value: str) -> str: """Format the output value according to the pattern's output_format.""" if self.output_format is None: return value return self.output_format.format(value=value) ANSWER_PATTERNS: Dict[AnswerPatternId, AnswerPatternConfig] = { AnswerPatternId.ANSWER_TAGS_JP: AnswerPatternConfig( pattern_text="<answer></answer>タグで囲んで回答してください", extract_regex=r"<answer>(.*?)</answer>", output_format="<answer>{value}</answer>", ), AnswerPatternId.ANSWER_TAGS_EN: AnswerPatternConfig( pattern_text="Please answer within <answer></answer> tags", extract_regex=r"<answer>(.*?)</answer>", output_format="<answer>{value}</answer>", ), AnswerPatternId.CHOICE_ONLY_JP: AnswerPatternConfig( pattern_text="選択肢の番号または文字のみで回答してください", extract_regex=r"(?s)^(.*?)(?=\n|\Z)", output_format=None, # 生の値をそのまま使用 ), AnswerPatternId.CHOICE_ONLY_EN: AnswerPatternConfig( pattern_text="Please answer with only the choice number or letter", extract_regex=r"(?s)^(.*?)(?=\n|\Z)", output_format=None, # 生の値をそのまま使用 ), AnswerPatternId.JSON_OUTPUT_JP: AnswerPatternConfig( pattern_text="```json\n```のJSON形式で回答してください", extract_regex=r"```json\s*\n?(.*?)\n?```", output_format="```json\n{value}\n```", ), AnswerPatternId.JSON_OUTPUT_EN: AnswerPatternConfig( pattern_text="Please answer in JSON format: ```json\n```", extract_regex=r"```json\s*\n?(.*?)\n?```", output_format="```json\n{value}\n```", ), AnswerPatternId.CODE_OUTPUT_JP: AnswerPatternConfig( pattern_text="```\n```のコードブロック形式で回答してください", extract_regex=r"```(?:\w+)?\s*\n?(.*?)\n?```", output_format="```\n{value}\n```", ), AnswerPatternId.CODE_OUTPUT_EN: AnswerPatternConfig( pattern_text="Please answer in code block format: ```\n```", extract_regex=r"```(?:\w+)?\s*\n?(.*?)\n?```", output_format="```\n{value}\n```", ), AnswerPatternId.LATEX_BOXED_JP: AnswerPatternConfig( pattern_text="注意:最終的な答えを $\\boxed{}$ に入れてください。", extract_regex=r"\\boxed\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}", output_format="$\\boxed{{{value}}}$", ), AnswerPatternId.LATEX_BOXED_EN: AnswerPatternConfig( pattern_text="Note: Please put the final answer in the $\\boxed{}$.", extract_regex=r"\\boxed\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}", output_format="$\\boxed{{{value}}}$", ), } def extract_answer_with_pattern( generated_text: str, pattern_id: AnswerPatternId, custom_regex: str | None = None, dataset_processor_class=None, metadata: dict | None = None, ) -> str: if pattern_id == AnswerPatternId.DATASET: # DATASETパターンの場合はデータセット側のextract_answerクラスメソッドを呼び出す if dataset_processor_class: return dataset_processor_class.extract_answer(generated_text, metadata or {}) else: raise ValueError("Dataset processor class required for DATASET pattern") if pattern_id == AnswerPatternId.CUSTOM: # customパターンの場合はcustom_regexまたはデフォルトパターンを使用 regex = custom_regex if custom_regex else r"(?s)^(.*?)(?=\n\n|\Z)" match = re.search(regex, generated_text, re.DOTALL) return "".join(match.groups()).strip() if match else "" if pattern_id not in ANSWER_PATTERNS: available_patterns = [p.name for p in AnswerPatternId if p != AnswerPatternId.CUSTOM] raise ValueError( f"Unknown answer pattern: {pattern_id}. Available patterns: {available_patterns} or AnswerPatternId.CUSTOM" ) pattern_config = ANSWER_PATTERNS[pattern_id] return pattern_config.extract_answer(generated_text) __all__ = ["AnswerPatternId", "AnswerPatternConfig", "ANSWER_PATTERNS", "extract_answer_with_pattern"]
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/metrics/metrics.py
src/llm_jp_eval/metrics/metrics.py
import ast import functools import json import logging import math import re import string import subprocess import sys import textwrap from abc import ABC, abstractmethod from collections import defaultdict from pathlib import Path from statistics import mean from typing import Callable, ClassVar, Type import bert_score import math_verify import torch from comet import download_model, load_from_checkpoint from fuzzywuzzy import fuzz from jinja2 import Template from sacrebleu import BLEU from scipy.stats import pearsonr, spearmanr from sklearn.metrics import accuracy_score from tqdm import tqdm from llm_jp_eval.metrics import RougeJa from llm_jp_eval.metrics.mifeval import mifeval from llm_jp_eval.metrics.sandbox_client import CodeExecutor, is_sandbox_running from llm_jp_eval.utils import _suppress_transformers_warning logger = logging.getLogger(__name__) @functools.cache def get_comet_model(saving_directory: str | None): saving_directory = saving_directory or "tmp/Unbabel/wmt22-comet-da" comet_model_path = download_model("Unbabel/wmt22-comet-da", saving_directory=saving_directory) return load_from_checkpoint(comet_model_path) class BaseMetric(ABC): name: ClassVar[str] def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) if hasattr(cls, "name"): MetricRegistry.register_class(cls) @abstractmethod def __call__(self, y_preds: list[str], y_trues: list[str], **kwargs) -> float: pass class ContextualBaseMetric(ABC): name: ClassVar[str] def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) if hasattr(cls, "name"): MetricRegistry.register_class(cls) @abstractmethod def __call__(self, xs: list[str], y_preds: list[str], y_trues: list[str], **kwargs) -> float: pass MetricFunction = BaseMetric | ContextualBaseMetric class MetricRegistry: _metrics: dict[str, MetricFunction] = {} @classmethod def register_class(cls, metric_cls: Type[MetricFunction]) -> None: instance = metric_cls() cls._metrics[metric_cls.name] = instance @classmethod def register(cls, name: str) -> Callable: def decorator(metric: MetricFunction) -> MetricFunction: cls._metrics[name] = metric return metric return decorator @classmethod def get(cls, name: str) -> MetricFunction: if name not in cls._metrics: raise ValueError(f"Unknown metric: {name}") return cls._metrics[name] class ExactMatchMetric(BaseMetric): name = "exact_match" def __call__(self, y_preds: list[str], y_trues: list[str], **kwargs) -> float: return accuracy_score(y_trues, y_preds) class PearsonMetric(BaseMetric): name = "pearson" def __call__(self, y_preds: list[str], y_trues: list[str], **kwargs) -> float: try: score = pearsonr(list(map(float, y_trues)), list(map(float, y_preds)))[0] return 0.0 if math.isnan(score) else score except (ValueError, TypeError): return 0.0 class SpearmanMetric(BaseMetric): name = "spearman" def __call__(self, y_preds: list[str], y_trues: list[str], **kwargs) -> float: try: score = spearmanr(list(map(float, y_trues)), list(map(float, y_preds)))[0] return 0.0 if math.isnan(score) else score except (ValueError, TypeError): return 0.0 class CharF1Metric(BaseMetric): name = "char_f1" def __call__(self, y_preds: list[str], y_trues: list[str], **kwargs) -> float: return mean(fuzz.token_sort_ratio(pred, true) / 100.0 for pred, true in zip(y_preds, y_trues, strict=False)) class SetF1Metric(BaseMetric): name = "set_f1" def __call__(self, y_preds: list[str], y_trues: list[str], **kwargs) -> float: scores = [] for y_pred, y_true in zip(y_preds, y_trues, strict=False): pred_set = {x.strip() for x in y_pred.split("\n")} true_set = {x.strip() for x in y_true.split("\n")} if not pred_set or not true_set: scores.append(0.0) continue true_positives = len(pred_set & true_set) precision = true_positives / len(pred_set) recall = true_positives / len(true_set) if precision + recall == 0: scores.append(0.0) else: f1 = 2 * (precision * recall) / (precision + recall) scores.append(f1) return mean(scores) class CodeExecMetric(BaseMetric): name = "code_exec" def __call__(self, y_preds: list[str], y_trues: list[str], **kwargs) -> float: logger.warning("CodeExecMetric is deprecated. Use CodeExecMetricWithSandbox instead.") scores = [] for pred, true in zip(y_preds, y_trues, strict=False): testcases = ast.literal_eval(true) all_passes = True for test_case in testcases: try: exec(pred) exec(test_case) except Exception: all_passes = False break scores.append(1.0 if all_passes else 0.0) return mean(scores) class CodeExecMetricWithSandbox(BaseMetric): name = "code_exec_sandbox" code_template = textwrap.dedent(""" {{ code}} {% for testcase in testcases %} {{ testcase }} {% endfor %} """) def __call__(self, y_preds: list[str], y_trues: list[str], **kwargs) -> float: if not is_sandbox_running(): logger.warning("Sandbox is not running. Skipping code execution.") return 0.0 scores = [] for pred, true in tqdm(zip(y_preds, y_trues, strict=False), total=len(y_preds), desc="Code execution"): testcases = ast.literal_eval(true) template = Template(self.code_template) scores.append(CodeExecutor.check_all_passed(code=template.render(code=pred, testcases=testcases)) * 1.0) return mean(scores) class PylintCheckMetric(BaseMetric): name = "pylint_check" def __call__(self, y_preds: list[str], y_trues: list[str], **kwargs) -> float: scores = [] tmp_path = Path("pylint.test.tmp.py") for pred in tqdm(y_preds, desc="Pylint check"): try: tmp_path.write_text(pred) result = ( subprocess.run( [sys.executable, "-m", "pylint", "--rc-file=pylintrc", str(tmp_path)], stdout=subprocess.PIPE, ) .stdout.decode("utf-8") .split("\n")[1:-1] ) is_valid = not any("E" in line.split()[1] for line in result if line and line.split()) scores.append(1.0 if is_valid else 0.0) except Exception: scores.append(0.0) finally: if tmp_path.exists(): tmp_path.unlink() return mean(scores) class BLEUScoreMetric(BaseMetric): available_langs = ["en", "ja"] def __init__(self, lang: str): if lang not in self.available_langs: raise ValueError(f"Invalid language: {lang}") self.lang = lang self.config = {"effective_order": True, "trg_lang": lang} def __call__(self, y_preds: list[str], y_trues: list[str], **kwargs) -> float: scores = [] for pred, true in zip(y_preds, y_trues, strict=False): if not pred.strip() or not true.strip(): scores.append(float(pred.strip() == true.strip())) continue score = BLEU(**self.config).corpus_score(pred.strip(), [true.strip()]).score # type: ignore scores.append(score) return mean(scores) @classmethod def register(cls) -> None: for lang in cls.available_langs: instance = cls(lang) MetricRegistry.register(f"bleu_{lang}")(instance) class BertScoreMetric(BaseMetric): available_langs = ["en", "ja"] def __init__(self, lang: str): self.lang = lang def __call__(self, y_preds: list[str], y_trues: list[str], **kwargs) -> float: with _suppress_transformers_warning(): return bert_score.score(y_preds, y_trues, lang=self.lang)[2].mean().item() @classmethod def register(cls) -> None: for lang in cls.available_langs: instance = cls(lang) MetricRegistry.register(f"bert_score_{lang}_f1")(instance) class RougeScoreMetric(BaseMetric): available_types = ["rouge1", "rouge2", "rougeLsum", "rouge2_scaling"] def __init__(self, rouge_type: str): if rouge_type not in self.available_types: raise ValueError(f"Invalid rouge type: {rouge_type}") self.rouge_type = rouge_type if rouge_type == "rouge2_scaling": self.scorer = RougeJa(rouge_type=["rouge2"]) else: self.scorer = RougeJa(rouge_type=[rouge_type]) def __call__(self, y_preds: list[str], y_trues: list[str], **kwargs) -> float: if self.rouge_type == "rouge2_scaling": score = self.scorer.score_system(y_preds, y_trues)["rouge2"].mid.fmeasure return score / 100 else: score = self.scorer.score_system(y_preds, y_trues)[self.rouge_type].mid.fmeasure return score @classmethod def register(cls) -> None: for rouge_type in cls.available_types: instance = cls(rouge_type) MetricRegistry.register(rouge_type)(instance) BLEUScoreMetric.register() BertScoreMetric.register() RougeScoreMetric.register() def create_comet_wmt22_scorer(resource_dir: Path | None) -> ContextualBaseMetric: _ = get_comet_model(resource_dir) class CometWmt22Metric(ContextualBaseMetric): def __call__(self, xs: list[str], y_preds: list[str], y_trues: list[str], **kwargs) -> float: comet_model = get_comet_model(resource_dir) comet_data = [ {"src": x, "mt": pred, "ref": true} for x, pred, true in zip(xs, y_preds, y_trues, strict=False) ] gpus = 0 if torch.cuda.is_available(): gpus = 1 return comet_model.predict(comet_data, gpus=gpus).system_score return MetricRegistry.register("comet_wmt22")(CometWmt22Metric()) class MIFEvalMetric(ContextualBaseMetric): available_types = ["strict", "loose"] def __init__(self, mode: str = "strict"): if mode == "strict": self.function = mifeval.test_instruction_following_strict elif mode == "loose": self.function = mifeval.test_instruction_following_loose else: raise ValueError(f"Invalid mode: {mode}. Available modes: {self.available_types}") def __call__(self, xs: list[str], y_preds: list[str], y_trues: list[str], **kwargs) -> float: scores = [] for x, y_pred, y_true in zip(xs, y_preds, y_trues, strict=False): inputs = json.loads(y_true) inputs["prompt"] = x scores.append(self.function(inp=inputs, output=y_pred)) return mean(scores) @classmethod def register(cls) -> None: for mode in cls.available_types: instance = cls(mode) MetricRegistry.register(f"mifeval_{mode}")(instance) MIFEvalMetric.register() class MathmaticalEquivalenceMetric(BaseMetric): name = "mathematical_equivalence" def _try_parse_latex(self, expr: str): normalized = math_verify.parse(f"${expr}$") if normalized and len(normalized) > 0: return normalized return math_verify.parse(expr) def _verify_mathematical_equivalence(self, pred: str, true: str) -> bool: """Verify mathematical equivalence with proper LaTeX environment handling.""" pred_parsed = self._try_parse_latex(pred) true_parsed = self._try_parse_latex(true) if pred_parsed and true_parsed: return math_verify.verify(pred_parsed, true_parsed) # Invalid or empty expressions return False def __call__(self, y_preds: list[str], y_trues: list[str], **kwargs) -> float: return mean( self._verify_mathematical_equivalence(pred, true) for pred, true in zip(y_preds, y_trues, strict=False) ) class PolyMathWeightedAccuracyMetric(MathmaticalEquivalenceMetric): name = "polymath_weighted_accuracy" def __call__( self, y_preds: list[str], y_trues: list[str], samples_metadata: list[dict] | None = None, **kwargs ) -> float: if not samples_metadata: raise ValueError("samples_metadata must be provided for polymath_weighted_accuracy metric") difficulty_weights = {"low": 1, "medium": 2, "high": 4, "top": 8} difficulty_groups = defaultdict(list) for pred, true, metadata in zip(y_preds, y_trues, samples_metadata, strict=False): difficulty = metadata.get("difficulty", "unknown") difficulty_groups[difficulty].append((pred, true)) weighted_sum = sum( difficulty_weights[level] * ( mean(self._verify_mathematical_equivalence(pred, true) for pred, true in difficulty_groups[level]) if level in difficulty_groups else 0.0 ) for level in ["low", "medium", "high", "top"] ) return weighted_sum / 15.0 class DROPF1Metric(BaseMetric): """F1 metric for DROP dataset that handles multiple answer spans.""" name = "drop_f1" def _compute_f1(self, prediction: str, ground_truth: str) -> float: """Compute token-level F1 score between prediction and ground truth strings.""" prediction_tokens = prediction.lower().split() ground_truth_tokens = ground_truth.lower().split() if not prediction_tokens or not ground_truth_tokens: return 0.0 # Compute token-level F1 common = set(prediction_tokens) & set(ground_truth_tokens) num_same = sum(min(prediction_tokens.count(t), ground_truth_tokens.count(t)) for t in common) if num_same == 0: return 0.0 precision = num_same / len(prediction_tokens) recall = num_same / len(ground_truth_tokens) f1 = (2 * precision * recall) / (precision + recall) return f1 def __call__( self, y_preds: list[str], y_trues: list[str], samples_metadata: list[dict] | None = None, **kwargs ) -> float: """Calculate DROP F1 score considering multiple answer spans. This metric is designed specifically for DROP dataset and expects samples_metadata with 'all_answer_spans' for each sample. """ if not samples_metadata: raise ValueError("DROPF1Metric requires samples_metadata with 'all_answer_spans'") scores = [] for i, pred in enumerate(y_preds): metadata = samples_metadata[i] all_spans = metadata.get("all_answer_spans", []) if not all_spans: raise ValueError(f"Sample {i}: 'all_answer_spans' is missing or empty in metadata") # Compute F1 score against all possible answer spans and take the maximum span_f1_scores = [self._compute_f1(pred, span) for span in all_spans] max_f1 = max(span_f1_scores) scores.append(max_f1) return mean(scores) def init_metrics(metrics: list[str], cache_dir: Path) -> None: if CodeExecMetricWithSandbox.name in metrics: if not is_sandbox_running(): raise RuntimeError( "Sandbox is not running. Please start the sandbox before using code_exec_sandbox metric." ) if "comet_wmt22" in metrics: create_comet_wmt22_scorer(resource_dir=cache_dir) if "mifeval_strict" in metrics or "mifeval_loose" in metrics: import nltk nltk.download("punkt_tab") def calculate_metrics( metric: str, y_preds: list[str], y_trues: list[str], xs: list[str] | None = None, samples_metadata: list[dict] | None = None, **kwargs, ) -> float: metric_func = MetricRegistry.get(metric) if isinstance(metric_func, ContextualBaseMetric): if xs is None: raise ValueError(f"Metric {metric} requires input context (xs)") return metric_func(xs, y_preds, y_trues, **kwargs) # Check if metric supports metadata if samples_metadata is not None and callable(metric_func): # Try to pass metadata if the metric function accepts it import inspect sig = inspect.signature(metric_func.__call__) if "samples_metadata" in sig.parameters: return metric_func(y_preds, y_trues, samples_metadata=samples_metadata, **kwargs) return metric_func(y_preds, y_trues, **kwargs) class TriviaQABaseMetric(BaseMetric): """Base class for TriviaQA metrics with common normalization function.""" def normalize_answer(self, text: str) -> str: """TriviaQA specific normalization function based on official evaluation script.""" # see: https://github.com/mandarjoshi90/triviaqa/blob/master/evaluation/triviaqa_evaluation.py#L14 text = text.lower() text = re.sub(r"\b(a|an|the)\b", " ", text) # remove articles exclude = set(string.punctuation + "".join([""", """, "´", "`"])) text = "".join(ch if ch not in exclude else " " for ch in text) text = text.replace("_", " ") return " ".join(text.split()).strip() @abstractmethod def score_func(self, prediction: str, ground_truth: str) -> float: """Calculate score between prediction and ground truth.""" pass def __call__( self, y_preds: list[str], y_trues: list[str], samples_metadata: list[dict] | None = None, **kwargs ) -> float: if samples_metadata is None: raise ValueError(f"samples_metadata must be provided for {self.name} metric") scores = [] for pred, true, metadata in zip(y_preds, y_trues, samples_metadata, strict=False): aliases = metadata.get("aliases", [true]) max_score = max(self.score_func(pred, alias) for alias in aliases) scores.append(max_score) return sum(scores) / len(scores) if scores else 0.0 class TriviaQAExactMatchMetric(TriviaQABaseMetric): name = "triviaqa_exact_match" def score_func(self, prediction: str, ground_truth: str) -> float: """Check if normalized prediction exactly matches normalized ground truth.""" return float(self.normalize_answer(prediction) == self.normalize_answer(ground_truth)) class TriviaQAF1Metric(TriviaQABaseMetric): name = "triviaqa_f1" def score_func(self, prediction: str, ground_truth: str) -> float: """Calculate token-level F1 score between prediction and ground truth.""" from collections import Counter pred_tokens = self.normalize_answer(prediction).split() true_tokens = self.normalize_answer(ground_truth).split() if not pred_tokens and not true_tokens: return 1.0 if not pred_tokens or not true_tokens: return 0.0 common = Counter(pred_tokens) & Counter(true_tokens) num_same = sum(common.values()) if num_same == 0: return 0.0 precision = num_same / len(pred_tokens) recall = num_same / len(true_tokens) return (2 * precision * recall) / (precision + recall) class HLEExactMatchMetric(MathmaticalEquivalenceMetric): """Base class for HLE metrics.""" name = "hle_exact_match" def __call__( self, y_preds: list[str], y_trues: list[str], samples_metadata: list[dict] | None = None, **kwargs ) -> float: if samples_metadata is None: raise ValueError(f"samples_metadata must be provided for {self.name} metric.") scores = [] for pred, true, metadata in zip(y_preds, y_trues, samples_metadata, strict=False): answer_type = metadata.get("answer_type", None) if answer_type == "math": scores.append(self._verify_mathematical_equivalence(pred, true)) elif answer_type == "multipleChoice" or answer_type == "exactMatch": scores.append(accuracy_score([true.lower()], [pred.lower()])) else: raise ValueError(f"answer_type must be provided for {self.name} metric.") return sum(scores) / len(scores) if scores else 0.0
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/metrics/rouge_ja.py
src/llm_jp_eval/metrics/rouge_ja.py
"""Rouge Score for Japanese ROUGE の演算は Google の rougescore を使用する。 (Rogue Scores という論文によると、Google の rougescore は Rouge の再実装として一部を除き正確に再現しているとのこと) https://aclanthology.org/2023.acl-long.107/ Tokenizer には sacrebleu で使用されている日本語 Tokenizer を使用する。 This script uses Google's rougescore for ROUGE calculation. (According to the paper "Rogue Scores", Google's rougescore accurately reproduces ROUGE except for some parts.) https://aclanthology.org/2023.acl-long.107/ This script uses the Japanese Tokenizer used in sacrebleu. """ from rouge_score.rouge_scorer import RougeScorer from rouge_score.scoring import AggregateScore, BootstrapAggregator, Score from sacrebleu.tokenizers.tokenizer_ja_mecab import TokenizerJaMecab class JapaneseWordTokenizer: def __init__(self): self.tokenizer = TokenizerJaMecab() def tokenize(self, text: str) -> list[str]: return self.tokenizer(text).strip().split() class JapaneseSentenceTokenizer: """Japanese Sentence Tokenizer For Rouge-L, the input text must be split into sentences. This class adds a newline character at the end of the sentence. """ punct = "。!?" def __init__(self): pass def add_new_line(self, text: str) -> str: """Add a newline character at the end of the sentence The Rouge-L needs to split the input text into sentences. If the input text includes a newline character, it will not be processed. """ if self._is_include_newline(text): return text for p in self.punct: text = text.replace(p, p + "\n") return text def _is_include_newline(self, text: str) -> bool: """Check if the text includes a newline character""" return "\n" in text class RougeJa: """Rouge Score for Japanese Examples: >> rouge = RougeJa(rouge_type=["rouge1", "rouge2", "rougeLsum"]) >> hypothesis = ["私は猫です。", "私は犬です。"] >> reference = ["私は犬です。", "私は猫です。"] >> rouge.score_system(hypothesis, reference) { 'rouge1': AggregateScore( low=Score(precision=50.0, recall=50.0, fmeasure=50.0), mid=Score(precision=50.0, recall=50.0, fmeasure=50.0), high=Score(precision=50.0, recall=50.0, fmeasure=50.0), ), 'rouge2': AggregateScore( low=Score(precision=50.0, recall=50.0, fmeasure=50.0), mid=Score(precision=50.0, recall=50.0, fmeasure=50.0), high=Score(precision=50.0, recall=50.0, fmeasure=50.0), } """ # Exclude Sentence Level Rouge because of Rogue Scores. supported_rouge_types = ["rouge1", "rouge2", "rougeLsum"] def __init__(self, rouge_type: list[str]): assert all(rouge in self.supported_rouge_types for rouge in rouge_type), ( f"rouge_type must be in {self.supported_rouge_types}" ) word_tokenizer = JapaneseWordTokenizer() self.sentence_tokenizer = JapaneseSentenceTokenizer() self.scorer = RougeScorer(rouge_types=rouge_type, tokenizer=word_tokenizer) def score_single(self, hypothesis: str, reference: str) -> dict[str, float]: """Calculate ROUGE scores for a single pair of hypothesis and reference As a preprocessing step, add a newline character at the end of the sentence. The Rouge-L needs to split the input text into sentences, so the input text includes a newline character. If not use Rouge-L, the word tokenizer strips the newline character. """ hypothesis = self.sentence_tokenizer.add_new_line(hypothesis) reference = self.sentence_tokenizer.add_new_line(reference) scores: dict[str, Score] = self.scorer.score(target=reference, prediction=hypothesis) for rouge_type, score in scores.items(): scores[rouge_type] = Score( precision=score.precision * 100, recall=score.recall * 100, fmeasure=score.fmeasure * 100, ) return scores def score_system(self, hypothesis: list[str], reference: list[str]) -> dict[str, AggregateScore]: """Aggregate ROUGE scores for a system's output. See also: https://github.com/google-research/google-research/blob/3b0ecae8f8ca15336319e403635ba487ad9dfeae/rouge/scoring.py#L61 """ assert len(hypothesis) == len(reference), "The number of hypothesis and reference must be the same." aggregator = BootstrapAggregator() for hyp, ref in zip(hypothesis, reference, strict=False): scores = self.score_single(hyp, ref) aggregator.add_scores(scores) return aggregator.aggregate()
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/metrics/__init__.py
src/llm_jp_eval/metrics/__init__.py
from .rouge_ja import RougeJa __all__ = ["RougeJa"]
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/metrics/sandbox_client.py
src/llm_jp_eval/metrics/sandbox_client.py
# simplified version of the code from # https://github.com/langgenius/dify/blob/main/api/core/helper/code_executor/code_executor.py import logging from enum import Enum from threading import Lock from typing import Optional from httpx import Timeout, get, post from pydantic import BaseModel from pydantic_settings import BaseSettings from yarl import URL logger = logging.getLogger(__name__) # defaut value from dify_config # https://github.com/langgenius/dify/blob/main/api/configs/feature/__init__.py class DifyConfig(BaseSettings): CODE_EXECUTION_ENDPOINT: str = "http://localhost:8194" CODE_EXECUTION_API_KEY: str = "dify-sandbox" CODE_EXECUTION_CONNECT_TIMEOUT: float = 10.0 CODE_EXECUTION_READ_TIMEOUT: float = 60.0 CODE_EXECUTION_WRITE_TIMEOUT: float = 60.0 dify_config = DifyConfig() class CodeExecutionServerError(Exception): pass class CodeExecutionError(Exception): pass class CodeExecutionResponse(BaseModel): class Data(BaseModel): stdout: Optional[str] = None error: Optional[str] = None code: int message: str data: Data class CodeLanguage(str, Enum): PYTHON3 = "python3" def is_sandbox_running() -> bool: """ Check if the sandbox is running :return: True if the sandbox is running, False otherwise """ try: response = get( str(URL(str(dify_config.CODE_EXECUTION_ENDPOINT)) / "health"), timeout=Timeout( connect=dify_config.CODE_EXECUTION_CONNECT_TIMEOUT, read=dify_config.CODE_EXECUTION_READ_TIMEOUT, write=dify_config.CODE_EXECUTION_WRITE_TIMEOUT, pool=None, ), ) return response.status_code == 200 except Exception as e: logger.error(f"Failed to check if the sandbox is running: {e}") return False class CodeExecutor: dependencies_cache: dict[str, str] = {} dependencies_cache_lock = Lock() code_language_to_running_language = { CodeLanguage.PYTHON3: CodeLanguage.PYTHON3, } supported_dependencies_languages: set[CodeLanguage] = {CodeLanguage.PYTHON3} @classmethod def execute_code(cls, language: CodeLanguage, preload: str, code: str) -> str: """ Execute code :param language: code language :param code: code :return: """ url = URL(str(dify_config.CODE_EXECUTION_ENDPOINT)) / "v1" / "sandbox" / "run" headers = {"X-Api-Key": dify_config.CODE_EXECUTION_API_KEY} data = { "language": cls.code_language_to_running_language.get(language), "code": code, "preload": preload, "enable_network": True, } try: response = post( str(url), json=data, headers=headers, timeout=Timeout( connect=dify_config.CODE_EXECUTION_CONNECT_TIMEOUT, read=dify_config.CODE_EXECUTION_READ_TIMEOUT, write=dify_config.CODE_EXECUTION_WRITE_TIMEOUT, pool=None, ), ) if response.status_code == 503: raise CodeExecutionError("Code execution service is unavailable") elif response.status_code != 200: raise Exception( f"Failed to execute code, got status code {response.status_code}," f" please check if the sandbox service is running" ) except CodeExecutionError as e: raise e except Exception as e: raise CodeExecutionServerError( "Failed to execute code, which is likely a network issue," " please check if the sandbox service is running." f" ( Error: {str(e)} )" ) from e try: response_data = response.json() except Exception as e: raise CodeExecutionError("Failed to parse response") from e if (code := response_data.get("code")) != 0: raise CodeExecutionError(f"Got error code: {code}. Got error msg: {response_data.get('message')}") response_code = CodeExecutionResponse(**response_data) if response_code.data.error: raise CodeExecutionError(response_code.data.error) return response_code.data.stdout or "" @classmethod def check_all_passed(cls, code: str, preload: str = "") -> bool: try: _ = cls.execute_code(CodeLanguage.PYTHON3, preload, code) return True except CodeExecutionError: return False
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false
llm-jp/llm-jp-eval
https://github.com/llm-jp/llm-jp-eval/blob/a5b71613317dfc051714585740f5a2be006ea1d0/src/llm_jp_eval/metrics/mifeval/instructions_registry.py
src/llm_jp_eval/metrics/mifeval/instructions_registry.py
# https://github.com/lightblue-tech/M-IFEval/blob/0bc3143a919eef2f9ec64aa26da8a546086653de/instructions_registry.py # type: ignore # coding=utf-8 # Copyright 2024 The Google Research Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Registry of all instructions.""" from llm_jp_eval.metrics.mifeval import en_instructions, ja_instructions _KEYWORD = "keywords:" _LANGUAGE = "language:" _LENGTH = "length_constraints:" _CONTENT = "detectable_content:" _FORMAT = "detectable_format:" _MULTITURN = "multi-turn:" _COMBINATION = "combination:" _STARTEND = "startend:" _CHANGE_CASES = "change_case:" _PUNCTUATION = "punctuation:" _LETTERS = "letters:" _SPECIAL_CHARACTER = "special_character:" EN_INSTRUCTION_DICT = { _KEYWORD + "existence": en_instructions.KeywordChecker, _KEYWORD + "frequency": en_instructions.KeywordFrequencyChecker, # TODO(jeffreyzhou): make a proper set of sentences to choose from # _KEYWORD + "key_sentences": instructions.KeySentenceChecker, _KEYWORD + "forbidden_words": en_instructions.ForbiddenWords, _KEYWORD + "letter_frequency": en_instructions.LetterFrequencyChecker, _LANGUAGE + "response_language": en_instructions.ResponseLanguageChecker, _LENGTH + "number_sentences": en_instructions.NumberOfSentences, _LENGTH + "number_paragraphs": en_instructions.ParagraphChecker, _LENGTH + "number_words": en_instructions.NumberOfWords, _LENGTH + "nth_paragraph_first_word": en_instructions.ParagraphFirstWordCheck, _CONTENT + "number_placeholders": en_instructions.PlaceholderChecker, _CONTENT + "postscript": en_instructions.PostscriptChecker, _FORMAT + "number_bullet_lists": en_instructions.BulletListChecker, # TODO(jeffreyzhou): Pre-create paragraph or use prompt to replace # _CONTENT + "rephrase_paragraph": instructions.RephraseParagraph, _FORMAT + "constrained_response": en_instructions.ConstrainedResponseChecker, _FORMAT + "number_highlighted_sections": (en_instructions.HighlightSectionChecker), _FORMAT + "multiple_sections": en_instructions.SectionChecker, # TODO(tianjianlu): Re-enable rephrasing with preprocessing the message. # _FORMAT + "rephrase": instructions.RephraseChecker, _FORMAT + "json_format": en_instructions.JsonFormat, _FORMAT + "title": en_instructions.TitleChecker, # TODO(tianjianlu): Re-enable with specific prompts. # _MULTITURN + "constrained_start": instructions.ConstrainedStartChecker, _COMBINATION + "two_responses": en_instructions.TwoResponsesChecker, _COMBINATION + "repeat_prompt": en_instructions.RepeatPromptThenAnswer, _STARTEND + "end_checker": en_instructions.EndChecker, _CHANGE_CASES + "capital_word_frequency": en_instructions.CapitalWordFrequencyChecker, _CHANGE_CASES + "english_capital": en_instructions.CapitalLettersEnglishChecker, _CHANGE_CASES + "english_lowercase": en_instructions.LowercaseLettersEnglishChecker, _PUNCTUATION + "no_comma": en_instructions.CommaChecker, _STARTEND + "quotation": en_instructions.QuotationChecker, } JA_INSTRUCTION_DICT = { _KEYWORD + "existence": ja_instructions.KeywordChecker, _KEYWORD + "frequency": ja_instructions.KeywordFrequencyChecker, # TODO(jeffreyzhou): make a proper set of sentences to choose from # _KEYWORD + "key_sentences": instructions.KeySentenceChecker, _KEYWORD + "forbidden_words": ja_instructions.ForbiddenWords, _KEYWORD + "letter_frequency": ja_instructions.LetterFrequencyChecker, _LANGUAGE + "response_language": ja_instructions.ResponseLanguageChecker, _LENGTH + "number_sentences": ja_instructions.NumberOfSentences, _LENGTH + "number_paragraphs": ja_instructions.ParagraphChecker, _LENGTH + "number_letters": ja_instructions.NumberOfLetters, _LENGTH + "nth_paragraph_first_word": ja_instructions.ParagraphFirstWordCheck, _CONTENT + "number_placeholders": ja_instructions.PlaceholderChecker, _CONTENT + "postscript": ja_instructions.PostscriptChecker, _FORMAT + "number_bullet_lists": ja_instructions.BulletListChecker, _FORMAT + "number_numbered_lists": ja_instructions.NumberedListChecker, # TODO(jeffreyzhou): Pre-create paragraph or use prompt to replace # _CONTENT + "rephrase_paragraph": instructions.RephraseParagraph, _FORMAT + "constrained_response": ja_instructions.ConstrainedResponseChecker, _FORMAT + "number_highlighted_sections": (ja_instructions.HighlightSectionChecker), _FORMAT + "multiple_sections": ja_instructions.SectionChecker, # TODO(tianjianlu): Re-enable rephrasing with preprocessing the message. # _FORMAT + "rephrase": instructions.RephraseChecker, _FORMAT + "json_format": ja_instructions.JsonFormat, _FORMAT + "title": ja_instructions.TitleChecker, # TODO(tianjianlu): Re-enable with specific prompts. # _MULTITURN + "constrained_start": instructions.ConstrainedStartChecker, _COMBINATION + "two_responses": ja_instructions.TwoResponsesChecker, _COMBINATION + "repeat_prompt": ja_instructions.RepeatPromptThenAnswer, _STARTEND + "end_checker": ja_instructions.EndChecker, _STARTEND + "sentence_unified_end": ja_instructions.SentenceEndingUnification, _FORMAT + "nominal_ending": ja_instructions.NominalEndingChecker, _LETTERS + "furigana": ja_instructions.FuriganaForKanji, _LETTERS + "kansuuji": ja_instructions.KanjiNumberNotationChecker, _LETTERS + "no_katakana": ja_instructions.NoKatakana, _LETTERS + "katakana_only": ja_instructions.KatakanaOnly, _LETTERS + "no_hiragana": ja_instructions.NoHiragana, _LETTERS + "hiragana_only": ja_instructions.HiraganaOnly, _LETTERS + "kanji": ja_instructions.KanjiLimit, _PUNCTUATION + "no_comma": ja_instructions.CommaChecker, _PUNCTUATION + "no_period": ja_instructions.PeriodChecker, _STARTEND + "quotation": ja_instructions.QuotationChecker, } INSTRUCTION_DICT = {} INSTRUCTION_DICT.update({"en:" + k: v for k, v in EN_INSTRUCTION_DICT.items()}) INSTRUCTION_DICT.update({"ja:" + k: v for k, v in JA_INSTRUCTION_DICT.items()})
python
Apache-2.0
a5b71613317dfc051714585740f5a2be006ea1d0
2026-01-05T07:13:14.125338Z
false