forked from redhat-developer/quarkus-ls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Available Ollama models support for
quarkus.langchain4j.ollama.chat-model.model-id property value Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
9f4c520
commit 3b4fcd1
Showing
6 changed files
with
225 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
...kus.ls/src/main/java/com/redhat/quarkus/extensions/ollama/OllamaItemMetadataProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat Inc. and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
* which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.quarkus.extensions.ollama; | ||
|
||
import java.io.StringReader; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.net.http.HttpResponse.BodyHandlers; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.lsp4mp.commons.metadata.ItemHint; | ||
import org.eclipse.lsp4mp.commons.metadata.ItemMetadata; | ||
import org.eclipse.lsp4mp.commons.metadata.ValueHint; | ||
import org.eclipse.lsp4mp.commons.utils.StringUtils; | ||
import org.eclipse.lsp4mp.extensions.ExtendedMicroProfileProjectInfo; | ||
import org.eclipse.lsp4mp.extensions.ItemMetadataProvider; | ||
import org.eclipse.lsp4mp.model.Node; | ||
import org.eclipse.lsp4mp.model.Node.NodeType; | ||
import org.eclipse.lsp4mp.model.PropertiesModel; | ||
import org.eclipse.lsp4mp.model.Property; | ||
|
||
import com.google.gson.GsonBuilder; | ||
|
||
/** | ||
* Properties provider for available Ollama models. | ||
*/ | ||
public class OllamaItemMetadataProvider implements ItemMetadataProvider { | ||
|
||
private static final String QUARKUS_LANGCHAIN4J_OLLAMA_CHAT_MODEL_MODEL_ID_KEY = "quarkus.langchain4j.ollama.chat-model.model-id"; | ||
|
||
private static final String QUARKUS_LANGCHAIN4J_OLLAMA_BASE_URL_KEY = "quarkus.langchain4j.ollama.base-url"; | ||
|
||
private static final String DEFAULT_SEARCH_API = "http://localhost:11434"; | ||
|
||
private ExtendedMicroProfileProjectInfo projectInfo; | ||
private boolean available; | ||
|
||
private String currentSearchUrl; | ||
|
||
public OllamaItemMetadataProvider(ExtendedMicroProfileProjectInfo projectInfo) { | ||
this.projectInfo = projectInfo; | ||
this.available = this.projectInfo.getProperties().stream() | ||
.anyMatch(p -> QUARKUS_LANGCHAIN4J_OLLAMA_CHAT_MODEL_MODEL_ID_KEY.equals(p.getName())); | ||
} | ||
|
||
private List<ValueHint> collectOllamaModels(String searchUrl) { | ||
List<ValueHint> models = new ArrayList<>(); | ||
|
||
try { | ||
HttpRequest request = HttpRequest.newBuilder() // | ||
.uri(new URI(searchUrl)) // | ||
.GET() // | ||
.build(); | ||
|
||
HttpResponse<String> response = HttpClient.newBuilder() // | ||
.build() // | ||
.send(request, BodyHandlers.ofString()); | ||
String result = response.body(); | ||
// ex : | ||
// {"object":"list","data":[{"id":"qwen2:latest","object":"model","created":1724172988,"owned_by":"library"}]} | ||
OllamaResult r = new GsonBuilder().create().fromJson(new StringReader(result), OllamaResult.class); | ||
if (r != null && r.getData() != null) { | ||
return r.getData() // | ||
.stream() // | ||
.map(m -> { | ||
ValueHint model = new ValueHint(); | ||
model.setValue(m.getId()); | ||
return model; | ||
}) // | ||
.collect(Collectors.toList()); | ||
|
||
} | ||
|
||
} catch (Exception e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
|
||
ValueHint model1 = new ValueHint(); | ||
model1.setValue("Model 1"); | ||
models.add(model1); | ||
|
||
ValueHint model2 = new ValueHint(); | ||
model2.setValue("Model 2"); | ||
models.add(model2); | ||
|
||
return models; | ||
} | ||
|
||
@Override | ||
public boolean isAvailable() { | ||
return available; | ||
} | ||
|
||
@Override | ||
public void update(PropertiesModel document) { | ||
boolean hasModelProperty = getProperty(document, QUARKUS_LANGCHAIN4J_OLLAMA_CHAT_MODEL_MODEL_ID_KEY) != null; | ||
if (hasModelProperty) { | ||
String searchUrl = getSearchUrl(document); | ||
if (!Objects.equals(searchUrl, currentSearchUrl)) { | ||
ItemHint hint = projectInfo.getHint(QUARKUS_LANGCHAIN4J_OLLAMA_CHAT_MODEL_MODEL_ID_KEY); | ||
if (hint == null) { | ||
hint = new ItemHint(); | ||
hint.setName(QUARKUS_LANGCHAIN4J_OLLAMA_CHAT_MODEL_MODEL_ID_KEY); | ||
projectInfo.getHints().add(hint); | ||
} | ||
hint.setValues(collectOllamaModels(searchUrl)); | ||
currentSearchUrl = searchUrl; | ||
} | ||
} | ||
} | ||
|
||
private String getSearchUrl(PropertiesModel document) { | ||
Property baseUrlProperty = getProperty(document, QUARKUS_LANGCHAIN4J_OLLAMA_BASE_URL_KEY); | ||
if (baseUrlProperty != null) { | ||
String propertyValue = baseUrlProperty.getPropertyValue(); | ||
if (StringUtils.hasText(propertyValue)) { | ||
return propertyValue; | ||
} | ||
} | ||
return DEFAULT_SEARCH_API; | ||
} | ||
|
||
private Property getProperty(PropertiesModel document, String propertyName) { | ||
for (Node node : document.getChildren()) { | ||
if (node.getNodeType() == NodeType.PROPERTY) { | ||
Property property = (Property) node; | ||
if (propertyName.equals(property.getPropertyName())) { | ||
return property; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public List<ItemMetadata> getProperties() { | ||
return null; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...src/main/java/com/redhat/quarkus/extensions/ollama/OllamaItemMetadataProviderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat Inc. and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
* which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.quarkus.extensions.ollama; | ||
|
||
import org.eclipse.lsp4mp.extensions.ExtendedMicroProfileProjectInfo; | ||
import org.eclipse.lsp4mp.extensions.ItemMetadataProvider; | ||
import org.eclipse.lsp4mp.extensions.ItemMetadataProviderFactory; | ||
|
||
/** | ||
* Factory for creating {@link ItemMetadataProvider} instance for available | ||
* Ollama models. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class OllamaItemMetadataProviderFactory implements ItemMetadataProviderFactory { | ||
|
||
@Override | ||
public ItemMetadataProvider create(ExtendedMicroProfileProjectInfo projectInfo) { | ||
return new OllamaItemMetadataProvider(projectInfo); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...com.redhat.quarkus.ls/src/main/java/com/redhat/quarkus/extensions/ollama/OllamaModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
package com.redhat.quarkus.extensions.ollama; | ||
|
||
public class OllamaModel { | ||
|
||
private String id; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...om.redhat.quarkus.ls/src/main/java/com/redhat/quarkus/extensions/ollama/OllamaResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.redhat.quarkus.extensions.ollama; | ||
|
||
import java.util.List; | ||
|
||
public class OllamaResult { | ||
|
||
private List<OllamaModel> data; | ||
|
||
public List<OllamaModel> getData() { | ||
return data; | ||
} | ||
|
||
public void setData(List<OllamaModel> data) { | ||
this.data = data; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ain/resources/META-INF/services/org.eclipse.lsp4mp.extensions.ItemMetadataProviderFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.redhat.quarkus.extensions.ollama.OllamaItemMetadataProviderFactory |