[GH-ISSUE #3221] How to catch errors using ollama compatibility with OpenAI API #1984

Closed
opened 2026-04-12 12:10:35 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @ejgutierrez74 on GitHub (Mar 18, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/3221

Hi, im trying this code:

def llama_openaiv2(prompt, 
          add_inst=True, #By default True, if you use a base model should write it as False
          model="llama2", 
          temperature=0.0, #By default in openai is 1.0 o 0.7 depends of the model, openai from 0.0 to 2.0, llama2 from 0.0 to 1.0
          max_tokens=1024,
          verbose=False
         ):
    
    if add_inst:
        prompt = f"[INST]{prompt}[/INST]"

    if verbose:
        print(f"Prompt:\n{prompt}\n")
        print(f"model: {model}")

    error = 0
    try:
        response = client.chat.completions.create(
            messages=[
                        {
                        'role': 'user',
                        'content': prompt,
                        }
                    ],
            model=model,
            max_tokens=max_tokens,
            temperature=temperature
                )

    except openai.APIError as e:
      #Handle API error here, e.g. retry or log
      print(f"Llama2: OpenAI API returned an API Error:{e} ")
      error = 1
      pass
    except openai.APIConnectionError as e:
      #Handle connection error here
      print(f"Llama2:Failed to connect to OpenAI API:{e} ")
      error = 2
      pass
    except openai.RateLimitError as e:
      #Handle rate limit error (we recommend using exponential backoff)
      print(f"Llama2: OpenAI API request exceeded rate limit:{e} ")
      error = 3
      pass

 
    #Si interesa saber el response entero print(f"response object: {response}")
    #Solo vamos a devolver la respuesta del sistema
           
    return (response.choices[0].message.content, error)

So according to the original OpenAI API:

https://platform.openai.com/docs/guides/error-codes/python-library-error-types

This is an example code:

import openai
from openai import OpenAI
client = OpenAI()

try:
  #Make your OpenAI API request here
  response = client.completions.create(
    prompt="Hello world",
    model="gpt-3.5-turbo-instruct"
  )
except openai.APIError as e:
  #Handle API error here, e.g. retry or log
  print(f"OpenAI API returned an API Error: {e}")
  pass
except openai.APIConnectionError as e:
  #Handle connection error here
  print(f"Failed to connect to OpenAI API: {e}")
  pass
except openai.RateLimitError as e:
  #Handle rate limit error (we recommend using exponential backoff)
  print(f"OpenAI API request exceeded rate limit: {e}")
  pass

But im facing that running my function, jupyter netbook raises and error, so somehow the errors arent catched properly.

For example, if i dont have ollama server running, should give an Connection refused or Connection Error. This error should be catched, but my jupyter netbook seems cant catch it. In concrete i have this error when ollama serve is stopped:

---------------------------------------------------------------------------
ConnectionRefusedError                    Traceback (most recent call last)
File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_exceptions.py:10, in map_exceptions(map)
      9 try:
---> 10     yield
     11 except Exception as exc:  # noqa: PIE786

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_backends/sync.py:206, in SyncBackend.connect_tcp(self, host, port, timeout, local_address, socket_options)
    205 with map_exceptions(exc_map):
--> 206     sock = socket.create_connection(
    207         address,
    208         timeout,
    209         source_address=source_address,
    210     )
    211     for option in socket_options:

File /opt/anaconda3/lib/python3.11/socket.py:851, in create_connection(address, timeout, source_address, all_errors)
    850 if not all_errors:
--> 851     raise exceptions[0]
    852 raise ExceptionGroup("create_connection failed", exceptions)

File /opt/anaconda3/lib/python3.11/socket.py:836, in create_connection(address, timeout, source_address, all_errors)
    835     sock.bind(source_address)
--> 836 sock.connect(sa)
    837 # Break explicitly a reference cycle

ConnectionRefusedError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

ConnectError                              Traceback (most recent call last)
File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:67, in map_httpcore_exceptions()
     66 try:
---> 67     yield
     68 except Exception as exc:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:231, in HTTPTransport.handle_request(self, request)
    230 with map_httpcore_exceptions():
--> 231     resp = self._pool.handle_request(req)
    233 assert isinstance(resp.stream, typing.Iterable)

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py:268, in ConnectionPool.handle_request(self, request)
    267         self.response_closed(status)
--> 268     raise exc
    269 else:

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py:251, in ConnectionPool.handle_request(self, request)
    250 try:
--> 251     response = connection.handle_request(request)
    252 except ConnectionNotAvailable:
    253     # The ConnectionNotAvailable exception is a special case, that
    254     # indicates we need to retry the request on a new connection.
   (...)
    258     # might end up as an HTTP/2 connection, but which actually ends
    259     # up as HTTP/1.1.

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:99, in HTTPConnection.handle_request(self, request)
     98         self._connect_failed = True
---> 99         raise exc
    100 elif not self._connection.is_available():

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:76, in HTTPConnection.handle_request(self, request)
     75 try:
---> 76     stream = self._connect(request)
     78     ssl_object = stream.get_extra_info("ssl_object")

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:124, in HTTPConnection._connect(self, request)
    123 with Trace("connect_tcp", logger, request, kwargs) as trace:
--> 124     stream = self._network_backend.connect_tcp(**kwargs)
    125     trace.return_value = stream

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_backends/sync.py:205, in SyncBackend.connect_tcp(self, host, port, timeout, local_address, socket_options)
    200 exc_map: ExceptionMapping = {
    201     socket.timeout: ConnectTimeout,
    202     OSError: ConnectError,
    203 }
--> 205 with map_exceptions(exc_map):
    206     sock = socket.create_connection(
    207         address,
    208         timeout,
    209         source_address=source_address,
    210     )

File /opt/anaconda3/lib/python3.11/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback)
    157 try:
--> 158     self.gen.throw(typ, value, traceback)
    159 except StopIteration as exc:
    160     # Suppress StopIteration *unless* it's the same exception that
    161     # was passed to throw().  This prevents a StopIteration
    162     # raised inside the "with" statement from being suppressed.

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_exceptions.py:14, in map_exceptions(map)
     13     if isinstance(exc, from_exc):
---> 14         raise to_exc(exc) from exc
     15 raise

ConnectError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

ConnectError                              Traceback (most recent call last)
File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:897, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
    896 try:
--> 897     response = self._client.send(
    898         request,
    899         stream=stream or self._should_stream_response_body(request=request),
    900         **kwargs,
    901     )
    902 except httpx.TimeoutException as err:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:915, in Client.send(self, request, stream, auth, follow_redirects)
    913 auth = self._build_request_auth(request, auth)
--> 915 response = self._send_handling_auth(
    916     request,
    917     auth=auth,
    918     follow_redirects=follow_redirects,
    919     history=[],
    920 )
    921 try:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:943, in Client._send_handling_auth(self, request, auth, follow_redirects, history)
    942 while True:
--> 943     response = self._send_handling_redirects(
    944         request,
    945         follow_redirects=follow_redirects,
    946         history=history,
    947     )
    948     try:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:980, in Client._send_handling_redirects(self, request, follow_redirects, history)
    978     hook(request)
--> 980 response = self._send_single_request(request)
    981 try:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:1016, in Client._send_single_request(self, request)
   1015 with request_context(request=request):
-> 1016     response = transport.handle_request(request)
   1018 assert isinstance(response.stream, SyncByteStream)

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:230, in HTTPTransport.handle_request(self, request)
    218 req = httpcore.Request(
    219     method=request.method,
    220     url=httpcore.URL(
   (...)
    228     extensions=request.extensions,
    229 )
--> 230 with map_httpcore_exceptions():
    231     resp = self._pool.handle_request(req)

File /opt/anaconda3/lib/python3.11/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback)
    157 try:
--> 158     self.gen.throw(typ, value, traceback)
    159 except StopIteration as exc:
    160     # Suppress StopIteration *unless* it's the same exception that
    161     # was passed to throw().  This prevents a StopIteration
    162     # raised inside the "with" statement from being suppressed.

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:84, in map_httpcore_exceptions()
     83 message = str(exc)
---> 84 raise mapped_exc(message) from exc

ConnectError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

ConnectionRefusedError                    Traceback (most recent call last)
File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_exceptions.py:10, in map_exceptions(map)
      9 try:
---> 10     yield
     11 except Exception as exc:  # noqa: PIE786

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_backends/sync.py:206, in SyncBackend.connect_tcp(self, host, port, timeout, local_address, socket_options)
    205 with map_exceptions(exc_map):
--> 206     sock = socket.create_connection(
    207         address,
    208         timeout,
    209         source_address=source_address,
    210     )
    211     for option in socket_options:

File /opt/anaconda3/lib/python3.11/socket.py:851, in create_connection(address, timeout, source_address, all_errors)
    850 if not all_errors:
--> 851     raise exceptions[0]
    852 raise ExceptionGroup("create_connection failed", exceptions)

File /opt/anaconda3/lib/python3.11/socket.py:836, in create_connection(address, timeout, source_address, all_errors)
    835     sock.bind(source_address)
--> 836 sock.connect(sa)
    837 # Break explicitly a reference cycle

ConnectionRefusedError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

ConnectError                              Traceback (most recent call last)
File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:67, in map_httpcore_exceptions()
     66 try:
---> 67     yield
     68 except Exception as exc:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:231, in HTTPTransport.handle_request(self, request)
    230 with map_httpcore_exceptions():
--> 231     resp = self._pool.handle_request(req)
    233 assert isinstance(resp.stream, typing.Iterable)

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py:268, in ConnectionPool.handle_request(self, request)
    267         self.response_closed(status)
--> 268     raise exc
    269 else:

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py:251, in ConnectionPool.handle_request(self, request)
    250 try:
--> 251     response = connection.handle_request(request)
    252 except ConnectionNotAvailable:
    253     # The ConnectionNotAvailable exception is a special case, that
    254     # indicates we need to retry the request on a new connection.
   (...)
    258     # might end up as an HTTP/2 connection, but which actually ends
    259     # up as HTTP/1.1.

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:99, in HTTPConnection.handle_request(self, request)
     98         self._connect_failed = True
---> 99         raise exc
    100 elif not self._connection.is_available():

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:76, in HTTPConnection.handle_request(self, request)
     75 try:
---> 76     stream = self._connect(request)
     78     ssl_object = stream.get_extra_info("ssl_object")

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:124, in HTTPConnection._connect(self, request)
    123 with Trace("connect_tcp", logger, request, kwargs) as trace:
--> 124     stream = self._network_backend.connect_tcp(**kwargs)
    125     trace.return_value = stream

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_backends/sync.py:205, in SyncBackend.connect_tcp(self, host, port, timeout, local_address, socket_options)
    200 exc_map: ExceptionMapping = {
    201     socket.timeout: ConnectTimeout,
    202     OSError: ConnectError,
    203 }
--> 205 with map_exceptions(exc_map):
    206     sock = socket.create_connection(
    207         address,
    208         timeout,
    209         source_address=source_address,
    210     )

File /opt/anaconda3/lib/python3.11/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback)
    157 try:
