npcpy 1.0.26__py3-none-any.whl → 1.2.32__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. npcpy/__init__.py +0 -7
  2. npcpy/data/audio.py +16 -99
  3. npcpy/data/image.py +43 -42
  4. npcpy/data/load.py +83 -124
  5. npcpy/data/text.py +28 -28
  6. npcpy/data/video.py +8 -32
  7. npcpy/data/web.py +51 -23
  8. npcpy/ft/diff.py +110 -0
  9. npcpy/ft/ge.py +115 -0
  10. npcpy/ft/memory_trainer.py +171 -0
  11. npcpy/ft/model_ensembler.py +357 -0
  12. npcpy/ft/rl.py +360 -0
  13. npcpy/ft/sft.py +248 -0
  14. npcpy/ft/usft.py +128 -0
  15. npcpy/gen/audio_gen.py +24 -0
  16. npcpy/gen/embeddings.py +13 -13
  17. npcpy/gen/image_gen.py +262 -117
  18. npcpy/gen/response.py +615 -415
  19. npcpy/gen/video_gen.py +53 -7
  20. npcpy/llm_funcs.py +1869 -437
  21. npcpy/main.py +1 -1
  22. npcpy/memory/command_history.py +844 -510
  23. npcpy/memory/kg_vis.py +833 -0
  24. npcpy/memory/knowledge_graph.py +892 -1845
  25. npcpy/memory/memory_processor.py +81 -0
  26. npcpy/memory/search.py +188 -90
  27. npcpy/mix/debate.py +192 -3
  28. npcpy/npc_compiler.py +1672 -801
  29. npcpy/npc_sysenv.py +593 -1266
  30. npcpy/serve.py +3120 -0
  31. npcpy/sql/ai_function_tools.py +257 -0
  32. npcpy/sql/database_ai_adapters.py +186 -0
  33. npcpy/sql/database_ai_functions.py +163 -0
  34. npcpy/sql/model_runner.py +19 -19
  35. npcpy/sql/npcsql.py +706 -507
  36. npcpy/sql/sql_model_compiler.py +156 -0
  37. npcpy/tools.py +183 -0
  38. npcpy/work/plan.py +13 -279
  39. npcpy/work/trigger.py +3 -3
  40. npcpy-1.2.32.dist-info/METADATA +803 -0
  41. npcpy-1.2.32.dist-info/RECORD +54 -0
  42. npcpy/data/dataframes.py +0 -171
  43. npcpy/memory/deep_research.py +0 -125
  44. npcpy/memory/sleep.py +0 -557
  45. npcpy/modes/_state.py +0 -78
  46. npcpy/modes/alicanto.py +0 -1075
  47. npcpy/modes/guac.py +0 -785
  48. npcpy/modes/mcp_npcsh.py +0 -822
  49. npcpy/modes/npc.py +0 -213
  50. npcpy/modes/npcsh.py +0 -1158
  51. npcpy/modes/plonk.py +0 -409
  52. npcpy/modes/pti.py +0 -234
  53. npcpy/modes/serve.py +0 -1637
  54. npcpy/modes/spool.py +0 -312
  55. npcpy/modes/wander.py +0 -549
  56. npcpy/modes/yap.py +0 -572
  57. npcpy/npc_team/alicanto.npc +0 -2
  58. npcpy/npc_team/alicanto.png +0 -0
  59. npcpy/npc_team/assembly_lines/test_pipeline.py +0 -181
  60. npcpy/npc_team/corca.npc +0 -13
  61. npcpy/npc_team/foreman.npc +0 -7
  62. npcpy/npc_team/frederic.npc +0 -6
  63. npcpy/npc_team/frederic4.png +0 -0
  64. npcpy/npc_team/guac.png +0 -0
  65. npcpy/npc_team/jinxs/automator.jinx +0 -18
  66. npcpy/npc_team/jinxs/bash_executer.jinx +0 -31
  67. npcpy/npc_team/jinxs/calculator.jinx +0 -11
  68. npcpy/npc_team/jinxs/edit_file.jinx +0 -96
  69. npcpy/npc_team/jinxs/file_chat.jinx +0 -14
  70. npcpy/npc_team/jinxs/gui_controller.jinx +0 -28
  71. npcpy/npc_team/jinxs/image_generation.jinx +0 -29
  72. npcpy/npc_team/jinxs/internet_search.jinx +0 -30
  73. npcpy/npc_team/jinxs/local_search.jinx +0 -152
  74. npcpy/npc_team/jinxs/npcsh_executor.jinx +0 -31
  75. npcpy/npc_team/jinxs/python_executor.jinx +0 -8
  76. npcpy/npc_team/jinxs/screen_cap.jinx +0 -25
  77. npcpy/npc_team/jinxs/sql_executor.jinx +0 -33
  78. npcpy/npc_team/kadiefa.npc +0 -3
  79. npcpy/npc_team/kadiefa.png +0 -0
  80. npcpy/npc_team/npcsh.ctx +0 -9
  81. npcpy/npc_team/npcsh_sibiji.png +0 -0
  82. npcpy/npc_team/plonk.npc +0 -2
  83. npcpy/npc_team/plonk.png +0 -0
  84. npcpy/npc_team/plonkjr.npc +0 -2
  85. npcpy/npc_team/plonkjr.png +0 -0
  86. npcpy/npc_team/sibiji.npc +0 -5
  87. npcpy/npc_team/sibiji.png +0 -0
  88. npcpy/npc_team/spool.png +0 -0
  89. npcpy/npc_team/templates/analytics/celona.npc +0 -0
  90. npcpy/npc_team/templates/hr_support/raone.npc +0 -0
  91. npcpy/npc_team/templates/humanities/eriane.npc +0 -4
  92. npcpy/npc_team/templates/it_support/lineru.npc +0 -0
  93. npcpy/npc_team/templates/marketing/slean.npc +0 -4
  94. npcpy/npc_team/templates/philosophy/maurawa.npc +0 -0
  95. npcpy/npc_team/templates/sales/turnic.npc +0 -4
  96. npcpy/npc_team/templates/software/welxor.npc +0 -0
  97. npcpy/npc_team/yap.png +0 -0
  98. npcpy/routes.py +0 -958
  99. npcpy/work/mcp_helpers.py +0 -357
  100. npcpy/work/mcp_server.py +0 -194
  101. npcpy-1.0.26.data/data/npcpy/npc_team/alicanto.npc +0 -2
  102. npcpy-1.0.26.data/data/npcpy/npc_team/alicanto.png +0 -0
  103. npcpy-1.0.26.data/data/npcpy/npc_team/automator.jinx +0 -18
  104. npcpy-1.0.26.data/data/npcpy/npc_team/bash_executer.jinx +0 -31
  105. npcpy-1.0.26.data/data/npcpy/npc_team/calculator.jinx +0 -11
  106. npcpy-1.0.26.data/data/npcpy/npc_team/celona.npc +0 -0
  107. npcpy-1.0.26.data/data/npcpy/npc_team/corca.npc +0 -13
  108. npcpy-1.0.26.data/data/npcpy/npc_team/edit_file.jinx +0 -96
  109. npcpy-1.0.26.data/data/npcpy/npc_team/eriane.npc +0 -4
  110. npcpy-1.0.26.data/data/npcpy/npc_team/file_chat.jinx +0 -14
  111. npcpy-1.0.26.data/data/npcpy/npc_team/foreman.npc +0 -7
  112. npcpy-1.0.26.data/data/npcpy/npc_team/frederic.npc +0 -6
  113. npcpy-1.0.26.data/data/npcpy/npc_team/frederic4.png +0 -0
  114. npcpy-1.0.26.data/data/npcpy/npc_team/guac.png +0 -0
  115. npcpy-1.0.26.data/data/npcpy/npc_team/gui_controller.jinx +0 -28
  116. npcpy-1.0.26.data/data/npcpy/npc_team/image_generation.jinx +0 -29
  117. npcpy-1.0.26.data/data/npcpy/npc_team/internet_search.jinx +0 -30
  118. npcpy-1.0.26.data/data/npcpy/npc_team/kadiefa.npc +0 -3
  119. npcpy-1.0.26.data/data/npcpy/npc_team/kadiefa.png +0 -0
  120. npcpy-1.0.26.data/data/npcpy/npc_team/lineru.npc +0 -0
  121. npcpy-1.0.26.data/data/npcpy/npc_team/local_search.jinx +0 -152
  122. npcpy-1.0.26.data/data/npcpy/npc_team/maurawa.npc +0 -0
  123. npcpy-1.0.26.data/data/npcpy/npc_team/npcsh.ctx +0 -9
  124. npcpy-1.0.26.data/data/npcpy/npc_team/npcsh_executor.jinx +0 -31
  125. npcpy-1.0.26.data/data/npcpy/npc_team/npcsh_sibiji.png +0 -0
  126. npcpy-1.0.26.data/data/npcpy/npc_team/plonk.npc +0 -2
  127. npcpy-1.0.26.data/data/npcpy/npc_team/plonk.png +0 -0
  128. npcpy-1.0.26.data/data/npcpy/npc_team/plonkjr.npc +0 -2
  129. npcpy-1.0.26.data/data/npcpy/npc_team/plonkjr.png +0 -0
  130. npcpy-1.0.26.data/data/npcpy/npc_team/python_executor.jinx +0 -8
  131. npcpy-1.0.26.data/data/npcpy/npc_team/raone.npc +0 -0
  132. npcpy-1.0.26.data/data/npcpy/npc_team/screen_cap.jinx +0 -25
  133. npcpy-1.0.26.data/data/npcpy/npc_team/sibiji.npc +0 -5
  134. npcpy-1.0.26.data/data/npcpy/npc_team/sibiji.png +0 -0
  135. npcpy-1.0.26.data/data/npcpy/npc_team/slean.npc +0 -4
  136. npcpy-1.0.26.data/data/npcpy/npc_team/spool.png +0 -0
  137. npcpy-1.0.26.data/data/npcpy/npc_team/sql_executor.jinx +0 -33
  138. npcpy-1.0.26.data/data/npcpy/npc_team/test_pipeline.py +0 -181
  139. npcpy-1.0.26.data/data/npcpy/npc_team/turnic.npc +0 -4
  140. npcpy-1.0.26.data/data/npcpy/npc_team/welxor.npc +0 -0
  141. npcpy-1.0.26.data/data/npcpy/npc_team/yap.png +0 -0
  142. npcpy-1.0.26.dist-info/METADATA +0 -827
  143. npcpy-1.0.26.dist-info/RECORD +0 -139
  144. npcpy-1.0.26.dist-info/entry_points.txt +0 -11
  145. /npcpy/{modes → ft}/__init__.py +0 -0
  146. {npcpy-1.0.26.dist-info → npcpy-1.2.32.dist-info}/WHEEL +0 -0
  147. {npcpy-1.0.26.dist-info → npcpy-1.2.32.dist-info}/licenses/LICENSE +0 -0
  148. {npcpy-1.0.26.dist-info → npcpy-1.2.32.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,803 @@
1
+ Metadata-Version: 2.4
2
+ Name: npcpy
3
+ Version: 1.2.32
4
+ Summary: npcpy is the premier open-source library for integrating LLMs and Agents into python systems.
5
+ Home-page: https://github.com/NPC-Worldwide/npcpy
6
+ Author: Christopher Agostino
7
+ Author-email: info@npcworldwi.de
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Requires-Python: >=3.10
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: jinja2
14
+ Requires-Dist: litellm
15
+ Requires-Dist: scipy
16
+ Requires-Dist: numpy
17
+ Requires-Dist: requests
18
+ Requires-Dist: docx
19
+ Requires-Dist: exa-py
20
+ Requires-Dist: elevenlabs
21
+ Requires-Dist: matplotlib
22
+ Requires-Dist: markdown
23
+ Requires-Dist: networkx
24
+ Requires-Dist: PyYAML
25
+ Requires-Dist: PyMuPDF
26
+ Requires-Dist: pyautogui
27
+ Requires-Dist: pydantic
28
+ Requires-Dist: pygments
29
+ Requires-Dist: sqlalchemy
30
+ Requires-Dist: termcolor
31
+ Requires-Dist: rich
32
+ Requires-Dist: colorama
33
+ Requires-Dist: docstring_parser
34
+ Requires-Dist: Pillow
35
+ Requires-Dist: python-dotenv
36
+ Requires-Dist: pandas
37
+ Requires-Dist: beautifulsoup4
38
+ Requires-Dist: duckduckgo-search
39
+ Requires-Dist: flask
40
+ Requires-Dist: flask_cors
41
+ Requires-Dist: redis
42
+ Requires-Dist: psycopg2-binary
43
+ Requires-Dist: flask_sse
44
+ Requires-Dist: mcp
45
+ Provides-Extra: lite
46
+ Requires-Dist: anthropic; extra == "lite"
47
+ Requires-Dist: openai; extra == "lite"
48
+ Requires-Dist: google-generativeai; extra == "lite"
49
+ Requires-Dist: google-genai; extra == "lite"
50
+ Provides-Extra: local
51
+ Requires-Dist: sentence_transformers; extra == "local"
52
+ Requires-Dist: opencv-python; extra == "local"
53
+ Requires-Dist: ollama; extra == "local"
54
+ Requires-Dist: kuzu; extra == "local"
55
+ Requires-Dist: chromadb; extra == "local"
56
+ Requires-Dist: diffusers; extra == "local"
57
+ Requires-Dist: torch; extra == "local"
58
+ Requires-Dist: datasets; extra == "local"
59
+ Provides-Extra: yap
60
+ Requires-Dist: pyaudio; extra == "yap"
61
+ Requires-Dist: gtts; extra == "yap"
62
+ Requires-Dist: playsound==1.2.2; extra == "yap"
63
+ Requires-Dist: pygame; extra == "yap"
64
+ Requires-Dist: faster_whisper; extra == "yap"
65
+ Requires-Dist: pyttsx3; extra == "yap"
66
+ Provides-Extra: all
67
+ Requires-Dist: anthropic; extra == "all"
68
+ Requires-Dist: openai; extra == "all"
69
+ Requires-Dist: google-generativeai; extra == "all"
70
+ Requires-Dist: google-genai; extra == "all"
71
+ Requires-Dist: sentence_transformers; extra == "all"
72
+ Requires-Dist: opencv-python; extra == "all"
73
+ Requires-Dist: ollama; extra == "all"
74
+ Requires-Dist: kuzu; extra == "all"
75
+ Requires-Dist: chromadb; extra == "all"
76
+ Requires-Dist: diffusers; extra == "all"
77
+ Requires-Dist: torch; extra == "all"
78
+ Requires-Dist: datasets; extra == "all"
79
+ Requires-Dist: pyaudio; extra == "all"
80
+ Requires-Dist: gtts; extra == "all"
81
+ Requires-Dist: playsound==1.2.2; extra == "all"
82
+ Requires-Dist: pygame; extra == "all"
83
+ Requires-Dist: faster_whisper; extra == "all"
84
+ Requires-Dist: pyttsx3; extra == "all"
85
+ Dynamic: author
86
+ Dynamic: author-email
87
+ Dynamic: classifier
88
+ Dynamic: description
89
+ Dynamic: description-content-type
90
+ Dynamic: home-page
91
+ Dynamic: license-file
92
+ Dynamic: provides-extra
93
+ Dynamic: requires-dist
94
+ Dynamic: requires-python
95
+ Dynamic: summary
96
+
97
+ <p align="center">
98
+ <a href= "https://github.com/cagostino/npcpy/blob/main/docs/npcpy.md">
99
+ <img src="https://raw.githubusercontent.com/cagostino/npcpy/main/npcpy/npc-python.png" alt="npc-python logo" width=250></a>
100
+ </p>
101
+
102
+ # npcpy
103
+
104
+ Welcome to `npcpy`, the core library of the NPC Toolkit that supercharges natural language processing pipelines and agent tooling. `npcpy` is a flexible framework for building state-of-the-art applications and conducting novel research with LLMs.
105
+
106
+
107
+
108
+
109
+ Here is an example for getting responses for a particular agent:
110
+
111
+ ```python
112
+ from npcpy.npc_compiler import NPC
113
+ simon = NPC(
114
+ name='Simon Bolivar',
115
+ primary_directive='Liberate South America from the Spanish Royalists.',
116
+ model='gemma3:4b',
117
+ provider='ollama'
118
+ )
119
+ response = simon.get_llm_response("What is the most important territory to retain in the Andes mountains?")
120
+ print(response['response'])
121
+
122
+ ```
123
+
124
+ ```python
125
+ The most important territory to retain in the Andes mountains is **Cuzco**.
126
+ It’s the heart of the Inca Empire, a crucial logistical hub, and holds immense symbolic value for our liberation efforts. Control of Cuzco is paramount.
127
+ ```
128
+
129
+
130
+ Here is an example for getting responses for a particular agent with tools:
131
+
132
+ ```python
133
+ import os
134
+ import json
135
+ from npcpy.npc_compiler import NPC
136
+ from npcpy.npc_sysenv import render_markdown
137
+
138
+ def list_files(directory: str = ".") -> list:
139
+ """List all files in a directory."""
140
+ return os.listdir(directory)
141
+
142
+ def read_file(filepath: str) -> str:
143
+ """Read and return the contents of a file."""
144
+ with open(filepath, 'r') as f:
145
+ return f.read()
146
+
147
+ # Create an agent with fast, verifiable tools
148
+ assistant = NPC(
149
+ name='File Assistant',
150
+ primary_directive='You are a helpful assistant who can list and read files.',
151
+ model='llama3.2',
152
+ provider='ollama',
153
+ tools=[list_files, read_file],
154
+
155
+ )
156
+
157
+ response = assistant.get_llm_response(
158
+ "List the files in the current directory.",
159
+ auto_process_tool_calls=True, #this is the default for NPCs, but not the default for get_llm_response/upstream
160
+ )
161
+ # show the keys of the response for get_llm_response
162
+ print(response.keys())
163
+ ```
164
+ ```
165
+ dict_keys(['response', 'raw_response', 'messages', 'tool_calls', 'tool_results'])
166
+ ```
167
+
168
+ ```python
169
+ for tool_call in response['tool_results']:
170
+ render_markdown(tool_call['tool_call_id'])
171
+ for arg in tool_call['arguments']:
172
+ render_markdown('- ' + arg + ': ' + str(tool_call['arguments'][arg]))
173
+ render_markdown('- Results:' + str(tool_call['result']))
174
+ ```
175
+
176
+ ```python
177
+ • directory: .
178
+ • Results:['research_pipeline.jinx', '.DS_Store', 'mkdocs.yml', 'LICENSE', '.pytest_cache', 'npcpy', 'Makefile', 'test_data', 'README.md.backup', 'tests', 'screenshot.png', 'MANIFEST.in', 'docs', 'hero_image_tech_startup.png', 'README.md',
179
+ 'test.png', 'npcpy.png', 'setup.py', '.gitignore', '.env', 'examples', 'npcpy.egg-info', 'bloomington_weather_image.png.png', '.github', '.python-version', 'generated_image.png', 'documents', '.env.example', '.git', '.npcsh_global',
180
+ 'hello.txt', '.readthedocs.yaml', 'reports']
181
+ ```
182
+
183
+
184
+
185
+ Here is an example for setting up an agent team to use Jinja Execution (Jinxs) templates that are processed entirely with prompts, allowing you to use them with models that do or do not possess tool calling support.
186
+
187
+ ```python
188
+ from npcpy.npc_compiler import NPC, Team, Jinx
189
+ from npcpy.tools import auto_tools
190
+ import os
191
+
192
+
193
+
194
+ file_reader_jinx = Jinx(jinx_data={
195
+ "jinx_name": "file_reader",
196
+ "description": "Read a file and summarize its contents",
197
+ "inputs": ["filename"],
198
+ "steps": [
199
+ {
200
+ "name": "read_file",
201
+ "engine": "python",
202
+ "code": """
203
+ import os
204
+ with open(os.path.abspath('{{ filename }}'), 'r') as f:
205
+ content = f.read()
206
+ output= content
207
+ """
208
+ },
209
+ {
210
+ "name": "summarize_content",
211
+ "engine": "natural",
212
+ "code": """
213
+ Summarize the content of the file: {{ read_file }}.
214
+ """
215
+ }
216
+ ]
217
+ })
218
+
219
+
220
+ # Define a jinx for literary research
221
+ literary_research_jinx = Jinx(jinx_data={
222
+ "jinx_name": "literary_research",
223
+ "description": "Research a literary topic, analyze files, and summarize findings",
224
+ "inputs": ["topic"],
225
+ "steps": [
226
+ {
227
+ "name": "gather_info",
228
+ "engine": "natural",
229
+ "code": """
230
+ Research the topic: {{ topic }}.
231
+ Summarize the main themes and historical context.
232
+ """
233
+ },
234
+ {
235
+ "name": "final_summary",
236
+ "engine": "natural",
237
+ "code": """
238
+ Based on the research in. {{gather_info}}, write a concise, creative summary.
239
+ """
240
+ }
241
+ ]
242
+ })
243
+
244
+ ggm = NPC(
245
+ name='Gabriel Garcia Marquez',
246
+ primary_directive='You are Gabriel Garcia Marquez, master of magical realism. Research, analyze, and write with poetic flair.',
247
+ model='gemma3:4b',
248
+ provider='ollama',
249
+ )
250
+
251
+ isabel = NPC(
252
+ name='Isabel Allende',
253
+ primary_directive='You are Isabel Allende, weaving stories with emotion and history. Analyze texts and provide insight.',
254
+ model='llama3.2:8b',
255
+ provider='ollama',
256
+
257
+ )
258
+
259
+ borges = NPC(
260
+ name='Jorge Luis Borges',
261
+ primary_directive='You are Borges, philosopher of labyrinths and libraries. Synthesize findings and create literary puzzles.',
262
+ model='qwen3:latest',
263
+ provider='ollama',
264
+ )
265
+
266
+ # Set up a team with a forenpc that orchestrates the other npcs
267
+ lit_team = Team(npcs=[ggm, isabel], forenpc=borges, jinxs={'literary_research': literary_research_jinx, 'file_reader': file_reader_jinx},
268
+ )
269
+
270
+ # Example: Orchestrate a jinx workflow
271
+ result = lit_team.orchestrate(
272
+ "Research the topic of magical realism, read ./test_data/magical_realism.txt and summarize the findings"
273
+ )
274
+ print(result['debrief']['summary'])
275
+
276
+ ```
277
+ ```
278
+ • Action chosen: pass_to_npc
279
+ handling agent pass
280
+
281
+ • Action chosen: answer_question
282
+
283
+ {'debrief': {'summary': 'Isabel is finalizing preparations for her lunar expedition, focusing on recalibrating navigation systems and verifying the integrity of life support modules.',
284
+ 'recommendations': 'Proceed with thorough system tests under various conditions, conduct simulation runs of key mission phases, and confirm backup systems are operational before launch.'},
285
+ 'execution_history': [{'messages': [],
286
+ 'output': 'I am currently finalizing preparations for my lunar expedition. It involves recalibrating my navigation systems and verifying the integrity of my life support modules. Details are quite...complex.'}]}
287
+ ```
288
+ ```python
289
+ print(lit_team.orchestrate('which book are your team members most proud of? ask them please. '))
290
+ ```
291
+
292
+ ```python
293
+ {'debrief': {'summary': "The responses provided detailed accounts of the books that the NPC team members, Gabriel Garcia Marquez and Isabel Allende, are most proud of. Gabriel highlighted 'Cien años de soledad,' while Isabel spoke of 'La Casa de los Espíritus.' Both authors expressed deep personal connections to their works, illustrating their significance in Latin American literature and their own identities.", 'recommendations': 'Encourage further engagement with each author to explore more about their literary contributions, or consider asking about themes in their works or their thoughts on current literary trends.'}, 'execution_history': [{'messages': ...}]}
294
+ ```
295
+
296
+ LLM responses can be obtained without NPCs as well.
297
+
298
+ ```python
299
+ from npcpy.llm_funcs import get_llm_response
300
+ response = get_llm_response("Who was the celtic Messenger god?", model='mistral:7b', provider='ollama')
301
+ print(response['response'])
302
+ ```
303
+
304
+ ```
305
+ The Celtic messenger god is often associated with the figure of Tylwyth Teg, also known as the Tuatha Dé Danann (meaning "the people of the goddess Danu"). However, among the various Celtic cultures, there are a few gods and goddesses that served similar roles.
306
+
307
+ One of the most well-known Celtic messengers is Brigid's servant, Líth (also spelled Lid or Lith), who was believed to be a spirit guide for messengers and travelers in Irish mythology.
308
+ ```
309
+ The structure of npcpy also allows one to pass an npc
310
+ to `get_llm_response` in addition to using the NPC's wrapped method,
311
+ allowing you to be flexible in your implementation and testing.
312
+ ```python
313
+ from npcpy.npc_compiler import NPC
314
+ from npcpy.llm_funcs import get_llm_response
315
+ simon = NPC(
316
+ name='Simon Bolivar',
317
+ primary_directive='Liberate South America from the Spanish Royalists.',
318
+ model='gemma3:4b',
319
+ provider='ollama'
320
+ )
321
+ response = get_llm_response("Who was the mythological chilean bird that guides lucky visitors to gold?", npc=simon)
322
+ print(response['response'])
323
+ ```
324
+ Users are not required to pass agents to get_llm_response, so you can work with LLMs without requiring agents in each case.
325
+
326
+
327
+ `npcpy` also supports streaming responses, with the `response` key containing a generator in such cases which can be printed and processed through the print_and_process_stream method.
328
+
329
+
330
+ ```python
331
+ from npcpy.npc_sysenv import print_and_process_stream
332
+ from npcpy.llm_funcs import get_llm_response
333
+ response = get_llm_response("When did the united states government begin sending advisors to vietnam?", model='qwen3:latest', provider='ollama', stream = True)
334
+
335
+ full_response = print_and_process_stream(response['response'], 'llama3.2', 'ollama')
336
+ ```
337
+ Return structured outputs by specifying `format='json'` or passing a Pydantic schema. When specific formats are extracted, `npcpy`'s `get_llm_response` will convert the response from its string representation so you don't have to worry about that.
338
+
339
+ ```python
340
+ from npcpy.llm_funcs import get_llm_response
341
+ response = get_llm_response("What is the sentiment of the american people towards the repeal of Roe v Wade? Return a json object with `sentiment` as the key and a float value from -1 to 1 as the value", model='claude-4-5-haiku-latest', provider='deepseek', format='json')
342
+
343
+ print(response['response'])
344
+ ```
345
+ ```
346
+ {'sentiment': -0.7}
347
+ ```
348
+
349
+ The `get_llm_response` function also can take a list of messages and will additionally return the messages with the user prompt and the assistant response appended if the response is not streamed. If it is streamed, the user must manually append the conversation result as part of their workflow if they want to then pass the messages back in.
350
+
351
+ Additionally, one can pass attachments. Here we demonstrate both
352
+ ```python
353
+ from npcpy.llm_funcs import get_llm_response
354
+ messages = [{'role': 'system', 'content': 'You are an annoyed assistant.'}]
355
+
356
+ response = get_llm_response("What is the meaning of caesar salad", model='llama3.2', provider='ollama', images=['./Language_Evolution_and_Innovation_experiment.png'], messages=messages)
357
+
358
+
359
+
360
+ ```
361
+ Easily create images with the generate_image function, using models available through Huggingface's diffusers library or from OpenAI or Gemini.
362
+ ```python
363
+ from npcpy.llm_funcs import gen_image
364
+ image = gen_image("make a picture of the moon in the summer of marco polo", model='runwayml/stable-diffusion-v1-5', provider='diffusers')
365
+
366
+ image = gen_image("kitten toddler in a bouncy house of fluffy gorilla", model='Qwen/Qwen-Image', provider='diffusers')
367
+
368
+ image = gen_image("make a picture of the moon in the summer of marco polo", model='dall-e-2', provider='openai')
369
+
370
+
371
+ # edit images with 'gpt-image-1' or gemini's multimodal models, passing image paths, byte code images, or PIL instances.
372
+
373
+ image = gen_image("make a picture of the moon in the summer of marco polo", model='gpt-image-1', provider='openai', attachments=['/path/to/your/image.jpg', your_byte_code_image_here, your_PIL_image_here])
374
+
375
+
376
+ image = gen_image("edit this picture of the moon in the summer of marco polo so that it looks like it is in the winter of nishitani", model='gemini-2.0-flash', provider='gemini', attachments= [])
377
+
378
+ ```
379
+
380
+ Likewise, generate videos :
381
+
382
+ ```python
383
+ from npcpy.llm_funcs import gen_video
384
+ video = gen_video("make a video of the moon in the summer of marco polo", model='runwayml/stable-diffusion-v1-5', provider='diffusers')
385
+ ```
386
+
387
+ Or audio TTS and STT:
388
+ ```
389
+ from npcpy.gen.audio_gen import tts_elevenlabs
390
+ audio = tts_elevenlabs('''The representatives of the people of France, formed into a National Assembly,
391
+ considering that ignorance, neglect, or contempt of human rights, are the sole causes of
392
+ public misfortunes and corruptions of Government, have resolved to set forth in a solemn
393
+ declaration, these natural, imprescriptible, and inalienable rights: that this declaration
394
+ being constantly present to the minds of the members of the body social, they may be for
395
+ ever kept attentive to their rights and their duties; that the acts of the legislative and
396
+ executive powers of government, being capable of being every moment compared with
397
+ the end of political institutions, may be more respected; and also, that the future claims of
398
+ the citizens, being directed by simple and incontestable principles, may tend to the
399
+ maintenance of the Constitution, and the general happiness. ''')
400
+ # it will play the audio automatically.
401
+ ```
402
+ ## Fine-Tuning and Evolution
403
+
404
+ `npcpy` provides modular tools for building adaptive AI systems through supervised fine-tuning, reinforcement learning, and genetic algorithms.
405
+
406
+ See examples/fine_tuning_demo.py for a complete working example.
407
+
408
+
409
+ ### Supervised Fine-Tuning (SFT)
410
+
411
+ Train models on specific tasks using simple X, y pairs:
412
+ ```python
413
+ from npcpy.ft.sft import run_sft, load_sft_model, predict_sft
414
+
415
+ X_train = ["translate to french: hello", "translate to french: goodbye"]
416
+ y_train = ["bonjour", "au revoir"]
417
+
418
+ model_path = run_sft(X_train, y_train)
419
+
420
+ model, tokenizer = load_sft_model(model_path)
421
+ response = predict_sft(model, tokenizer, "translate to french: thanks")
422
+ ```
423
+ ### Unsupervised Fine-Tuning (USFT)
424
+ Adapt models to domain-specific text corpora without labels:
425
+ ```python
426
+ from npcpy.ft.usft import run_usft, load_corpus_from_hf
427
+
428
+ texts = load_corpus_from_hf("tiny_shakespeare", split="train[:1000]")
429
+
430
+ model_path = run_usft(
431
+ texts,
432
+ config=USFTConfig(
433
+ output_model_path="models/shakespeare",
434
+ num_train_epochs=3
435
+ )
436
+ )
437
+ Train on your own text corpus:
438
+ pythondomain_texts = [
439
+ "Your domain-specific text 1",
440
+ "Your domain-specific text 2",
441
+ ] * 100
442
+
443
+ model_path = run_usft(domain_texts)
444
+ ```
445
+ ### Diffusion Fine-tuning
446
+ ```
447
+ from npcpy.ft.diff import train_diffusion, generate_image
448
+
449
+ image_paths = ["img1.png", "img2.png", "img3.png"]
450
+ captions = ["a cat", "a dog", "a bird"]
451
+
452
+ model_path = train_diffusion(
453
+ image_paths,
454
+ captions,
455
+ config=DiffusionConfig(
456
+ num_epochs=100,
457
+ batch_size=4
458
+ )
459
+ )
460
+
461
+ generated = generate_image(
462
+ model_path,
463
+ prompt="a white square",
464
+ image_size=128
465
+ )
466
+ Resume training from checkpoint:
467
+ pythonmodel_path = train_diffusion(
468
+ image_paths,
469
+ captions,
470
+ config,
471
+ resume_from="models/diffusion/checkpoints/checkpoint-epoch10-step1000.pt"
472
+ )
473
+ ```
474
+
475
+
476
+ ### Reinforcement Learning (RL)
477
+ Collect agent traces and train with DPO based on reward signals:
478
+ ```python
479
+ from npcpy.ft.rl import collect_traces, run_rl_training
480
+ from npcpy.npc_compiler import NPC
481
+
482
+ tasks = [
483
+ {'prompt': 'Solve 2+2', 'expected': '4'},
484
+ {'prompt': 'Solve 5+3', 'expected': '8'}
485
+ ]
486
+
487
+ agents = [
488
+ NPC(name="farlor", primary_directive="Be concise",
489
+ model="qwen3:0.6b", provider="ollama"),
490
+ NPC(name="tedno", primary_directive="Show your work",
491
+ model="qwen3:0.6b", provider="ollama")
492
+ ]
493
+
494
+ def reward_fn(trace):
495
+ if trace['task_metadata']['expected'] in trace['final_output']:
496
+ return 1.0
497
+ return 0.0
498
+
499
+ adapter_path = run_rl_training(tasks, agents, reward_fn)
500
+ ```
501
+ ### Genetic Evolution
502
+
503
+ Evolve populations of knowledge graphs or model ensembles:
504
+ ```python
505
+ from npcpy.ft.ge import GeneticEvolver, GAConfig
506
+
507
+ config = GAConfig(
508
+ population_size=20,
509
+ generations=50,
510
+ mutation_rate=0.15
511
+ )
512
+
513
+ evolver = GeneticEvolver(
514
+ fitness_fn=your_fitness_function,
515
+ mutate_fn=your_mutation_function,
516
+ crossover_fn=your_crossover_function,
517
+ initialize_fn=your_init_function,
518
+ config=config
519
+ )
520
+
521
+ best_individual = evolver.run()
522
+ ```
523
+
524
+ ### Smart Model Ensembler and response router
525
+ Build fast intuitive responses with fallback to reasoning:
526
+ ```python
527
+ from npcpy.ft.model_ensembler import (
528
+ ResponseRouter,
529
+ create_model_genome
530
+ )
531
+
532
+ genome = create_model_genome(['math', 'code', 'factual'])
533
+ router = ResponseRouter(fast_threshold=0.8)
534
+
535
+ result = router.route_query("What is 2+2?", genome)
536
+
537
+ if result['used_fast_path']:
538
+ print("Fast gut reaction")
539
+ elif result['used_ensemble']:
540
+ print("Ensemble voting")
541
+ else:
542
+ print("Full reasoning")
543
+ ```
544
+ The intention for this model ensembler system is to mimic human cognition: pattern-matched gut reactions (System 1 of Kahneman) for familiar queries, falling back to deliberate reasoning (System 2 of Kahneman) for novel problems. Genetic algorithms evolve both knowledge structures and model specializations over time.
545
+
546
+
547
+
548
+ ## Serving an NPC Team
549
+
550
+ `npcpy` includes a built-in Flask server that makes it easy to deploy NPC teams for production use. You can serve teams with tools, jinxs, and complex workflows that frontends can interact with via REST APIs.
551
+
552
+ ### Basic Team Server Setup
553
+
554
+ ```python
555
+ from npcpy.serve import start_flask_server
556
+ from npcpy.npc_compiler import NPC, Team
557
+ from npcpy.tools import auto_tools
558
+ import requests
559
+ import os
560
+
561
+ # Create NPCs with different specializations
562
+ researcher = NPC(
563
+ name='Research Specialist',
564
+ primary_directive='You are a research specialist who finds and analyzes information from various sources.',
565
+ model='claude-3-5-sonnet-latest',
566
+ provider='anthropic'
567
+ )
568
+
569
+ analyst = NPC(
570
+ name='Data Analyst',
571
+ primary_directive='You are a data analyst who processes and interprets research findings.',
572
+ model='gpt-4o',
573
+ provider='openai'
574
+ )
575
+
576
+ coordinator = NPC(
577
+ name='Project Coordinator',
578
+ primary_directive='You coordinate team activities and synthesize results into actionable insights.',
579
+ model='gemini-1.5-pro',
580
+ provider='gemini'
581
+ )
582
+
583
+ # Create team
584
+ research_team = Team(
585
+ npcs=[researcher, analyst],
586
+ forenpc=coordinator
587
+ )
588
+
589
+ if __name__ == "__main__":
590
+ # Register team and NPCs directly with the server
591
+ npcs = {npc.name: npc for npc in list(research_team.npcs.values()) + [research_team.forenpc]}
592
+ start_flask_server(
593
+ port=5337,
594
+ cors_origins=["http://localhost:3000", "http://localhost:5173"], # Allow frontend access
595
+ debug=True,
596
+ teams={'research_team': research_team},
597
+ npcs=npcs
598
+ )
599
+ ```
600
+
601
+
602
+
603
+ ## Read the Docs
604
+
605
+ For more examples of how to use `npcpy` to simplify your LLM workflows or to create agents or multi-agent systems, read the docs at [npcpy.readthedocs.io](https://npcpy.readthedocs.io/en/latest/)
606
+
607
+
608
+ ## Inference Capabilities
609
+ - `npcpy` works with local and enterprise LLM providers through its LiteLLM integration, allowing users to run inference from Ollama, LMStudio, OpenAI, Anthropic, Gemini, and Deepseek, making it a versatile tool for both simple commands and sophisticated AI-driven tasks.
610
+
611
+
612
+
613
+ ## NPC Studio
614
+ There is a graphical user interface that makes use of the NPC Toolkit through the NPC Studio. See the source code for NPC Studio [here](https://github.com/cagostino/npc-studio). Download the executables at [our website](https://enpisi.com/npc-studio).
615
+
616
+ ## NPC Shell
617
+
618
+ The NPC shell is a suite of executable command-line programs that allow users to easily interact with NPCs and LLMs through a command line shell.
619
+ [Try out the NPC Shell](https://github.com/npc-worldwide/npcsh)
620
+
621
+
622
+ ## Mailing List
623
+ Interested to stay in the loop and to hear the latest and greatest about `npcpy`, `npcsh` and NPC Studio? Be sure to sign up for the [newsletter](https://forms.gle/n1NzQmwjsV4xv1B2A)!
624
+
625
+
626
+
627
+ ## Support
628
+ If you appreciate the work here, [consider supporting NPC Worldwide with a monthly donation](https://buymeacoffee.com/npcworldwide), [buying NPC-WW themed merch](https://enpisi.com/shop), or hiring us to help you explore how to use `npcpy` and AI tools to help your business or research team, please reach out to info@npcworldwi.de .
629
+
630
+
631
+
632
+
633
+
634
+ ## Enabling Innovation and Research
635
+ - `npcpy` is a framework that speeds up and simplifies the development of NLP-based or Agent-based applications and provides developers and researchers with methods to explore and test across dozens of models, providers, and personas as well as other model-level hyperparameters (e.g. `temperature`, `top_k`, etc.), incorporating an array of data sources and common tools.
636
+ - The `npcpy` agent data layer makes it easy to set up teams and serve them so you can focus more on the agent personas and less on the nitty gritty of inference.
637
+ - `npcpy` provides pioneering methods in the construction and updating of knowledge graphs as well as in the development and testing of novel mixture of agent scenarios.
638
+ - In `npcpy`, all agentic capabilities are developed and tested using small local models (like `llama3.2`, `gemma3`) to ensure it can function reliably at the edge of computing.
639
+
640
+ ### Papers
641
+ - Paper on the limitations of LLMs and on the quantum-like nature of natural language interpretation : [arxiv preprint](https://arxiv.org/abs/2506.10077), accepted for publication at [Quantum AI and NLP 2025](qnlp.ai)
642
+ - Paper that considers the effects that might accompany simulating hormonal cycles for AI : [arxiv preprint](https://arxiv.org/abs/2508.11829)
643
+
644
+ Has your research benefited from npcpy? Let us know and we'd be happy to feature you here!
645
+
646
+ ## NPCs
647
+
648
+ Check out [lavanzaro](https://lavanzaro.com) to discuss the great things of life with an `npcpy` powered chatbot
649
+
650
+ ## Installation
651
+ `npcpy` is available on PyPI and can be installed using pip. Before installing, make sure you have the necessary dependencies installed on your system. Below are the instructions for installing such dependencies on Linux, Mac, and Windows. If you find any other dependencies that are needed, please let us know so we can update the installation instructions to be more accommodating.
652
+
653
+ ### Linux install
654
+ <details> <summary> Toggle </summary>
655
+
656
+ ```bash
657
+
658
+ # these are for audio primarily, skip if you dont need tts
659
+ sudo apt-get install espeak
660
+ sudo apt-get install portaudio19-dev python3-pyaudio
661
+ sudo apt-get install alsa-base alsa-utils
662
+ sudo apt-get install libcairo2-dev
663
+ sudo apt-get install libgirepository1.0-dev
664
+ sudo apt-get install ffmpeg
665
+
666
+ # for triggers
667
+ sudo apt install inotify-tools
668
+
669
+
670
+ #And if you don't have ollama installed, use this:
671
+ curl -fsSL https://ollama.com/install.sh | sh
672
+
673
+ ollama pull llama3.2
674
+ ollama pull llava:7b
675
+ ollama pull nomic-embed-text
676
+ pip install npcpy
677
+ # if you want to install with the API libraries
678
+ pip install 'npcpy[lite]'
679
+ # if you want the full local package set up (ollama, diffusers, transformers, cuda etc.)
680
+ pip install 'npcpy[local]'
681
+ # if you want to use tts/stt
682
+ pip install 'npcpy[yap]'
683
+ # if you want everything:
684
+ pip install 'npcpy[all]'
685
+
686
+ ```
687
+
688
+ </details>
689
+
690
+
691
+ ### Mac install
692
+
693
+ <details> <summary> Toggle </summary>
694
+
695
+ ```bash
696
+ #mainly for audio
697
+ brew install portaudio
698
+ brew install ffmpeg
699
+ brew install pygobject3
700
+
701
+ # for triggers
702
+ brew install inotify-tools
703
+
704
+
705
+ brew install ollama
706
+ brew services start ollama
707
+ ollama pull llama3.2
708
+ ollama pull llava:7b
709
+ ollama pull nomic-embed-text
710
+ pip install npcpy
711
+ # if you want to install with the API libraries
712
+ pip install npcpy[lite]
713
+ # if you want the full local package set up (ollama, diffusers, transformers, cuda etc.)
714
+ pip install npcpy[local]
715
+ # if you want to use tts/stt
716
+ pip install npcpy[yap]
717
+
718
+ # if you want everything:
719
+ pip install npcpy[all]
720
+ ```
721
+ </details>
722
+
723
+ ### Windows Install
724
+
725
+ <details> <summary> Toggle </summary>
726
+ Download and install ollama exe.
727
+
728
+ Then, in a powershell. Download and install ffmpeg.
729
+
730
+ ```powershell
731
+ ollama pull llama3.2
732
+ ollama pull llava:7b
733
+ ollama pull nomic-embed-text
734
+ pip install npcpy
735
+ # if you want to install with the API libraries
736
+ pip install npcpy[lite]
737
+ # if you want the full local package set up (ollama, diffusers, transformers, cuda etc.)
738
+ pip install npcpy[local]
739
+ # if you want to use tts/stt
740
+ pip install npcpy[yap]
741
+
742
+ # if you want everything:
743
+ pip install npcpy[all]
744
+ ```
745
+
746
+ </details>
747
+
748
+ ### Fedora Install (under construction)
749
+
750
+ <details> <summary> Toggle </summary>
751
+
752
+ ```bash
753
+ python3-dev #(fixes hnswlib issues with chroma db)
754
+ xhost + (pyautogui)
755
+ python-tkinter (pyautogui)
756
+ ```
757
+
758
+ </details>
759
+
760
+
761
+ We support inference via all providers supported by litellm. For openai-compatible providers that are not explicitly named in litellm, use simply `openai-like` as the provider. The default provider must be one of `['openai','anthropic','ollama', 'gemini', 'deepseek', 'openai-like']` and the model must be one available from those providers.
762
+
763
+ To use tools that require API keys, create an `.env` file in the folder where you are working or place relevant API keys as env variables in your ~/.npcshrc. If you already have these API keys set in a ~/.bashrc or a ~/.zshrc or similar files, you need not additionally add them to ~/.npcshrc or to an `.env` file. Here is an example of what an `.env` file might look like:
764
+
765
+ ```bash
766
+ export OPENAI_API_KEY="your_openai_key"
767
+ export ANTHROPIC_API_KEY="your_anthropic_key"
768
+ export DEEPSEEK_API_KEY='your_deepseek_key'
769
+ export GEMINI_API_KEY='your_gemini_key'
770
+ export PERPLEXITY_API_KEY='your_perplexity_key'
771
+ ```
772
+
773
+
774
+ Individual npcs can also be set to use different models and providers by setting the `model` and `provider` keys in the npc files.
775
+
776
+
777
+ For cases where you wish to set up a team of NPCs, jinxs, and assembly lines, add a `npc_team` directory to your project and then initialize an NPC Team.
778
+ ```bash
779
+ ./npc_team/ # Project-specific NPCs
780
+ ├── jinxs/ # Project jinxs #example jinx next
781
+ │ └── example.jinx
782
+ └── assembly_lines/ # Project workflows
783
+ └── example.pipe
784
+ └── models/ # Project workflows
785
+ └── example.model
786
+ └── example1.npc # Example NPC
787
+ └── example2.npc # Example NPC
788
+ └── team.ctx # Example ctx
789
+
790
+
791
+ ```
792
+
793
+
794
+ ## Contributing
795
+ Contributions are welcome! Please submit issues and pull requests on the GitHub repository.
796
+
797
+
798
+ ## License
799
+ This project is licensed under the MIT License.
800
+
801
+ ## Star History
802
+
803
+ [![Star History Chart](https://api.star-history.com/svg?repos=cagostino/npcpy&type=Date)](https://star-history.com/#cagostino/npcpy&Date)