-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from intelligentnode/86-add-nvidia-models
Add Nvidia models
- Loading branch information
Showing
9 changed files
with
236 additions
and
0 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
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
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
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
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
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,45 @@ | ||
import unittest | ||
import os | ||
import asyncio | ||
from dotenv import load_dotenv | ||
from intelli.function.chatbot import Chatbot, ChatProvider | ||
from intelli.model.input.chatbot_input import ChatModelInput | ||
|
||
load_dotenv() | ||
|
||
class TestChatbotNvidiaChatAndStream(unittest.TestCase): | ||
def setUp(self): | ||
self.nvidia_api_key = os.getenv("NVIDIA_API_KEY") | ||
assert self.nvidia_api_key, "NVIDIA_API_KEY is not set." | ||
self.chatbot = Chatbot(self.nvidia_api_key, ChatProvider.NVIDIA.value) | ||
|
||
def test_nvidia_chat_and_stream(self): | ||
|
||
# Test normal chat | ||
print("Testing Nvidia chat") | ||
normal_input = ChatModelInput("You are a helpful assistant.", model="deepseek-ai/deepseek-r1", max_tokens=1024, temperature=0.6) | ||
normal_input.add_user_message("What is the capital city of france?") | ||
response = self.chatbot.chat(normal_input) | ||
if isinstance(response, dict) and "result" in response: | ||
normal_output = response["result"] | ||
else: | ||
normal_output = response | ||
self.assertTrue(len(normal_output) > 0, "Nvidia normal chat response should not be empty") | ||
print("Nvidia normal chat output:", normal_output) | ||
|
||
# Test streaming chat | ||
print("Testing Nvidia stream") | ||
stream_input = ChatModelInput("You are a helpful assistant.", model="deepseek-ai/deepseek-r1", max_tokens=1024, temperature=0.6) | ||
stream_input.add_user_message("What is the capital city of france?") | ||
stream_output = asyncio.run(self.get_stream_output(stream_input)) | ||
self.assertTrue(len(stream_output) > 0, "Nvidia stream response should not be empty") | ||
print("Nvidia stream output:", stream_output) | ||
|
||
async def get_stream_output(self, chat_input): | ||
output = "" | ||
for chunk in self.chatbot.stream(chat_input): | ||
output += chunk | ||
return output | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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,68 @@ | ||
import unittest | ||
import os | ||
from dotenv import load_dotenv | ||
from intelli.wrappers.nvidia_wrapper import NvidiaWrapper | ||
|
||
load_dotenv() | ||
|
||
|
||
class TestNvidiaWrapper(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.api_key = os.getenv("NVIDIA_API_KEY") | ||
assert cls.api_key, "NVIDIA_API_KEY is not set." | ||
cls.wrapper = NvidiaWrapper(cls.api_key) | ||
|
||
def test_generate_text_llama(self): | ||
params = { | ||
"model": "meta/llama-3.3-70b-instruct", | ||
"messages": [ | ||
{"role": "user", "content": "Write a limerick about GPU computing."} | ||
], | ||
"max_tokens": 1024, | ||
"temperature": 0.2, | ||
"top_p": 0.7, | ||
"stream": False, | ||
} | ||
response = self.wrapper.generate_text(params) | ||
self.assertIn("choices", response) | ||
self.assertGreater(len(response["choices"]), 0) | ||
message = response["choices"][0]["message"]["content"] | ||
self.assertTrue(isinstance(message, str) and len(message) > 0) | ||
|
||
def test_generate_text_deepseek(self): | ||
params = { | ||
"model": "deepseek-ai/deepseek-r1", | ||
"messages": [ | ||
{"role": "user", "content": "Which number is larger, 9.11 or 9.8?"} | ||
], | ||
"max_tokens": 4096, | ||
"temperature": 0.6, | ||
"top_p": 0.7, | ||
"stream": False, | ||
} | ||
response = self.wrapper.generate_text(params) | ||
self.assertIn("choices", response) | ||
self.assertGreater(len(response["choices"]), 0) | ||
message = response["choices"][0]["message"]["content"] | ||
self.assertTrue(isinstance(message, str) and len(message) > 0) | ||
|
||
def test_get_embeddings(self): | ||
params = { | ||
"input": ["What is the capital of France?"], | ||
"model": "nvidia/llama-3.2-nv-embedqa-1b-v2", | ||
"input_type": "query", | ||
"encoding_format": "float", | ||
"truncate": "NONE", | ||
} | ||
response = self.wrapper.get_embeddings(params) | ||
self.assertIn("data", response) | ||
self.assertGreater(len(response["data"]), 0) | ||
self.assertIn("embedding", response["data"][0]) | ||
embedding = response["data"][0]["embedding"] | ||
self.assertIsInstance(embedding, list) | ||
self.assertGreater(len(embedding), 0) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
29 changes: 29 additions & 0 deletions
29
intelli/test/integration/test_remote_embed_model_nvidia.py
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,29 @@ | ||
import unittest | ||
import os | ||
from dotenv import load_dotenv | ||
from intelli.model.input.embed_input import EmbedInput | ||
from intelli.controller.remote_embed_model import RemoteEmbedModel | ||
|
||
load_dotenv() | ||
|
||
class TestRemoteEmbedModelNvidia(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.api_key = os.getenv("NVIDIA_API_KEY") | ||
assert cls.api_key, "NVIDIA_API_KEY is not set." | ||
cls.embed_model = RemoteEmbedModel(cls.api_key, "nvidia") | ||
|
||
def test_get_embeddings(self): | ||
text = "What is the capital of France?" | ||
embed_input = EmbedInput([text], model="nvidia/llama-3.2-nv-embedqa-1b-v2") | ||
result = self.embed_model.get_embeddings(embed_input) | ||
self.assertIn("data", result) | ||
self.assertGreater(len(result["data"]), 0) | ||
self.assertIn("embedding", result["data"][0]) | ||
embedding = result["data"][0]["embedding"] | ||
self.assertIsInstance(embedding, list) | ||
self.assertGreater(len(embedding), 0) | ||
print("Nvidia embedding sample:", embedding[:5]) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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,38 @@ | ||
import requests | ||
from intelli.config import config | ||
|
||
|
||
class NvidiaWrapper: | ||
def __init__(self, api_key: str): | ||
self.api_key = api_key | ||
self.base_url = config["url"]["nvidia"]["base"] | ||
self.chat_endpoint = config["url"]["nvidia"]["chat"] | ||
self.embeddings_endpoint = config["url"]["nvidia"]["embeddings"] | ||
self.headers = { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
"Authorization": f"Bearer {api_key}", | ||
} | ||
|
||
def generate_text(self, params: dict) -> dict: | ||
if "stream" not in params: | ||
params["stream"] = False | ||
url = self.base_url + self.chat_endpoint | ||
response = requests.post(url, json=params, headers=self.headers) | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
def generate_text_stream(self, params: dict): | ||
params["stream"] = True | ||
url = self.base_url + self.chat_endpoint | ||
response = requests.post(url, json=params, headers=self.headers, stream=True) | ||
response.raise_for_status() | ||
for line in response.iter_lines(decode_unicode=True): | ||
if line: | ||
yield line | ||
|
||
def get_embeddings(self, params: dict) -> dict: | ||
url = self.base_url + self.embeddings_endpoint | ||
response = requests.post(url, json=params, headers=self.headers) | ||
response.raise_for_status() | ||
return response.json() |