--> 158     self.gen.throw(typ, value, traceback)
    159 except StopIteration as exc:
    160     # Suppress StopIteration *unless* it's the same exception that
    161     # was passed to throw().  This prevents a StopIteration
    162     # raised inside the "with" statement from being suppressed.

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_exceptions.py:14, in map_exceptions(map)
     13     if isinstance(exc, from_exc):
---> 14         raise to_exc(exc) from exc
     15 raise

ConnectError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

ConnectError                              Traceback (most recent call last)
File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:897, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
    896 try:
--> 897     response = self._client.send(
    898         request,
    899         stream=stream or self._should_stream_response_body(request=request),
    900         **kwargs,
    901     )
    902 except httpx.TimeoutException as err:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:915, in Client.send(self, request, stream, auth, follow_redirects)
    913 auth = self._build_request_auth(request, auth)
--> 915 response = self._send_handling_auth(
    916     request,
    917     auth=auth,
    918     follow_redirects=follow_redirects,
    919     history=[],
    920 )
    921 try:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:943, in Client._send_handling_auth(self, request, auth, follow_redirects, history)
    942 while True:
--> 943     response = self._send_handling_redirects(
    944         request,
    945         follow_redirects=follow_redirects,
    946         history=history,
    947     )
    948     try:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:980, in Client._send_handling_redirects(self, request, follow_redirects, history)
    978     hook(request)
--> 980 response = self._send_single_request(request)
    981 try:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:1016, in Client._send_single_request(self, request)
   1015 with request_context(request=request):
-> 1016     response = transport.handle_request(request)
   1018 assert isinstance(response.stream, SyncByteStream)

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:230, in HTTPTransport.handle_request(self, request)
    218 req = httpcore.Request(
    219     method=request.method,
    220     url=httpcore.URL(
   (...)
    228     extensions=request.extensions,
    229 )
--> 230 with map_httpcore_exceptions():
    231     resp = self._pool.handle_request(req)

File /opt/anaconda3/lib/python3.11/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback)
    157 try:
