Skip to content

Commit 5eaceab

Browse files
committed
Add Java Files - DRAFT
1 parent c0a90d6 commit 5eaceab

File tree

15 files changed

+1489
-0
lines changed

15 files changed

+1489
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Azure OpenAI API Key
2+
AZURE_API_KEY=your_api_key_here
3+
4+
# Azure OpenAI Endpoint
5+
AZURE_ENDPOINT=your_endpoint_here
6+
7+
# Azure OpenAI Model Deployment Name
8+
AZURE_DEPLOYMENT=your_deployment_name_here
9+
10+
# Project ID (Optional - will be generated if not provided)
11+
PROJECT_ID=your_project_id_here
12+
MODEL_DEPLOYMENT_NAME=gpt-4o
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
# Java QuickStart for Azure AI Foundry SDK
2+
3+
This quickstart guide demonstrates how to use the Azure AI Foundry SDK with Java to create projects, run chat completions, create agents, add file search capabilities, and evaluate agent runs.
4+
5+
## Prerequisites
6+
7+
- Java Development Kit (JDK) 17 or later
8+
- We recommend the [Microsoft Build of OpenJDK](https://learn.microsoft.com/en-us/java/openjdk/download), which is a free, Long-Term Support (LTS) distribution of OpenJDK
9+
- Maven 3.6.0 or later
10+
- Download from the [Apache Maven website](https://maven.apache.org/download.cgi)
11+
- An Azure subscription with access to Azure AI services
12+
- Free account: [Create an Azure account](https://azure.microsoft.com/free/)
13+
- Access to [Azure AI Foundry](https://ai.azure.com)
14+
- Install the [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli)
15+
16+
## Setup
17+
18+
### Development Environment
19+
20+
#### Visual Studio Code Setup (Recommended)
21+
22+
1. **Install Visual Studio Code**
23+
- Download and install from [VS Code website](https://code.visualstudio.com/)
24+
25+
2. **Install Java Extensions**
26+
- Install the [Extension Pack for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack) which includes:
27+
- Language Support for Java by Red Hat
28+
- Debugger for Java
29+
- Test Runner for Java
30+
- Maven for Java
31+
- Project Manager for Java
32+
- Visual Studio IntelliCode
33+
34+
3. **Configure Java in VS Code**
35+
- Set JAVA_HOME environment variable pointing to your JDK installation
36+
- Follow the [VS Code Java setup guide](https://code.visualstudio.com/docs/languages/java) for detailed instructions
37+
38+
4. **Install Additional Helpful Extensions**
39+
- [Azure Tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-node-azure-pack) for Azure integration
40+
- [GitHub Pull Requests](https://marketplace.visualstudio.com/items?itemName=GitHub.vscode-pull-request-github) for GitHub integration
41+
42+
#### Other IDEs
43+
You can also use other IDEs such as:
44+
- [IntelliJ IDEA](https://www.jetbrains.com/idea/) (Community or Ultimate edition)
45+
- [Eclipse](https://www.eclipse.org/downloads/) with the Maven plugin
46+
47+
### Rename .env.template to .env
48+
49+
Rename the `.env.template` file to `.env` and fill in your Azure credentials:
50+
51+
```properties
52+
AZURE_API_KEY=your_api_key_here
53+
AZURE_ENDPOINT=your_endpoint_here
54+
AZURE_DEPLOYMENT=your_deployment_name_here
55+
```
56+
57+
### Install Dependencies
58+
59+
1. Install the [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli)
60+
2. Log in to Azure:
61+
```bash
62+
az login
63+
```
64+
3. Install Maven dependencies:
65+
```bash
66+
mvn clean install
67+
```
68+
69+
## Create a Project and Model Deployment
70+
71+
Run the CreateProject example to create a new project and model deployment:
72+
73+
```bash
74+
mvn compile exec:java -Dexec.mainClass="com.azure.ai.foundry.samples.CreateProject"
75+
```
76+
77+
Sample code:
78+
79+
```java
80+
// Create a client using your Azure credentials
81+
ProjectsClient client = new ProjectsClientBuilder()
82+
.credential(new AzureKeyCredential(System.getenv("AZURE_API_KEY")))
83+
.endpoint(System.getenv("AZURE_ENDPOINT"))
84+
.buildClient();
85+
86+
// Create a project with a name and description
87+
Project project = client.createProject("My Sample Project", "A project created using the Java SDK");
88+
System.out.println("Created project: " + project.getName() + " with ID: " + project.getId());
89+
```
90+
91+
## Run a Chat Completion
92+
93+
Execute the chat completion example:
94+
95+
```bash
96+
mvn compile exec:java -Dexec.mainClass="com.azure.ai.foundry.samples.ChatCompletionSample"
97+
```
98+
99+
Sample code:
100+
101+
```java
102+
// Create a Projects client
103+
ProjectsClient client = new ProjectsClientBuilder()
104+
.credential(new AzureKeyCredential(System.getenv("AZURE_API_KEY")))
105+
.endpoint(System.getenv("AZURE_ENDPOINT"))
106+
.buildClient();
107+
108+
// Get a chat client from the project
109+
ChatClient chatClient = client.getChatClient(System.getenv("AZURE_DEPLOYMENT"));
110+
111+
// Create chat messages
112+
List<ChatMessage> messages = Arrays.asList(
113+
new ChatMessage(ChatRole.SYSTEM, "You are a helpful assistant."),
114+
new ChatMessage(ChatRole.USER, "Tell me about Azure AI Foundry.")
115+
);
116+
117+
// Get chat completion
118+
ChatCompletionOptions options = new ChatCompletionOptions(messages)
119+
.setTemperature(0.7)
120+
.setMaxTokens(800);
121+
122+
ChatCompletion completion = chatClient.getChatCompletion(options);
123+
System.out.println(completion.getChoices().get(0).getMessage().getContent());
124+
```
125+
126+
## Create and Run an Agent
127+
128+
Create and run an agent that can perform tasks:
129+
130+
```bash
131+
mvn compile exec:java -Dexec.mainClass="com.azure.ai.foundry.samples.AgentSample"
132+
```
133+
134+
Sample code:
135+
136+
```java
137+
// Create a Projects client
138+
ProjectsClient client = new ProjectsClientBuilder()
139+
.credential(new AzureKeyCredential(System.getenv("AZURE_API_KEY")))
140+
.endpoint(System.getenv("AZURE_ENDPOINT"))
141+
.buildClient();
142+
143+
// Get an agent client
144+
AgentClient agentClient = client.getAgentClient();
145+
146+
// Create an agent
147+
Agent agent = agentClient.createAgent(new AgentOptions()
148+
.setName("Research Assistant")
149+
.setDescription("An agent that helps with research tasks")
150+
.setInstructions("You are a research assistant. Help users find information and summarize content.")
151+
.setModel(System.getenv("AZURE_DEPLOYMENT")));
152+
153+
// Run the agent with a task
154+
AgentThread thread = agentClient.createThread();
155+
AgentMessage userMessage = new AgentMessage()
156+
.setRole(AgentRole.USER)
157+
.setContent("Research the benefits of cloud computing");
158+
159+
AgentRun run = agentClient.createRun(thread.getId(), agent.getId(), userMessage);
160+
161+
// Wait for the run to complete and get the results
162+
AgentMessage assistantMessage = agentClient.getMessages(thread.getId())
163+
.stream()
164+
.filter(message -> message.getRole() == AgentRole.ASSISTANT)
165+
.findFirst()
166+
.orElse(null);
167+
168+
if (assistantMessage != null) {
169+
System.out.println("Agent response: " + assistantMessage.getContent());
170+
}
171+
```
172+
173+
## Add File Search to Agent
174+
175+
Add file search capabilities to your agent:
176+
177+
```bash
178+
mvn compile exec:java -Dexec.mainClass="com.azure.ai.foundry.samples.FileSearchAgentSample"
179+
```
180+
181+
Sample code:
182+
183+
```java
184+
// Create a Projects client
185+
ProjectsClient client = new ProjectsClientBuilder()
186+
.credential(new AzureKeyCredential(System.getenv("AZURE_API_KEY")))
187+
.endpoint(System.getenv("AZURE_ENDPOINT"))
188+
.buildClient();
189+
190+
// Get an agent client
191+
AgentClient agentClient = client.getAgentClient();
192+
193+
// Upload a file for search
194+
FileClient fileClient = client.getFileClient();
195+
File uploadedFile = fileClient.uploadFile("path/to/your/document.pdf");
196+
197+
// Add file search capability to the agent
198+
Agent agent = agentClient.createAgent(new AgentOptions()
199+
.setName("Document Assistant")
200+
.setDescription("An agent that helps with document searching")
201+
.setInstructions("You are a document assistant. Help users find information in their documents.")
202+
.setModel(System.getenv("AZURE_DEPLOYMENT"))
203+
.addTool(new FileTool()
204+
.addFile(uploadedFile.getId())));
205+
206+
// Run the agent with a file search query
207+
AgentThread thread = agentClient.createThread();
208+
AgentMessage userMessage = new AgentMessage()
209+
.setRole(AgentRole.USER)
210+
.setContent("Find information about cloud computing in my document");
211+
212+
AgentRun run = agentClient.createRun(thread.getId(), agent.getId(), userMessage);
213+
214+
// Display the search results
215+
AgentMessage assistantMessage = agentClient.getMessages(thread.getId())
216+
.stream()
217+
.filter(message -> message.getRole() == AgentRole.ASSISTANT)
218+
.findFirst()
219+
.orElse(null);
220+
221+
if (assistantMessage != null) {
222+
System.out.println("Search results: " + assistantMessage.getContent());
223+
}
224+
```
225+
226+
## Evaluate Agent Run
227+
228+
Evaluate how well the agent performed on a given task:
229+
230+
```bash
231+
mvn compile exec:java -Dexec.mainClass="com.azure.ai.foundry.samples.EvaluateAgentSample"
232+
```
233+
234+
Sample code:
235+
236+
```java
237+
// Create a Projects client
238+
ProjectsClient client = new ProjectsClientBuilder()
239+
.credential(new AzureKeyCredential(System.getenv("AZURE_API_KEY")))
240+
.endpoint(System.getenv("AZURE_ENDPOINT"))
241+
.buildClient();
242+
243+
// Get an evaluation client
244+
EvaluationClient evaluationClient = client.getEvaluationClient();
245+
246+
// Create an evaluation for an agent run
247+
AgentEvaluation evaluation = evaluationClient.evaluateAgentRun(
248+
"agent-run-id",
249+
new EvaluationOptions()
250+
.addMetric(EvaluationMetric.HELPFULNESS)
251+
.addMetric(EvaluationMetric.ACCURACY)
252+
);
253+
254+
// Display the evaluation results
255+
System.out.println("Evaluation results:");
256+
System.out.println("Helpfulness score: " + evaluation.getScores().get(EvaluationMetric.HELPFULNESS));
257+
System.out.println("Accuracy score: " + evaluation.getScores().get(EvaluationMetric.ACCURACY));
258+
```
259+
260+

0 commit comments

Comments
 (0)