|
| 1 | +# 📘 Delphi Tutorial: Creating a Wikipedia Search Agent |
| 2 | + |
| 3 | +In this tutorial, we'll guide you through creating a conversational agent using |
| 4 | +Delphi that can perform Wikipedia searches. This agent will leverage the Azure |
| 5 | +OpenAI client for communication and a custom agent function for searching |
| 6 | +Wikipedia. |
| 7 | + |
| 8 | +## 📚 Prerequisites |
| 9 | + |
| 10 | +- Ensure you have Node.js installed. |
| 11 | +- Set up a project with TypeScript. |
| 12 | +- Install the `@azure/openai` and `wikipedia` npm packages. |
| 13 | + |
| 14 | +## Step 1: Import Dependencies |
| 15 | + |
| 16 | +First, import all the necessary modules at the top of your TypeScript file: |
| 17 | + |
| 18 | +```typescript |
| 19 | +import { OpenAIClient, OpenAIKeyCredential } from "@azure/openai"; |
| 20 | +import wikipedia from "wikipedia"; |
| 21 | + |
| 22 | +import { |
| 23 | + Agent, |
| 24 | + AgentFunction, |
| 25 | + Context, |
| 26 | + type JSONSchemaType, |
| 27 | +} from "@wecandobetter/delphi"; |
| 28 | +``` |
| 29 | + |
| 30 | +## Step 2: Define Search Parameters and Results |
| 31 | + |
| 32 | +Define interfaces for your search parameters and the expected search results: |
| 33 | + |
| 34 | +```typescript |
| 35 | +interface SearchParameters { |
| 36 | + query: string; |
| 37 | + limit?: number; |
| 38 | + suggestion?: boolean; |
| 39 | +} |
| 40 | + |
| 41 | +interface SearchResult { |
| 42 | + results: string[]; |
| 43 | + suggestion?: string; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +## Step 3: Create a JSON Schema |
| 48 | + |
| 49 | +Define a JSON schema for validating the input to your search function: |
| 50 | + |
| 51 | +```typescript |
| 52 | +const searchSchema: JSONSchemaType<SearchParameters> = { |
| 53 | + type: "object", |
| 54 | + properties: { |
| 55 | + query: { type: "string" }, |
| 56 | + limit: { type: "number", nullable: true }, |
| 57 | + suggestion: { type: "boolean", nullable: true }, |
| 58 | + }, |
| 59 | + required: ["query"], |
| 60 | + additionalProperties: false, |
| 61 | +}; |
| 62 | +``` |
| 63 | + |
| 64 | +## Step 4: Implement the Search Function |
| 65 | + |
| 66 | +Create an instance of `AgentFunction` that performs the Wikipedia search: |
| 67 | + |
| 68 | +```typescript |
| 69 | +const searchFn = new AgentFunction<SearchParameters, SearchResult>( |
| 70 | + "search", |
| 71 | + "Search on Wikipedia", |
| 72 | + searchSchema, |
| 73 | + async ({ query, limit = 5, suggestion = true }) => { |
| 74 | + const { results, suggestion: sugg } = await wikipedia.search(query, { |
| 75 | + limit, |
| 76 | + suggestion, |
| 77 | + }); |
| 78 | + return { results: results.map(({ title }) => title), suggestion: sugg }; |
| 79 | + }, |
| 80 | +); |
| 81 | +``` |
| 82 | + |
| 83 | +## Step 5: Set Up the OpenAI Client |
| 84 | + |
| 85 | +Configure the OpenAI client with your API key: |
| 86 | + |
| 87 | +```typescript |
| 88 | +const client = new OpenAIClient(new OpenAIKeyCredential("<API_KEY>")); |
| 89 | +``` |
| 90 | + |
| 91 | +## Step 6: Create the Agent |
| 92 | + |
| 93 | +Instantiate an `Agent` that will handle chat completions: |
| 94 | + |
| 95 | +```typescript |
| 96 | +const agent = new Agent( |
| 97 | + "wiki", |
| 98 | + async (messages, options) => { |
| 99 | + const response = await client.getChatCompletions( |
| 100 | + options.model, |
| 101 | + messages, |
| 102 | + options, |
| 103 | + ); |
| 104 | + return response.choices[0].message!; |
| 105 | + }, |
| 106 | + { client: { model: "gpt-4-1106-preview" } }, |
| 107 | +); |
| 108 | +``` |
| 109 | + |
| 110 | +## Step 7: Initialize the Context |
| 111 | + |
| 112 | +Create a new `Context` and add a system message to it: |
| 113 | + |
| 114 | +```typescript |
| 115 | +const context = new Context(); |
| 116 | +context.addMessage({ |
| 117 | + role: "system", |
| 118 | + content: |
| 119 | + "Search Wikipedia for a random scientific subject and tell me about it.", |
| 120 | +}); |
| 121 | +``` |
| 122 | + |
| 123 | +## Step 8: Register and Enable the Function |
| 124 | + |
| 125 | +Register your search function in the context and enable it: |
| 126 | + |
| 127 | +```typescript |
| 128 | +context.functions.set(searchFn.name, searchFn); |
| 129 | +context.functions.enable(searchFn.name); |
| 130 | +``` |
| 131 | + |
| 132 | +## Step 9: Run the Agent and Display the Result |
| 133 | + |
| 134 | +Finally, run the agent with the context and log the result: |
| 135 | + |
| 136 | +```typescript |
| 137 | +const result = await agent.run(context); |
| 138 | +console.log(result.content); |
| 139 | +``` |
| 140 | + |
| 141 | +## 🎉 Conclusion |
| 142 | + |
| 143 | +Congratulations! You've successfully created a Delphi agent that can search |
| 144 | +Wikipedia based on user prompts and display the results. This tutorial showcases |
| 145 | +the integration of custom agent functions, use of external APIs, and |
| 146 | +conversation management in Delphi. Feel free to expand and customize your agent |
| 147 | +to suit your specific needs. |
0 commit comments