--> 158     self.gen.throw(typ, value, traceback)
    159 except StopIteration as exc:
    160     # Suppress StopIteration *unless* it's the same exception that
    161     # was passed to throw().  This prevents a StopIteration
    162     # raised inside the "with" statement from being suppressed.

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:84, in map_httpcore_exceptions()
     83 message = str(exc)
---> 84 raise mapped_exc(message) from exc

ConnectError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

ConnectionRefusedError                    Traceback (most recent call last)
File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_exceptions.py:10, in map_exceptions(map)
      9 try:
---> 10     yield
     11 except Exception as exc:  # noqa: PIE786

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_backends/sync.py:206, in SyncBackend.connect_tcp(self, host, port, timeout, local_address, socket_options)
    205 with map_exceptions(exc_map):
--> 206     sock = socket.create_connection(
    207         address,
    208         timeout,
    209         source_address=source_address,
    210     )
    211     for option in socket_options:

File /opt/anaconda3/lib/python3.11/socket.py:851, in create_connection(address, timeout, source_address, all_errors)
    850 if not all_errors:
--> 851     raise exceptions[0]
    852 raise ExceptionGroup("create_connection failed", exceptions)

File /opt/anaconda3/lib/python3.11/socket.py:836, in create_connection(address, timeout, source_address, all_errors)
    835     sock.bind(source_address)
--> 836 sock.connect(sa)
    837 # Break explicitly a reference cycle

ConnectionRefusedError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

ConnectError                              Traceback (most recent call last)
File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:67, in map_httpcore_exceptions()
     66 try:
---> 67     yield
     68 except Exception as exc:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:231, in HTTPTransport.handle_request(self, request)
    230 with map_httpcore_exceptions():
--> 231     resp = self._pool.handle_request(req)
    233 assert isinstance(resp.stream, typing.Iterable)

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py:268, in ConnectionPool.handle_request(self, request)
    267         self.response_closed(status)
--> 268     raise exc
    269 else:

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py:251, in ConnectionPool.handle_request(self, request)
    250 try:
--> 251     response = connection.handle_request(request)
    252 except ConnectionNotAvailable:
    253     # The ConnectionNotAvailable exception is a special case, that
    254     # indicates we need to retry the request on a new connection.
   (...)
    258     # might end up as an HTTP/2 connection, but which actually ends
    259     # up as HTTP/1.1.

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:99, in HTTPConnection.handle_request(self, request)
     98         self._connect_failed = True
---> 99         raise exc
    100 elif not self._connection.is_available():

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:76, in HTTPConnection.handle_request(self, request)
     75 try:
---> 76     stream = self._connect(request)
     78     ssl_object = stream.get_extra_info("ssl_object")

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:124, in HTTPConnection._connect(self, request)
    123 with Trace("connect_tcp", logger, request, kwargs) as trace:
--> 124     stream = self._network_backend.connect_tcp(**kwargs)
    125     trace.return_value = stream

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_backends/sync.py:205, in SyncBackend.connect_tcp(self, host, port, timeout, local_address, socket_options)
    200 exc_map: ExceptionMapping = {
    201     socket.timeout: ConnectTimeout,
    202     OSError: ConnectError,
    203 }
--> 205 with map_exceptions(exc_map):
    206     sock = socket.create_connection(
    207         address,
    208         timeout,
    209         source_address=source_address,
    210     )

File /opt/anaconda3/lib/python3.11/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback)
    157 try:
--> 158     self.gen.throw(typ, value, traceback)
    159 except StopIteration as exc:
    160     # Suppress StopIteration *unless* it's the same exception that
    161     # was passed to throw().  This prevents a StopIteration
    162     # raised inside the "with" statement from being suppressed.

File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_exceptions.py:14, in map_exceptions(map)
     13     if isinstance(exc, from_exc):
---> 14         raise to_exc(exc) from exc
     15 raise

ConnectError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

ConnectError                              Traceback (most recent call last)
File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:897, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
    896 try:
--> 897     response = self._client.send(
    898         request,
    899         stream=stream or self._should_stream_response_body(request=request),
    900         **kwargs,
    901     )
    902 except httpx.TimeoutException as err:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:915, in Client.send(self, request, stream, auth, follow_redirects)
    913 auth = self._build_request_auth(request, auth)
--> 915 response = self._send_handling_auth(
    916     request,
    917     auth=auth,
    918     follow_redirects=follow_redirects,
    919     history=[],
    920 )
    921 try:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:943, in Client._send_handling_auth(self, request, auth, follow_redirects, history)
    942 while True:
--> 943     response = self._send_handling_redirects(
    944         request,
    945         follow_redirects=follow_redirects,
    946         history=history,
    947     )
    948     try:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:980, in Client._send_handling_redirects(self, request, follow_redirects, history)
    978     hook(request)
--> 980 response = self._send_single_request(request)
    981 try:

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:1016, in Client._send_single_request(self, request)
   1015 with request_context(request=request):
-> 1016     response = transport.handle_request(request)
   1018 assert isinstance(response.stream, SyncByteStream)

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:230, in HTTPTransport.handle_request(self, request)
    218 req = httpcore.Request(
    219     method=request.method,
    220     url=httpcore.URL(
   (...)
    228     extensions=request.extensions,
    229 )
--> 230 with map_httpcore_exceptions():
    231     resp = self._pool.handle_request(req)

File /opt/anaconda3/lib/python3.11/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback)
    157 try:
--> 158     self.gen.throw(typ, value, traceback)
    159 except StopIteration as exc:
    160     # Suppress StopIteration *unless* it's the same exception that
    161     # was passed to throw().  This prevents a StopIteration
    162     # raised inside the "with" statement from being suppressed.

File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:84, in map_httpcore_exceptions()
     83 message = str(exc)
---> 84 raise mapped_exc(message) from exc

ConnectError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

APIConnectionError                        Traceback (most recent call last)
Cell In[32], line 18, in llama_openaiv2(prompt, add_inst, model, temperature, max_tokens, verbose)
     17 try:
---> 18     response = client.chat.completions.create(
     19         messages=[
     20                     {
     21                     'role': 'user',
     22                     'content': prompt,
     23                     }
     24                 ],
     25         model=model,
     26         max_tokens=max_tokens,
     27         temperature=temperature
     28             )
     30 except openai.APIError as e:
     31   #Handle API error here, e.g. retry or log

File /opt/anaconda3/lib/python3.11/site-packages/openai/_utils/_utils.py:271, in required_args.<locals>.inner.<locals>.wrapper(*args, **kwargs)
    270     raise TypeError(msg)
