A framework for LLM-powered interactions
Project description
longchain
Usage
Quest
- The main object. Ititialize it, then callasyncio.run(quest.run())
to run it.Path
- A Path is one "step" in a player's experience.ActionResolver
- This is the real decision-making part of thePath
. The two main ones are theSequentialActionResolver
, which takes severalAction
s and just runs them in order, and theLlmActionResolver
, which uses an LLM to decide what to do.starts_without_player_action
- If this parameter isTrue
, then aChangePathAction
withnext_action='path'
can immediately call thisActionResolver
without waiting for player input.
AgentAction
- This is a single step a path takes. It's something like sending a message or moving to another path. They have anext_action
parameter which controls whether the player is prompted for an input or not before the next action happens.core
vsimpl
vsplugins
-core
is the main part of the library,impl
is the implementation of thecore
interfaces, andplugins
are more niche implementations. For example, the path following code is incore
, theLlmActionResolver
is inimpl
, and the Bag utilities are inplugins
.Datastore
- This is an interface for the quest to keep player state. The only one currently implemented isjsonfile
, but another one could be implemented for a database or something.Messager
- Like Datastore, this is an abstraction layer that allows the quest to send messages to the player. The only one currently implemented isslack
.LlmActionResolver
- There's a lot here so it gets its own bullet point. The LLM automatically talks to the player, and you can addLlmTool
s in theagent_actions
parameter to allow it to do more. Thesystem_prompt
parameter can either be a string or a callable taking anLlmContext
. This allows the system prompt to change based on what's going on, which is recommended (LLMs are not good at keeping information secret).LlmTool
- These describe an action the LLM can take.description
should tell the LLM when to call the tool.params
is a list ofLlmToolParam
s, which have aname
and aschema
(which is a JSON Schema object).available
andaction
are callables that take anLlmContext
and return a boolean and anAgentAction
respectively.available
should returnTrue
if the tool can be used, andaction
should return theAgentAction
to take if the tool is used. These both getLlmContext
s, andaction
also gets adict
of the params the LLM called the action with.strict
can turn on structured output, which forces the LLM's output to conform to theparams
perfectly but disables some JSON Schema properties. See more in the OpenAI docs.. Ifstrict
is off, don't trust the LLM to call your function correctly.
Example
There is an example project in the examples folder.
Development
to build:
.\venv\Scripts\activate
py -m build
python -m twine upload dist/*
to regenerate the bag api:
- download the update bag.proto
pip install grpcio grpcio_tools
- run
cd .\src\longchain\plugins\bag\api\
- run
python -m grpc_tools.protoc -I . --grpc_python_out . --python_out . --pyi_out . bag.proto
- Go to the generated bag_pb2_grpc.py and change the
bag_pb2
import toimport longchain.plugins.bag.api.bag_pb2 as bag__pb2
Naming
It's a tool for making long chains of NPC interactions.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
longchain-0.4.2.tar.gz
(29.6 kB
view details)
Built Distribution
longchain-0.4.2-py3-none-any.whl
(33.3 kB
view details)
File details
Details for the file longchain-0.4.2.tar.gz
.
File metadata
- Download URL: longchain-0.4.2.tar.gz
- Upload date:
- Size: 29.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb31d809ad3a863e387f5bd2b0ee682a291649429efe5d57a3353fdb90367e99 |
|
MD5 | e357e80b701e123bcf5b1483870924ec |
|
BLAKE2b-256 | 2dc2ac5b6c5b71d4a5d61c757cb1f60e8c40c743e670543c91db49dc58e865c5 |
File details
Details for the file longchain-0.4.2-py3-none-any.whl
.
File metadata
- Download URL: longchain-0.4.2-py3-none-any.whl
- Upload date:
- Size: 33.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72fd681c74de3099fb9168ce1edfd80bc7cbdb259f47ca9958910567e90d68f9 |
|
MD5 | efbf9826be999d3cd9675eeab6a18762 |
|
BLAKE2b-256 | 9b496e3ba84a1954f8cecbb271b3cff66153531611038351949e35933dc4f01d |