Resource not found error when running miscrosoft semantic-kernel

789 Views Asked by At

I am getting the following error when trying to run semantic-kernel using azure open ai

Error: (<ErrorCodes.ServiceError: 6>, 'OpenAI service failed to complete the chat', InvalidRequestError(message='Resource not found', param=None, code='404', http_status=404, request_id=None))

I am using the following code with reference from https://github.com/microsoft/semantic-kernel/blob/main/python/README.md

from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion

kernel = sk.Kernel()

deployment, api_key, endpoint = "<MY_DEPLOYMENT_NAME>","<MY_API_KEY>", "<MY_ENDPOINT>"
# print(f'kernel.add_chat_service("dv", AzureChatCompletion(deployment_name=deployment, endpoint=endpoint, api_key=api_key))')
kernel.add_chat_service("dv", AzureChatCompletion(deployment_name=deployment, endpoint=endpoint, api_key=api_key))
# Wrap your prompt in a function
prompt = kernel.create_semantic_function("""
Write a simple hello-world python program.
""")

# Run your prompt
print(prompt()) 

When I tried finding solution, I ran print(f'') statement over kerrnel.add_chat_service And I got following error

Traceback (most recent call last):
  File "C:\Users\yashroop.rai\Desktop\semantic-kernel\semantic-kernel.py", line 10, in <module>
    prompt = kernel.create_semantic_function("""
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\yashroop.rai\AppData\Local\Programs\Python\Python311\Lib\site-packages\semantic_kernel\kernel.py", line 851, in create_semantic_function
    return self.register_semantic_function(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\yashroop.rai\AppData\Local\Programs\Python\Python311\Lib\site-packages\semantic_kernel\kernel.py", line 129, in register_semantic_function       
    function = self._create_semantic_function(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\yashroop.rai\AppData\Local\Programs\Python\Python311\Lib\site-packages\semantic_kernel\kernel.py", line 692, in _create_semantic_function        
    service = self.get_ai_service(
              ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\yashroop.rai\AppData\Local\Programs\Python\Python311\Lib\site-packages\semantic_kernel\kernel.py", line 429, in get_ai_service
    raise ValueError(
ValueError: TextCompletionClientBase service with service_id 'None' not found

Any solutions ? The endpoint and api_key are correct because I have been using that as a normal POST request through api call which works fine.

1

There are 1 best solutions below

4
On

Resource not found error when running Microsoft semantic-kernel

The above error occurs when you pass an incorrect deployment name or api_key or endpoint in your code.

I created Azure open ai service and deployed gpt-35-turbo model with the name of deploymentname1.

Portal:

enter image description here

Now using the below code, I can able to run semantic-kernel with Azure open ai using Python.

Code:

from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
import semantic_kernel as sk

kernel = sk.Kernel()

deployment="deploymentname1"
api_key="your-azure-api-key"
endpoint="https://<resource name>.openai.azure.com/"
kernel.add_chat_service("dv", AzureChatCompletion(deployment_name=deployment, endpoint=endpoint, api_key=api_key))

prompt = kernel.create_semantic_function("""
Write a simple palindrome python program.
""")

# Run your prompt
print(prompt()) 

Output:

Here's a simple palindrome python program:

```
word = input("Enter a word: ")

if word == word[::-1]:
    print("The word is a palindrome!")
else:
    print("The word is not a palindrome.")
```

This program prompts the user to enter a word, and then checks if the word is the same forwards and backwards (i.e. a palindrome) using slicing. If the word is a palindrome, it prints a message saying so. Otherwise, it prints a message saying the word is not a palindrome.

enter image description here

Reference:

Adding AI services to Semantic Kernel | Microsoft Learn