--> 271 return func(*args, **kwargs)

File /opt/anaconda3/lib/python3.11/site-packages/openai/resources/chat/completions.py:648, in Completions.create(self, messages, model, frequency_penalty, function_call, functions, logit_bias, logprobs, max_tokens, n, presence_penalty, response_format, seed, stop, stream, temperature, tool_choice, tools, top_logprobs, top_p, user, extra_headers, extra_query, extra_body, timeout)
    599 @required_args(["messages", "model"], ["messages", "model", "stream"])
    600 def create(
    601     self,
   (...)
    646     timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
    647 ) -> ChatCompletion | Stream[ChatCompletionChunk]:
--> 648     return self._post(
    649         "/chat/completions",
    650         body=maybe_transform(
    651             {
    652                 "messages": messages,
    653                 "model": model,
    654                 "frequency_penalty": frequency_penalty,
    655                 "function_call": function_call,
    656                 "functions": functions,
    657                 "logit_bias": logit_bias,
    658                 "logprobs": logprobs,
    659                 "max_tokens": max_tokens,
    660                 "n": n,
    661                 "presence_penalty": presence_penalty,
    662                 "response_format": response_format,
    663                 "seed": seed,
    664                 "stop": stop,
    665                 "stream": stream,
    666                 "temperature": temperature,
    667                 "tool_choice": tool_choice,
    668                 "tools": tools,
    669                 "top_logprobs": top_logprobs,
    670                 "top_p": top_p,
    671                 "user": user,
    672             },
    673             completion_create_params.CompletionCreateParams,
    674         ),
    675         options=make_request_options(
    676             extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
    677         ),
    678         cast_to=ChatCompletion,
    679         stream=stream or False,
    680         stream_cls=Stream[ChatCompletionChunk],
    681     )

File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:1179, in SyncAPIClient.post(self, path, cast_to, body, options, files, stream, stream_cls)
   1176 opts = FinalRequestOptions.construct(
   1177     method="post", url=path, json_data=body, files=to_httpx_files(files), **options
   1178 )
-> 1179 return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))

File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:868, in SyncAPIClient.request(self, cast_to, options, remaining_retries, stream, stream_cls)
    859 def request(
    860     self,
    861     cast_to: Type[ResponseT],
   (...)
    866     stream_cls: type[_StreamT] | None = None,
    867 ) -> ResponseT | _StreamT:
--> 868     return self._request(
    869         cast_to=cast_to,
    870         options=options,
    871         stream=stream,
    872         stream_cls=stream_cls,
    873         remaining_retries=remaining_retries,
    874     )

File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:921, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
    920 if retries > 0:
--> 921     return self._retry_request(
    922         options,
    923         cast_to,
    924         retries,
    925         stream=stream,
    926         stream_cls=stream_cls,
    927         response_headers=None,
    928     )
    930 log.debug("Raising connection error")

File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:992, in SyncAPIClient._retry_request(self, options, cast_to, remaining_retries, response_headers, stream, stream_cls)
    990 time.sleep(timeout)
--> 992 return self._request(
    993     options=options,
    994     cast_to=cast_to,
    995     remaining_retries=remaining,
    996     stream=stream,
    997     stream_cls=stream_cls,
    998 )

File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:921, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
    920 if retries > 0:
--> 921     return self._retry_request(
    922         options,
    923         cast_to,
    924         retries,
    925         stream=stream,
    926         stream_cls=stream_cls,
    927         response_headers=None,
    928     )
    930 log.debug("Raising connection error")

File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:992, in SyncAPIClient._retry_request(self, options, cast_to, remaining_retries, response_headers, stream, stream_cls)
    990 time.sleep(timeout)
--> 992 return self._request(
    993     options=options,
    994     cast_to=cast_to,
    995     remaining_retries=remaining,
    996     stream=stream,
    997     stream_cls=stream_cls,
    998 )

File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:931, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
    930     log.debug("Raising connection error")
--> 931     raise APIConnectionError(request=request) from err
    933 log.debug(
    934     'HTTP Request: %s %s "%i %s"', request.method, request.url, response.status_code, response.reason_phrase
    935 )

APIConnectionError: Connection error.

During handling of the above exception, another exception occurred:

NameError                                 Traceback (most recent call last)
Cell In[40], line 18, in llama_chat_openaiv2(promptActual, prompts, responses, model, temperature, max_tokens, verbose)
     15 try:
---> 18     response, error = llama_openaiv2(prompt=prompt,
     19                          add_inst=False,
     20                          model=model, 
     21                          temperature=temperature, 
     22                          max_tokens=max_tokens,
     23                          verbose=verbose,
     24                         )
     25 except ollama.ResponseError as e:

