Skip to content

Commit dce83cb

Browse files
committed
Add LLMClient tutorial and docs
1 parent 28932af commit dce83cb

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

docs/api/psyflow.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ psyflow.TriggerSender module
5252
:undoc-members:
5353
:show-inheritance:
5454

55+
psyflow.LLM module
56+
------------------
57+
58+
.. automodule:: psyflow.LLM
59+
:members:
60+
:undoc-members:
61+
:show-inheritance:
62+
5563
psyflow.utils module
5664
--------------------
5765

docs/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ Start with one of the tutorials below or explore the API reference.
2727
tutorials/build_stimulus
2828
tutorials/send_trigger
2929
tutorials/cli_usage
30-
30+
tutorials/llm_client
31+
3132

3233
.. toctree::
3334
:maxdepth: 2

docs/tutorials/llm_client.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Using `LLMClient` for AI-Driven Helpers
2+
3+
## Overview
4+
5+
`LLMClient` provides a unified interface for interacting with several large language model (LLM) providers and includes helpers for converting tasks to documentation, reconstructing tasks from documentation, and translating experiment resources.
6+
7+
It supports:
8+
9+
- **Google Gemini** via the GenAI SDK (`provider="gemini"`)
10+
- **OpenAI** models (`provider="openai"`)
11+
- **Deepseek** models using the OpenAI API format (`provider="deepseek"`)
12+
13+
Custom providers can also be registered programmatically.
14+
15+
## Initialization
16+
17+
Create an instance by specifying the provider, API key, and model name:
18+
19+
```python
20+
from psyflow import LLMClient
21+
22+
client = LLMClient(
23+
provider="openai",
24+
api_key="YOUR_API_KEY",
25+
model="gpt-3.5-turbo"
26+
)
27+
```
28+
29+
The client wraps the underlying SDK and exposes common methods regardless of provider.
30+
31+
## Generating Text
32+
33+
Use `generate(prompt, **kwargs)` to obtain a completion from the configured model. Optional keyword arguments are passed to the underlying provider. Setting `deterministic=True` disables sampling randomness.
34+
35+
```python
36+
reply = client.generate(
37+
"Summarise the Stroop task in one sentence",
38+
deterministic=True,
39+
max_tokens=50
40+
)
41+
print(reply)
42+
```
43+
44+
## Converting Tasks to Documentation
45+
46+
`task2doc()` summarises an existing task into a README. The function loads your task logic and configuration, optionally uses few‑shot examples from `add_knowledge()`, and returns the generated Markdown text. If `output_path` is provided the README is also written to disk.
47+
48+
```python
49+
readme_text = client.task2doc(
50+
logic_paths=["./src/run_trial.py", "./main.py"],
51+
config_paths=["./config/config.yaml"],
52+
deterministic=True,
53+
output_path="./README.md"
54+
)
55+
```
56+
57+
## Recreating Tasks from Documentation
58+
59+
`doc2task()` performs the reverse operation. Given a README or raw description it regenerates the key source files. Provide a directory for outputs via `taps_root` and optionally customise the list of expected file names.
60+
61+
```python
62+
files = client.doc2task(
63+
doc_text="./README.md",
64+
taps_root="./recreated_task",
65+
deterministic=True
66+
)
67+
# files is a dict mapping each file name to its saved path
68+
```
69+
70+
## Translation Utilities
71+
72+
Several helper methods assist with localisation. The base `translate(text, target_language)` function translates arbitrary text while keeping formatting intact. `translate_config()` applies translation to relevant fields in a psyflow YAML configuration and can write the translated file to disk.
73+
74+
```python
75+
translated = client.translate(
76+
"Press the space bar when you see the target word",
77+
target_language="German"
78+
)
79+
80+
new_cfg = client.translate_config(
81+
target_language="German",
82+
config="./config/config.yaml",
83+
output_dir="./i18n"
84+
)
85+
```
86+
87+
These utilities store the last prompt and response for inspection (`last_prompt`, `last_response`) and automatically count tokens for the active model.
88+
89+
## Further Reading
90+
91+
See the API reference for a full description of all attributes and methods provided by [`psyflow.LLMClient`](../api/psyflow#psyflow.LLMClient).
92+

0 commit comments

Comments
 (0)