Skip to content

Commit 83e1dbe

Browse files
committed
Add Elixir documentation to getting-started and AI integration guides
1 parent e03aea7 commit 83e1dbe

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

docs/ai-integration.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,71 @@ cosine_sim(A, B) ->
672672
Dot / (NormA * NormB).
673673
```
674674

675+
## Using from Elixir
676+
677+
All AI examples work seamlessly from Elixir:
678+
679+
```elixir
680+
# Start erlang_python
681+
{:ok, _} = Application.ensure_all_started(:erlang_python)
682+
683+
# Activate venv with AI libraries
684+
:ok = :py.activate_venv("/path/to/ai_venv")
685+
686+
# Generate embeddings using ai_helpers module
687+
{:ok, embeddings} = :py.call(:ai_helpers, :embed_texts, [
688+
["Elixir is functional", "Python does ML", "BEAM is concurrent"]
689+
])
690+
691+
# Semantic search
692+
{:ok, query_emb} = :py.call(:ai_helpers, :embed_single, ["concurrent programming"])
693+
694+
# Calculate similarity in Elixir
695+
similarities = Enum.zip(texts, embeddings)
696+
|> Enum.map(fn {text, emb} -> {text, cosine_similarity(query_emb, emb)} end)
697+
|> Enum.sort_by(fn {_, score} -> score end, :desc)
698+
```
699+
700+
### Parallel AI with BEAM Processes
701+
702+
```elixir
703+
# Register parallel embedding function
704+
:py.register_function(:parallel_embed, fn [texts] ->
705+
parent = self()
706+
707+
refs = Enum.map(texts, fn text ->
708+
ref = make_ref()
709+
spawn(fn ->
710+
{:ok, emb} = :py.call(:ai_helpers, :embed_single, [text])
711+
send(parent, {ref, emb})
712+
end)
713+
ref
714+
end)
715+
716+
Enum.map(refs, fn ref ->
717+
receive do
718+
{^ref, result} -> result
719+
after
720+
30_000 -> {:error, :timeout}
721+
end
722+
end)
723+
end)
724+
```
725+
726+
### Running the Elixir AI Example
727+
728+
```bash
729+
# Full Elixir example with AI integration
730+
elixir --erl "-pa _build/default/lib/erlang_python/ebin" examples/elixir_example.exs
731+
```
732+
733+
The example demonstrates:
734+
- Basic Python calls from Elixir
735+
- Data type conversion
736+
- Registering Elixir callbacks for Python
737+
- Parallel processing (10x speedup)
738+
- Semantic search with embeddings
739+
675740
## See Also
676741

677742
- [Getting Started](getting-started.md) - Basic usage

docs/getting-started.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,82 @@ py_semaphore:current(). %% Currently executing
139139

140140
See [Scalability](scalability.md) for details on execution modes and performance tuning.
141141

142+
## Using from Elixir
143+
144+
erlang_python works seamlessly with Elixir. The `:py` module can be called directly:
145+
146+
```elixir
147+
# Start the application
148+
{:ok, _} = Application.ensure_all_started(:erlang_python)
149+
150+
# Call Python functions
151+
{:ok, 4.0} = :py.call(:math, :sqrt, [16])
152+
153+
# Evaluate expressions
154+
{:ok, result} = :py.eval("2 + 2")
155+
156+
# With variables
157+
{:ok, 100} = :py.eval("x * y", %{x: 10, y: 10})
158+
159+
# Call with keyword arguments
160+
{:ok, json} = :py.call(:json, :dumps, [%{name: "Elixir"}], %{indent: 2})
161+
```
162+
163+
### Register Elixir Functions for Python
164+
165+
```elixir
166+
# Register an Elixir function
167+
:py.register_function(:factorial, fn [n] ->
168+
Enum.reduce(1..n, 1, &*/2)
169+
end)
170+
171+
# Call from Python
172+
{:ok, 3628800} = :py.eval("__import__('erlang').call('factorial', 10)")
173+
174+
# Cleanup
175+
:py.unregister_function(:factorial)
176+
```
177+
178+
### Parallel Processing with BEAM
179+
180+
```elixir
181+
# Register parallel map using BEAM processes
182+
:py.register_function(:parallel_map, fn [func_name, items] ->
183+
parent = self()
184+
185+
refs = Enum.map(items, fn item ->
186+
ref = make_ref()
187+
spawn(fn ->
188+
result = apply_function(func_name, item)
189+
send(parent, {ref, result})
190+
end)
191+
ref
192+
end)
193+
194+
Enum.map(refs, fn ref ->
195+
receive do
196+
{^ref, result} -> result
197+
after
198+
5000 -> {:error, :timeout}
199+
end
200+
end)
201+
end)
202+
```
203+
204+
### Running the Elixir Example
205+
206+
A complete working example is available:
207+
208+
```bash
209+
elixir --erl "-pa _build/default/lib/erlang_python/ebin" examples/elixir_example.exs
210+
```
211+
212+
This demonstrates basic calls, data conversion, callbacks, parallel processing (10x speedup), and AI integration.
213+
142214
## Next Steps
143215

144216
- See [Type Conversion](type-conversion.md) for detailed type mapping
145217
- See [Streaming](streaming.md) for working with generators
146218
- See [Memory Management](memory.md) for GC and debugging
147219
- See [Scalability](scalability.md) for parallelism and performance
220+
- See [AI Integration](ai-integration.md) for ML/AI examples

0 commit comments

Comments
 (0)