Cell In[32], line 30, in llama_openaiv2(prompt, add_inst, model, temperature, max_tokens, verbose)
     18     response = client.chat.completions.create(
     19         messages=[
     20                     {
   (...)
     27         temperature=temperature
     28             )
---> 30 except openai.APIError as e:
     31   #Handle API error here, e.g. retry or log
     32   print(f"Llama2: OpenAI API returned an API Error:{e} ")

NameError: name 'openai' is not defined

During handling of the above exception, another exception occurred:

NameError                                 Traceback (most recent call last)
Cell In[43], line 16
     11 responses = []
     13 prompt_1 = """
     14     What are fun activities I can do this weekend?
     15 """
---> 16 response_1 = llama_chat_openaiv2(prompt_1, prompts, responses)

Cell In[40], line 25, in llama_chat_openaiv2(promptActual, prompts, responses, model, temperature, max_tokens, verbose)
     15 try:
     18     response, error = llama_openaiv2(prompt=prompt,
     19                          add_inst=False,
     20                          model=model, 
   (...)
     23                          verbose=verbose,
     24                         )
---> 25 except ollama.ResponseError as e:
     26       print('Error:', e.content)
     27       #if e.status_code == 404:
     28       #      ollama.pull(model)

NameError: name 'ollama' is not defined

I also tried:


try:
  

        response, error = llama_openaiv2(prompt=prompt,
                             add_inst=False,
                             model=model, 
                             temperature=temperature, 
                             max_tokens=max_tokens,
                             verbose=verbose,
                            )
    except ollama.ResponseError as e:
          print('Error:', e.content)
          #if e.status_code == 404:
          #      ollama.pull(model)

according to ollama documentation...but no luck as you can see above to catch this errors.

1 - So is chat completions using the same try/catch errors ? Or isnt still implemented ? Or perhaps there are other errors/exceptions in ollama ?

2 - Related to the first one_ where can i find information about handling errors in ollama ?

Im using python.

openai 1.14.1
ollama 1.0.29

Thanks

Originally created by @ejgutierrez74 on GitHub (Mar 18, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/3221 Hi, im trying this code: ```python def llama_openaiv2(prompt, add_inst=True, #By default True, if you use a base model should write it as False model="llama2", temperature=0.0, #By default in openai is 1.0 o 0.7 depends of the model, openai from 0.0 to 2.0, llama2 from 0.0 to 1.0 max_tokens=1024, verbose=False ): if add_inst: prompt = f"[INST]{prompt}[/INST]" if verbose: print(f"Prompt:\n{prompt}\n") print(f"model: {model}") error = 0 try: response = client.chat.completions.create( messages=[ { 'role': 'user', 'content': prompt, } ], model=model, max_tokens=max_tokens, temperature=temperature ) except openai.APIError as e: #Handle API error here, e.g. retry or log print(f"Llama2: OpenAI API returned an API Error:{e} ") error = 1 pass except openai.APIConnectionError as e: #Handle connection error here print(f"Llama2:Failed to connect to OpenAI API:{e} ") error = 2 pass except openai.RateLimitError as e: #Handle rate limit error (we recommend using exponential backoff) print(f"Llama2: OpenAI API request exceeded rate limit:{e} ") error = 3 pass #Si interesa saber el response entero print(f"response object: {response}") #Solo vamos a devolver la respuesta del sistema return (response.choices[0].message.content, error) ``` So according to the original OpenAI API: https://platform.openai.com/docs/guides/error-codes/python-library-error-types This is an example code: ```python import openai from openai import OpenAI client = OpenAI() try: #Make your OpenAI API request here response = client.completions.create( prompt="Hello world", model="gpt-3.5-turbo-instruct" ) except openai.APIError as e: #Handle API error here, e.g. retry or log print(f"OpenAI API returned an API Error: {e}") pass except openai.APIConnectionError as e: #Handle connection error here print(f"Failed to connect to OpenAI API: {e}") pass except openai.RateLimitError as e: #Handle rate limit error (we recommend using exponential backoff) print(f"OpenAI API request exceeded rate limit: {e}") pass ``` But im facing that running my function, jupyter netbook raises and error, so somehow the errors arent catched properly. For example, if i dont have ollama server running, should give an Connection refused or Connection Error. This error should be catched, but my jupyter netbook seems cant catch it. In concrete i have this error when ollama serve is stopped: ``` --------------------------------------------------------------------------- ConnectionRefusedError Traceback (most recent call last) File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_exceptions.py:10, in map_exceptions(map) 9 try: ---> 10 yield 11 except Exception as exc: # noqa: PIE786 File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_backends/sync.py:206, in SyncBackend.connect_tcp(self, host, port, timeout, local_address, socket_options) 205 with map_exceptions(exc_map): --> 206 sock = socket.create_connection( 207 address, 208 timeout, 209 source_address=source_address, 210 ) 211 for option in socket_options: File /opt/anaconda3/lib/python3.11/socket.py:851, in create_connection(address, timeout, source_address, all_errors) 850 if not all_errors: --> 851 raise exceptions[0] 852 raise ExceptionGroup("create_connection failed", exceptions) File /opt/anaconda3/lib/python3.11/socket.py:836, in create_connection(address, timeout, source_address, all_errors) 835 sock.bind(source_address) --> 836 sock.connect(sa) 837 # Break explicitly a reference cycle ConnectionRefusedError: [Errno 111] Connection refused The above exception was the direct cause of the following exception: ConnectError Traceback (most recent call last) File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:67, in map_httpcore_exceptions() 66 try: ---> 67 yield 68 except Exception as exc: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:231, in HTTPTransport.handle_request(self, request) 230 with map_httpcore_exceptions(): --> 231 resp = self._pool.handle_request(req) 233 assert isinstance(resp.stream, typing.Iterable) File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py:268, in ConnectionPool.handle_request(self, request) 267 self.response_closed(status) --> 268 raise exc 269 else: File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py:251, in ConnectionPool.handle_request(self, request) 250 try: --> 251 response = connection.handle_request(request) 252 except ConnectionNotAvailable: 253 # The ConnectionNotAvailable exception is a special case, that 254 # indicates we need to retry the request on a new connection. (...) 258 # might end up as an HTTP/2 connection, but which actually ends 259 # up as HTTP/1.1. File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:99, in HTTPConnection.handle_request(self, request) 98 self._connect_failed = True ---> 99 raise exc 100 elif not self._connection.is_available(): File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:76, in HTTPConnection.handle_request(self, request) 75 try: ---> 76 stream = self._connect(request) 78 ssl_object = stream.get_extra_info("ssl_object") File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:124, in HTTPConnection._connect(self, request) 123 with Trace("connect_tcp", logger, request, kwargs) as trace: --> 124 stream = self._network_backend.connect_tcp(**kwargs) 125 trace.return_value = stream File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_backends/sync.py:205, in SyncBackend.connect_tcp(self, host, port, timeout, local_address, socket_options) 200 exc_map: ExceptionMapping = { 201 socket.timeout: ConnectTimeout, 202 OSError: ConnectError, 203 } --> 205 with map_exceptions(exc_map): 206 sock = socket.create_connection( 207 address, 208 timeout, 209 source_address=source_address, 210 ) File /opt/anaconda3/lib/python3.11/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback) 157 try: --> 158 self.gen.throw(typ, value, traceback) 159 except StopIteration as exc: 160 # Suppress StopIteration *unless* it's the same exception that 161 # was passed to throw(). This prevents a StopIteration 162 # raised inside the "with" statement from being suppressed. File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_exceptions.py:14, in map_exceptions(map) 13 if isinstance(exc, from_exc): ---> 14 raise to_exc(exc) from exc 15 raise ConnectError: [Errno 111] Connection refused The above exception was the direct cause of the following exception: ConnectError Traceback (most recent call last) File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:897, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls) 896 try: --> 897 response = self._client.send( 898 request, 899 stream=stream or self._should_stream_response_body(request=request), 900 **kwargs, 901 ) 902 except httpx.TimeoutException as err: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:915, in Client.send(self, request, stream, auth, follow_redirects) 913 auth = self._build_request_auth(request, auth) --> 915 response = self._send_handling_auth( 916 request, 917 auth=auth, 918 follow_redirects=follow_redirects, 919 history=[], 920 ) 921 try: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:943, in Client._send_handling_auth(self, request, auth, follow_redirects, history) 942 while True: --> 943 response = self._send_handling_redirects( 944 request, 945 follow_redirects=follow_redirects, 946 history=history, 947 ) 948 try: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:980, in Client._send_handling_redirects(self, request, follow_redirects, history) 978 hook(request) --> 980 response = self._send_single_request(request) 981 try: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:1016, in Client._send_single_request(self, request) 1015 with request_context(request=request): -> 1016 response = transport.handle_request(request) 1018 assert isinstance(response.stream, SyncByteStream) File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:230, in HTTPTransport.handle_request(self, request) 218 req = httpcore.Request( 219 method=request.method, 220 url=httpcore.URL( (...) 228 extensions=request.extensions, 229 ) --> 230 with map_httpcore_exceptions(): 231 resp = self._pool.handle_request(req) File /opt/anaconda3/lib/python3.11/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback) 157 try: --> 158 self.gen.throw(typ, value, traceback) 159 except StopIteration as exc: 160 # Suppress StopIteration *unless* it's the same exception that 161 # was passed to throw(). This prevents a StopIteration 162 # raised inside the "with" statement from being suppressed. File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:84, in map_httpcore_exceptions() 83 message = str(exc) ---> 84 raise mapped_exc(message) from exc ConnectError: [Errno 111] Connection refused During handling of the above exception, another exception occurred: ConnectionRefusedError Traceback (most recent call last) File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_exceptions.py:10, in map_exceptions(map) 9 try: ---> 10 yield 11 except Exception as exc: # noqa: PIE786 File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_backends/sync.py:206, in SyncBackend.connect_tcp(self, host, port, timeout, local_address, socket_options) 205 with map_exceptions(exc_map): --> 206 sock = socket.create_connection( 207 address, 208 timeout, 209 source_address=source_address, 210 ) 211 for option in socket_options: File /opt/anaconda3/lib/python3.11/socket.py:851, in create_connection(address, timeout, source_address, all_errors) 850 if not all_errors: --> 851 raise exceptions[0] 852 raise ExceptionGroup("create_connection failed", exceptions) File /opt/anaconda3/lib/python3.11/socket.py:836, in create_connection(address, timeout, source_address, all_errors) 835 sock.bind(source_address) --> 836 sock.connect(sa) 837 # Break explicitly a reference cycle ConnectionRefusedError: [Errno 111] Connection refused The above exception was the direct cause of the following exception: ConnectError Traceback (most recent call last) File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:67, in map_httpcore_exceptions() 66 try: ---> 67 yield 68 except Exception as exc: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:231, in HTTPTransport.handle_request(self, request) 230 with map_httpcore_exceptions(): --> 231 resp = self._pool.handle_request(req) 233 assert isinstance(resp.stream, typing.Iterable) File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py:268, in ConnectionPool.handle_request(self, request) 267 self.response_closed(status) --> 268 raise exc 269 else: File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py:251, in ConnectionPool.handle_request(self, request) 250 try: --> 251 response = connection.handle_request(request) 252 except ConnectionNotAvailable: 253 # The ConnectionNotAvailable exception is a special case, that 254 # indicates we need to retry the request on a new connection. (...) 258 # might end up as an HTTP/2 connection, but which actually ends 259 # up as HTTP/1.1. File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:99, in HTTPConnection.handle_request(self, request) 98 self._connect_failed = True ---> 99 raise exc 100 elif not self._connection.is_available(): File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:76, in HTTPConnection.handle_request(self, request) 75 try: ---> 76 stream = self._connect(request) 78 ssl_object = stream.get_extra_info("ssl_object") File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:124, in HTTPConnection._connect(self, request) 123 with Trace("connect_tcp", logger, request, kwargs) as trace: --> 124 stream = self._network_backend.connect_tcp(**kwargs) 125 trace.return_value = stream File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_backends/sync.py:205, in SyncBackend.connect_tcp(self, host, port, timeout, local_address, socket_options) 200 exc_map: ExceptionMapping = { 201 socket.timeout: ConnectTimeout, 202 OSError: ConnectError, 203 } --> 205 with map_exceptions(exc_map): 206 sock = socket.create_connection( 207 address, 208 timeout, 209 source_address=source_address, 210 ) File /opt/anaconda3/lib/python3.11/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback) 157 try: --> 158 self.gen.throw(typ, value, traceback) 159 except StopIteration as exc: 160 # Suppress StopIteration *unless* it's the same exception that 161 # was passed to throw(). This prevents a StopIteration 162 # raised inside the "with" statement from being suppressed. File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_exceptions.py:14, in map_exceptions(map) 13 if isinstance(exc, from_exc): ---> 14 raise to_exc(exc) from exc 15 raise ConnectError: [Errno 111] Connection refused The above exception was the direct cause of the following exception: ConnectError Traceback (most recent call last) File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:897, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls) 896 try: --> 897 response = self._client.send( 898 request, 899 stream=stream or self._should_stream_response_body(request=request), 900 **kwargs, 901 ) 902 except httpx.TimeoutException as err: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:915, in Client.send(self, request, stream, auth, follow_redirects) 913 auth = self._build_request_auth(request, auth) --> 915 response = self._send_handling_auth( 916 request, 917 auth=auth, 918 follow_redirects=follow_redirects, 919 history=[], 920 ) 921 try: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:943, in Client._send_handling_auth(self, request, auth, follow_redirects, history) 942 while True: --> 943 response = self._send_handling_redirects( 944 request, 945 follow_redirects=follow_redirects, 946 history=history, 947 ) 948 try: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:980, in Client._send_handling_redirects(self, request, follow_redirects, history) 978 hook(request) --> 980 response = self._send_single_request(request) 981 try: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:1016, in Client._send_single_request(self, request) 1015 with request_context(request=request): -> 1016 response = transport.handle_request(request) 1018 assert isinstance(response.stream, SyncByteStream) File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:230, in HTTPTransport.handle_request(self, request) 218 req = httpcore.Request( 219 method=request.method, 220 url=httpcore.URL( (...) 228 extensions=request.extensions, 229 ) --> 230 with map_httpcore_exceptions(): 231 resp = self._pool.handle_request(req) File /opt/anaconda3/lib/python3.11/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback) 157 try: --> 158 self.gen.throw(typ, value, traceback) 159 except StopIteration as exc: 160 # Suppress StopIteration *unless* it's the same exception that 161 # was passed to throw(). This prevents a StopIteration 162 # raised inside the "with" statement from being suppressed. File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:84, in map_httpcore_exceptions() 83 message = str(exc) ---> 84 raise mapped_exc(message) from exc ConnectError: [Errno 111] Connection refused During handling of the above exception, another exception occurred: ConnectionRefusedError Traceback (most recent call last) File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_exceptions.py:10, in map_exceptions(map) 9 try: ---> 10 yield 11 except Exception as exc: # noqa: PIE786 File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_backends/sync.py:206, in SyncBackend.connect_tcp(self, host, port, timeout, local_address, socket_options) 205 with map_exceptions(exc_map): --> 206 sock = socket.create_connection( 207 address, 208 timeout, 209 source_address=source_address, 210 ) 211 for option in socket_options: File /opt/anaconda3/lib/python3.11/socket.py:851, in create_connection(address, timeout, source_address, all_errors) 850 if not all_errors: --> 851 raise exceptions[0] 852 raise ExceptionGroup("create_connection failed", exceptions) File /opt/anaconda3/lib/python3.11/socket.py:836, in create_connection(address, timeout, source_address, all_errors) 835 sock.bind(source_address) --> 836 sock.connect(sa) 837 # Break explicitly a reference cycle ConnectionRefusedError: [Errno 111] Connection refused The above exception was the direct cause of the following exception: ConnectError Traceback (most recent call last) File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:67, in map_httpcore_exceptions() 66 try: ---> 67 yield 68 except Exception as exc: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:231, in HTTPTransport.handle_request(self, request) 230 with map_httpcore_exceptions(): --> 231 resp = self._pool.handle_request(req) 233 assert isinstance(resp.stream, typing.Iterable) File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py:268, in ConnectionPool.handle_request(self, request) 267 self.response_closed(status) --> 268 raise exc 269 else: File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py:251, in ConnectionPool.handle_request(self, request) 250 try: --> 251 response = connection.handle_request(request) 252 except ConnectionNotAvailable: 253 # The ConnectionNotAvailable exception is a special case, that 254 # indicates we need to retry the request on a new connection. (...) 258 # might end up as an HTTP/2 connection, but which actually ends 259 # up as HTTP/1.1. File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:99, in HTTPConnection.handle_request(self, request) 98 self._connect_failed = True ---> 99 raise exc 100 elif not self._connection.is_available(): File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:76, in HTTPConnection.handle_request(self, request) 75 try: ---> 76 stream = self._connect(request) 78 ssl_object = stream.get_extra_info("ssl_object") File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_sync/connection.py:124, in HTTPConnection._connect(self, request) 123 with Trace("connect_tcp", logger, request, kwargs) as trace: --> 124 stream = self._network_backend.connect_tcp(**kwargs) 125 trace.return_value = stream File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_backends/sync.py:205, in SyncBackend.connect_tcp(self, host, port, timeout, local_address, socket_options) 200 exc_map: ExceptionMapping = { 201 socket.timeout: ConnectTimeout, 202 OSError: ConnectError, 203 } --> 205 with map_exceptions(exc_map): 206 sock = socket.create_connection( 207 address, 208 timeout, 209 source_address=source_address, 210 ) File /opt/anaconda3/lib/python3.11/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback) 157 try: --> 158 self.gen.throw(typ, value, traceback) 159 except StopIteration as exc: 160 # Suppress StopIteration *unless* it's the same exception that 161 # was passed to throw(). This prevents a StopIteration 162 # raised inside the "with" statement from being suppressed. File /opt/anaconda3/lib/python3.11/site-packages/httpcore/_exceptions.py:14, in map_exceptions(map) 13 if isinstance(exc, from_exc): ---> 14 raise to_exc(exc) from exc 15 raise ConnectError: [Errno 111] Connection refused The above exception was the direct cause of the following exception: ConnectError Traceback (most recent call last) File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:897, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls) 896 try: --> 897 response = self._client.send( 898 request, 899 stream=stream or self._should_stream_response_body(request=request), 900 **kwargs, 901 ) 902 except httpx.TimeoutException as err: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:915, in Client.send(self, request, stream, auth, follow_redirects) 913 auth = self._build_request_auth(request, auth) --> 915 response = self._send_handling_auth( 916 request, 917 auth=auth, 918 follow_redirects=follow_redirects, 919 history=[], 920 ) 921 try: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:943, in Client._send_handling_auth(self, request, auth, follow_redirects, history) 942 while True: --> 943 response = self._send_handling_redirects( 944 request, 945 follow_redirects=follow_redirects, 946 history=history, 947 ) 948 try: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:980, in Client._send_handling_redirects(self, request, follow_redirects, history) 978 hook(request) --> 980 response = self._send_single_request(request) 981 try: File /opt/anaconda3/lib/python3.11/site-packages/httpx/_client.py:1016, in Client._send_single_request(self, request) 1015 with request_context(request=request): -> 1016 response = transport.handle_request(request) 1018 assert isinstance(response.stream, SyncByteStream) File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:230, in HTTPTransport.handle_request(self, request) 218 req = httpcore.Request( 219 method=request.method, 220 url=httpcore.URL( (...) 228 extensions=request.extensions, 229 ) --> 230 with map_httpcore_exceptions(): 231 resp = self._pool.handle_request(req) File /opt/anaconda3/lib/python3.11/contextlib.py:158, in _GeneratorContextManager.__exit__(self, typ, value, traceback) 157 try: --> 158 self.gen.throw(typ, value, traceback) 159 except StopIteration as exc: 160 # Suppress StopIteration *unless* it's the same exception that 161 # was passed to throw(). This prevents a StopIteration 162 # raised inside the "with" statement from being suppressed. File /opt/anaconda3/lib/python3.11/site-packages/httpx/_transports/default.py:84, in map_httpcore_exceptions() 83 message = str(exc) ---> 84 raise mapped_exc(message) from exc ConnectError: [Errno 111] Connection refused The above exception was the direct cause of the following exception: APIConnectionError Traceback (most recent call last) Cell In[32], line 18, in llama_openaiv2(prompt, add_inst, model, temperature, max_tokens, verbose) 17 try: ---> 18 response = client.chat.completions.create( 19 messages=[ 20 { 21 'role': 'user', 22 'content': prompt, 23 } 24 ], 25 model=model, 26 max_tokens=max_tokens, 27 temperature=temperature 28 ) 30 except openai.APIError as e: 31 #Handle API error here, e.g. retry or log File /opt/anaconda3/lib/python3.11/site-packages/openai/_utils/_utils.py:271, in required_args.<locals>.inner.<locals>.wrapper(*args, **kwargs) 270 raise TypeError(msg) --> 271 return func(*args, **kwargs) File /opt/anaconda3/lib/python3.11/site-packages/openai/resources/chat/completions.py:648, in Completions.create(self, messages, model, frequency_penalty, function_call, functions, logit_bias, logprobs, max_tokens, n, presence_penalty, response_format, seed, stop, stream, temperature, tool_choice, tools, top_logprobs, top_p, user, extra_headers, extra_query, extra_body, timeout) 599 @required_args(["messages", "model"], ["messages", "model", "stream"]) 600 def create( 601 self, (...) 646 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, 647 ) -> ChatCompletion | Stream[ChatCompletionChunk]: --> 648 return self._post( 649 "/chat/completions", 650 body=maybe_transform( 651 { 652 "messages": messages, 653 "model": model, 654 "frequency_penalty": frequency_penalty, 655 "function_call": function_call, 656 "functions": functions, 657 "logit_bias": logit_bias, 658 "logprobs": logprobs, 659 "max_tokens": max_tokens, 660 "n": n, 661 "presence_penalty": presence_penalty, 662 "response_format": response_format, 663 "seed": seed, 664 "stop": stop, 665 "stream": stream, 666 "temperature": temperature, 667 "tool_choice": tool_choice, 668 "tools": tools, 669 "top_logprobs": top_logprobs, 670 "top_p": top_p, 671 "user": user, 672 }, 673 completion_create_params.CompletionCreateParams, 674 ), 675 options=make_request_options( 676 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout 677 ), 678 cast_to=ChatCompletion, 679 stream=stream or False, 680 stream_cls=Stream[ChatCompletionChunk], 681 ) File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:1179, in SyncAPIClient.post(self, path, cast_to, body, options, files, stream, stream_cls) 1176 opts = FinalRequestOptions.construct( 1177 method="post", url=path, json_data=body, files=to_httpx_files(files), **options 1178 ) -> 1179 return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:868, in SyncAPIClient.request(self, cast_to, options, remaining_retries, stream, stream_cls) 859 def request( 860 self, 861 cast_to: Type[ResponseT], (...) 866 stream_cls: type[_StreamT] | None = None, 867 ) -> ResponseT | _StreamT: --> 868 return self._request( 869 cast_to=cast_to, 870 options=options, 871 stream=stream, 872 stream_cls=stream_cls, 873 remaining_retries=remaining_retries, 874 ) File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:921, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls) 920 if retries > 0: --> 921 return self._retry_request( 922 options, 923 cast_to, 924 retries, 925 stream=stream, 926 stream_cls=stream_cls, 927 response_headers=None, 928 ) 930 log.debug("Raising connection error") File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:992, in SyncAPIClient._retry_request(self, options, cast_to, remaining_retries, response_headers, stream, stream_cls) 990 time.sleep(timeout) --> 992 return self._request( 993 options=options, 994 cast_to=cast_to, 995 remaining_retries=remaining, 996 stream=stream, 997 stream_cls=stream_cls, 998 ) File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:921, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls) 920 if retries > 0: --> 921 return self._retry_request( 922 options, 923 cast_to, 924 retries, 925 stream=stream, 926 stream_cls=stream_cls, 927 response_headers=None, 928 ) 930 log.debug("Raising connection error") File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:992, in SyncAPIClient._retry_request(self, options, cast_to, remaining_retries, response_headers, stream, stream_cls) 990 time.sleep(timeout) --> 992 return self._request( 993 options=options, 994 cast_to=cast_to, 995 remaining_retries=remaining, 996 stream=stream, 997 stream_cls=stream_cls, 998 ) File /opt/anaconda3/lib/python3.11/site-packages/openai/_base_client.py:931, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls) 930 log.debug("Raising connection error") --> 931 raise APIConnectionError(request=request) from err 933 log.debug( 934 'HTTP Request: %s %s "%i %s"', request.method, request.url, response.status_code, response.reason_phrase 935 ) APIConnectionError: Connection error. During handling of the above exception, another exception occurred: NameError Traceback (most recent call last) Cell In[40], line 18, in llama_chat_openaiv2(promptActual, prompts, responses, model, temperature, max_tokens, verbose) 15 try: ---> 18 response, error = llama_openaiv2(prompt=prompt, 19 add_inst=False, 20 model=model, 21 temperature=temperature, 22 max_tokens=max_tokens, 23 verbose=verbose, 24 ) 25 except ollama.ResponseError as e: Cell In[32], line 30, in llama_openaiv2(prompt, add_inst, model, temperature, max_tokens, verbose) 18 response = client.chat.completions.create( 19 messages=[ 20 { (...) 27 temperature=temperature 28 ) ---> 30 except openai.APIError as e: 31 #Handle API error here, e.g. retry or log 32 print(f"Llama2: OpenAI API returned an API Error:{e} ") NameError: name 'openai' is not defined During handling of the above exception, another exception occurred: NameError Traceback (most recent call last) Cell In[43], line 16 11 responses = [] 13 prompt_1 = """ 14 What are fun activities I can do this weekend? 15 """ ---> 16 response_1 = llama_chat_openaiv2(prompt_1, prompts, responses) Cell In[40], line 25, in llama_chat_openaiv2(promptActual, prompts, responses, model, temperature, max_tokens, verbose) 15 try: 18 response, error = llama_openaiv2(prompt=prompt, 19 add_inst=False, 20 model=model, (...) 23 verbose=verbose, 24 ) ---> 25 except ollama.ResponseError as e: 26 print('Error:', e.content) 27 #if e.status_code == 404: 28 # ollama.pull(model) NameError: name 'ollama' is not defined ``` I also tried: ``` try: response, error = llama_openaiv2(prompt=prompt, add_inst=False, model=model, temperature=temperature, max_tokens=max_tokens, verbose=verbose, ) except ollama.ResponseError as e: print('Error:', e.content) #if e.status_code == 404: # ollama.pull(model) ``` according to ollama documentation...but no luck as you can see above to catch this errors. 1 - So is chat completions using the same try/catch errors ? Or isnt still implemented ? Or perhaps there are other errors/exceptions in ollama ? 2 - Related to the first one_ where can i find information about handling errors in ollama ? Im using python. openai 1.14.1 ollama 1.0.29 Thanks
Author
Owner

@ejgutierrez74 commented on GitHub (Mar 19, 2024):

Sorry the problem was that i missed import openai at the begining.

Can be closed by now.

<!-- gh-comment-id:2007753715 --> @ejgutierrez74 commented on GitHub (Mar 19, 2024): Sorry the problem was that i missed `import openai` at the begining. Can be closed by now.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#1984