|
| 1 | +# How to Add a Tool to Diffbot LLM Inference |
| 2 | + |
| 3 | +We discuss how to add a tool to Diffbot LLM Inference by showing how to |
| 4 | +add the tool `execute_js_v1` for javascript code execution. |
| 5 | + |
| 6 | +### 0. Set up local development |
| 7 | + |
| 8 | +To set up the virtual environment: |
| 9 | + |
| 10 | +``` |
| 11 | +poetry env use python3.10 |
| 12 | +poetry shell |
| 13 | +poetry install |
| 14 | +``` |
| 15 | + |
| 16 | +To start vLLM: |
| 17 | + |
| 18 | +Self-host one the Diffbot LLM models with docker (see [Self-Hosting](README.md)) and add "-p 8000:8000" to expose |
| 19 | +the vLLM endpoint. Set the vLLM endpoint in config.py. |
| 20 | + |
| 21 | +To start the server: `./start_server.sh` |
| 22 | + |
| 23 | +### 1. Add the new tool to the system prompt (`system_prompt.txt`). |
| 24 | + |
| 25 | +Below is the original system prompt, which includes the definition of the |
| 26 | +available tools in javascript. |
| 27 | +``` |
| 28 | +You are a helpful assistant with access to the following functions. Use them if required - |
| 29 | +namespace Diffbot { |
| 30 | +// Extract the content from the given URLs. Only call this endpoint if the user mentioned a URL. |
| 31 | +type extract_v1 = (_: { |
| 32 | +// URLs to extract, up to 5 |
| 33 | +page_url: string[], |
| 34 | +}) => any; |
| 35 | +// Query the Diffbot Knowledge Graph for an entity or set of entities that match a set of criteria using the Diffbot Query Language syntax. |
| 36 | +type dql_v1 = (_: { |
| 37 | +// Diffbot Query Language query |
| 38 | +dql_query: string, |
| 39 | +}) => any; |
| 40 | +// Search the web for information that could help answer the user's question. |
| 41 | +type web_search_v1 = (_: { |
| 42 | +// List of Google advanced search strings (can include phrases, booleans, site:, before:, after:, filetype:, etc) |
| 43 | +text: string[], |
| 44 | +// Number of results to return (default 5) |
| 45 | +num?: number, |
| 46 | +// Page number of results to return (default 1) |
| 47 | +page?: number, |
| 48 | +}) => any; |
| 49 | +} // namespace Diffbot |
| 50 | +``` |
| 51 | + |
| 52 | +To add the tool `execute_js_v1`, we can add the following lines as the last tool: |
| 53 | + |
| 54 | +``` |
| 55 | +// Execute JavaScript expressions and get accurate results that could help answer the user's question. |
| 56 | +type execute_js_v1 = (_: { |
| 57 | +// JavaScript expressions to execute separated by newlines |
| 58 | +expressions: string, |
| 59 | +}) => any; |
| 60 | +``` |
| 61 | + |
| 62 | +The final result is: |
| 63 | + |
| 64 | +``` |
| 65 | +You are a helpful assistant with access to the following functions. Use them if required - |
| 66 | +namespace Diffbot { |
| 67 | +// Extract the content from the given URLs. Only call this endpoint if the user mentioned a URL. |
| 68 | +type extract_v1 = (_: { |
| 69 | +// URLs to extract, up to 5 |
| 70 | +page_url: string[], |
| 71 | +}) => any; |
| 72 | +// Query the Diffbot Knowledge Graph for an entity or set of entities that match a set of criteria using the Diffbot Query Language syntax. |
| 73 | +type dql_v1 = (_: { |
| 74 | +// Diffbot Query Language query |
| 75 | +dql_query: string, |
| 76 | +}) => any; |
| 77 | +// Search the web for information that could help answer the user's question. |
| 78 | +type web_search_v1 = (_: { |
| 79 | +// List of Google advanced search strings (can include phrases, booleans, site:, before:, after:, filetype:, etc) |
| 80 | +text: string[], |
| 81 | +// Number of results to return (default 5) |
| 82 | +num?: number, |
| 83 | +// Page number of results to return (default 1) |
| 84 | +page?: number, |
| 85 | +}) => any; |
| 86 | +// Execute JavaScript expressions and get accurate results that could help answer the user's question. |
| 87 | +type execute_js_v1 = (_: { |
| 88 | +// JavaScript expressions to execute separated by newlines |
| 89 | +expressions: string, |
| 90 | +}) => any; |
| 91 | +} // namespace Diffbot |
| 92 | +``` |
| 93 | + |
| 94 | +### 2. Implement the new tool |
| 95 | + |
| 96 | +See `services/execute_js.py` for the implementation of `execute_js_v1`. |
| 97 | + |
| 98 | +### 3. Call the tool in llm/plugin.py |
| 99 | + |
| 100 | +The `invoke` method is responsible for calling tools requested by the LLM. To call the new tool, we can add the |
| 101 | +following lines to this method: |
| 102 | + |
| 103 | +```python |
| 104 | +if function_name == "execute_js_v1": |
| 105 | + resp = await get_js_execution_service().execute_js(function_arguments["expressions"]) |
| 106 | + return PluginResponse( |
| 107 | + plugin_url=function_name, method="INTERNAL", content=resp.json() |
| 108 | + ) |
| 109 | +``` |
| 110 | + |
| 111 | +where `get_js_execution_service().execute_js()` calls the implementation for this new tool. |
0 commit comments