PsychScanner with Local Ollama Models¶
This tutorial demonstrates how to run a local simulation using psychscanner and ollama.
1. Setup & Prerequisites¶
Before running this notebook, ensure you have Ollama installed on your system.
- Mac/Linux/Windows: Download at https://ollama.com/download
- Once installed, open a terminal and run
ollama serve(if not running in the background) and pull the model you want to use, for example:ollama pull llama3
In [8]:
Copied!
import psychscanner
print('PsychScanner successfully imported!')
import psychscanner
print('PsychScanner successfully imported!')
PsychScanner successfully imported!
2. Initialize the Experiment Card¶
We will configure the ExpCard to point to our local Ollama model instead of an external API.
In [9]:
Copied!
import psychscanner as psy #import ExpCard, ScannerModel
from pathlib import Path
# Set up the experiment card to run the default task using a local llama3 model
# - family='ollama' tells PsychScanner to use the local Ollama provider
# - model='llama3' will target the specific pulled Ollama model
task_file = Path.cwd() / 'tasks' / 'example_survey.json'
task_file.is_file(),task_file
import psychscanner as psy #import ExpCard, ScannerModel
from pathlib import Path
# Set up the experiment card to run the default task using a local llama3 model
# - family='ollama' tells PsychScanner to use the local Ollama provider
# - model='llama3' will target the specific pulled Ollama model
task_file = Path.cwd() / 'tasks' / 'example_survey.json'
task_file.is_file(),task_file
Out[9]:
(True,
PosixPath('/Users/saurabhext/Documents/PSYCHSCANNER/psychscanner/examples/tasks/example_survey.json'))
In [10]:
Copied!
card_in = psy.ExpCardInit()
card_in = psy.ExpCardInit()
In [11]:
Copied!
card_in = psy.ExpCardInit()
card_in.proj_dir = Path.cwd()
card_in.projectname = "testing" # ""study_rm_exp2"
card_in.tunnel_status = "1"
card_in.parameters = {"temperature":0}
card_in.model = 'smollm2:360m-instruct-fp16'
card_in.family = "ollama"
card_in.parser = "1"
card_in.task_file = task_file
card_in.cogtype = "no"
card_in.nsim = 5
card_in.chain_type = "task"
card_in.memory = "Convo"
card_in = psy.ExpCardInit()
card_in.proj_dir = Path.cwd()
card_in.projectname = "testing" # ""study_rm_exp2"
card_in.tunnel_status = "1"
card_in.parameters = {"temperature":0}
card_in.model = 'smollm2:360m-instruct-fp16'
card_in.family = "ollama"
card_in.parser = "1"
card_in.task_file = task_file
card_in.cogtype = "no"
card_in.nsim = 5
card_in.chain_type = "task"
card_in.memory = "Convo"
In [12]:
Copied!
expcard = psy.ExpCard(card_in)
expcard = psy.ExpCard(card_in)
task_initial: {'tasktype': 'survey', 'taskname': 'example_survey', 'instructions': {'definition': ['You will rate your agreement with the following statements.', 'Use a scale from 1 (strongly disagree) to 5 (strongly agree).', 'Be honest and thoughtful in your responses.']}, 'contexts': ['Openness to Experience', 'Conscientiousness'], 'contexts_id': ['O', 'C'], 'context_present': True, 'items': {'O_1': [{'trcode': 'O_1', 'stimulus': 'I enjoy trying new things and exploring new ideas.'}], 'O_2': [{'trcode': 'O_2', 'stimulus': 'I am comfortable with ambiguity and uncertainty.'}], 'O_3': [{'trcode': 'O_3', 'stimulus': 'I prefer routine and predictability.'}], 'C_1': [{'trcode': 'C_1', 'stimulus': 'I am organized and pay attention to details.'}], 'C_2': [{'trcode': 'C_2', 'stimulus': 'I follow through on my commitments.'}], 'C_3': [{'trcode': 'C_3', 'stimulus': 'I tend to procrastinate on important tasks.'}]}, 'parser': 'DefaultLiteralAgree', 'chain_type': 'item'}
type------- <class 'dict'>
----<PROJECT AND DATA ROOT DIRECTORY>----
Project root dir: /Users/saurabhext/Documents/PSYCHSCANNER/psychscanner/examples
Simulation data root dir: /Users/saurabhext/Documents/PSYCHSCANNER/psychscanner/examples/testing/example_survey/ollama_smollm2:360m-instruct-fp16_Convo ----<>----
In [15]:
Copied!
scanner = psy.ScannerModel(expcard=expcard)
# get simulations
simulation = scanner.run()
scanner = psy.ScannerModel(expcard=expcard)
# get simulations
simulation = scanner.run()
{'temperature': 0}
--<chat model>-- model='smollm2:360m-instruct-fp16' temperature=0.0
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[15], line 3 1 scanner = psy.ScannerModel(expcard=expcard) 2 # get simulations ----> 3 simulation = scanner.run() File ~/Documents/PSYCHSCANNER/psychscanner/src/psychscanner/scanner_models/scanner_model.py:223, in ScannerModel.run(self, progress_bar, feedback, feedback_fn, save_str, tunnel) 221 self.tunnel = tunnel 222 tunnel_id = None --> 223 self.tunnel.create_tunnel() 225 resume_idx = None # Initialize resume_idx with a default value 226 if self.continuing_scan: File ~/Documents/PSYCHSCANNER/psychscanner/src/psychscanner/session_tunnel/session_tunnel.py:147, in SessionTunnel.create_tunnel(self, tunnel_file) 145 if len(session_over)==1: 146 msg = "Session already has ended. Delete old files to run." --> 147 raise ValueError(msg) 149 with self.session.contextualize( 150 id=self.session_id, run_type=self.run_type, state=self.state 151 ): 152 self.session.critical("BEGIN") ValueError: Session already has ended. Delete old files to run.
In [14]:
Copied!
exp_card = psy.ExpCard(
model='smollm2:360m-instruct-fp16',
family='ollama',
cogtype = 'no',
task_file= task_file, # Custom Example Survey
memory='SingleTurn', # Independent trials
projectname='ollama_local_test',
enabletqdm=True # Show progress bar
)
print(f'Experiment structured perfectly! Saving results to: {exp_card.data_root_dir}')
exp_card = psy.ExpCard(
model='smollm2:360m-instruct-fp16',
family='ollama',
cogtype = 'no',
task_file= task_file, # Custom Example Survey
memory='SingleTurn', # Independent trials
projectname='ollama_local_test',
enabletqdm=True # Show progress bar
)
print(f'Experiment structured perfectly! Saving results to: {exp_card.data_root_dir}')
Cell In[14], line 2 model=, ^ SyntaxError: invalid syntax
3. Run the Scanner¶
This will invoke the local Ollama agent under the hood.
In [ ]:
Copied!
# Instantiate and run the scanner model
scanner = ScannerModel(exp_card)
print('Starting local simulation...')
results = scanner.run(progress_bar=True)
print('\nSimulation Complete!')
# Instantiate and run the scanner model
scanner = ScannerModel(exp_card)
print('Starting local simulation...')
results = scanner.run(progress_bar=True)
print('\nSimulation Complete!')
--<chat model>-- model='smollm2:360m-instruct-fp16' Starting local simulation...
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[30], line 5 2 scanner = ScannerModel(exp_card) 4 print('Starting local simulation...') ----> 5 results = scanner.run(progress_bar=True) 7 print('\nSimulation Complete!') File ~/Documents/PSYCHSCANNER/psychscanner/src/psychscanner/scanner_models/scanner_model.py:223, in ScannerModel.run(self, progress_bar, feedback, feedback_fn, save_str, tunnel) 221 self.tunnel = tunnel 222 tunnel_id = None --> 223 self.tunnel.create_tunnel() 225 resume_idx = None # Initialize resume_idx with a default value 226 if self.continuing_scan: File ~/Documents/PSYCHSCANNER/psychscanner/src/psychscanner/session_tunnel/session_tunnel.py:147, in SessionTunnel.create_tunnel(self, tunnel_file) 145 if len(session_over)==1: 146 msg = "Session already has ended. Delete old files to run." --> 147 raise ValueError(msg) 149 with self.session.contextualize( 150 id=self.session_id, run_type=self.run_type, state=self.state 151 ): 152 self.session.critical("BEGIN") ValueError: Session already has ended. Delete old files to run.
In [ ]:
Copied!
Cell In[23], line 1 make a new tutorial but this time changing to new tempreture and context window. ^ SyntaxError: invalid syntax
In [ ]:
Copied!
# REALITY MONITORING TASK
# REALITY MONITORING TASK
4. Customizing Model Parameters¶
You can also tweak parameters such as the temperature (controls randomness) and the context window (how many tokens the model holds in memory) using the parameters dictionary.
Ollama natively uses num_ctx to configure the internal context window limit constraint!
In [ ]:
Copied!
# Inject custom inference parameters dynamically
custom_card = psy.ExpCard(
model='smollm2:360m-instruct-fp16',
family="ollama",
task_file="tasks/example_survey.json",
memory="SingleTurn",
projectname="ollama_custom_params",
enabletqdm=True,
cogtype = 'no',
parameters={
"temperature": 0.8,
"num_ctx": 4096
}
)
print(f"Custom Card parameters: {custom_card.card_in.parameters}")
# Run the customized experiment!
custom_scanner = ScannerModel(custom_card)
custom_results = custom_scanner.run(progress_bar=True)
# Inject custom inference parameters dynamically
custom_card = psy.ExpCard(
model='smollm2:360m-instruct-fp16',
family="ollama",
task_file="tasks/example_survey.json",
memory="SingleTurn",
projectname="ollama_custom_params",
enabletqdm=True,
cogtype = 'no',
parameters={
"temperature": 0.8,
"num_ctx": 4096
}
)
print(f"Custom Card parameters: {custom_card.card_in.parameters}")
# Run the customized experiment!
custom_scanner = ScannerModel(custom_card)
custom_results = custom_scanner.run(progress_bar=True)
No input provided. Using default values.
----<PROJECT AND DATA ROOT DIRECTORY>----
Project root dir: /Users/saurabhext/psychscanner
Simulation data root dir: /Users/saurabhext/psychscanner/ollama_custom_params/example_survey/ollama_smollm2:360m-instruct-fp16_SingleTurn
----<>----
Custom Card parameters: {'temperature': 0.8, 'num_ctx': 4096}
--<chat model>-- model='smollm2:360m-instruct-fp16' num_ctx=4096 temperature=0.8
--------------------------------------------------------------------------- ValidationError Traceback (most recent call last) Cell In[31], line 19 16 print(f"Custom Card parameters: {custom_card.card_in.parameters}") 18 # Run the customized experiment! ---> 19 custom_scanner = ScannerModel(custom_card) 20 custom_results = custom_scanner.run(progress_bar=True) File ~/Documents/PSYCHSCANNER/psychscanner/src/psychscanner/scanner_models/scanner_model.py:36, in ScannerModel.__init__(self, expcard) 32 self.data_root_dir = self.expcard.data_root_dir 34 self.scanner_data = scanner_data(self.expcard) ---> 36 self.agent_config = AgentConfig( 37 modelname=self.expcard.card_in.model, 38 familyname=self.expcard.card_in.family, 39 parameters=self.expcard.card_in.parameters, 40 modelobject=llm_chat_model( 41 model=self.expcard.card_in.model, 42 family=self.expcard.card_in.family, 43 parameters=self.expcard.card_in.parameters, 44 ), 45 memory_type=self.expcard.card_in.memory, 46 memory_k=self.expcard.card_in.memory_k, 47 chain_type = self.expcard.task_data["chain_type"], 48 system_msg=None, 49 parser=self.expcard.parser, 50 parser_raw=self.expcard.card_in.parser_raw, 51 parser_config=self.expcard.card_in.parser_config, 52 ) 54 self.tqdm_progress_flag = expcard.card_in.enabletqdm 55 self.tunnel_status = self.expcard.card_in.tunnel_status File /opt/anaconda3/envs/psyscan/lib/python3.11/site-packages/pydantic/main.py:253, in BaseModel.__init__(self, **data) 251 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks 252 __tracebackhide__ = True --> 253 validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self) 254 if self is not validated_self: 255 warnings.warn( 256 'A custom validator is returning a value other than `self`.\n' 257 "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n" 258 'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.', 259 stacklevel=2, 260 ) ValidationError: 1 validation error for AgentConfig parameters Input should be a valid string [type=string_type, input_value={'temperature': 0.8, 'num_ctx': 4096}, input_type=dict] For further information visit https://errors.pydantic.dev/2.11/v/string_type
In [ ]:
Copied!