Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I use langchain's OpenAIEmbeddings to access my deployed like openai service, it's not working properly #21015

Open
5 tasks done
luckfu opened this issue Apr 29, 2024 · 0 comments
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: embeddings Related to text embedding models module 🔌: openai Primarily related to OpenAI integrations

Comments

@luckfu
Copy link

luckfu commented Apr 29, 2024

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

  • It works properly when accessed using curl.
! curl http://127.0.0.1:3000/v1/embeddings \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer Aw31nIfa2ONFI5dBeD59bFfB04194917fF1A15f5286F3" \
 -d '{ \
  "input": "Your text string goes here", \
  "model": "bge-small-zh" \
}'
  • I can also be properly called using the openai API
from openai import OpenAI
client = OpenAI(api_key="Aw31nIfa2ONFI5dBeD59bFfB04194917fF1A15f5286F3",
               base_url = "http://127.0.0.1:3000/v1")

def get_embedding(text_or_tokens, model="bge-small-zh"):
    return client.embeddings.create(input=text_or_tokens, model=model).data[0].embedding
  • but,I am unable to get langchain's OpenAIEmbeddings to work
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(
    model="bge-small-zh",
    openai_api_base="http://127.0.0.1:3000/v1",
    openai_api_key="Aw31nIfa2ONFI5dBeD59bFfB04194917fF1A15f5286F3"
)

Error Message and Stack Trace (if applicable)


InternalServerError Traceback (most recent call last)
Cell In[7], line 1
----> 1 query_result = embeddings.embed_query("haha")

File /data/miniconda3/lib/python3.12/site-packages/langchain_openai/embeddings/base.py:573, in OpenAIEmbeddings.embed_query(self, text)
564 def embed_query(self, text: str) -> List[float]:
565 """Call out to OpenAI's embedding endpoint for embedding query text.
566
567 Args:
(...)
571 Embedding for the text.
572 """
--> 573 return self.embed_documents([text])[0]

File /data/miniconda3/lib/python3.12/site-packages/langchain_openai/embeddings/base.py:532, in OpenAIEmbeddings.embed_documents(self, texts, chunk_size)
529 # NOTE: to keep things simple, we assume the list may contain texts longer
530 # than the maximum context and use length-safe embedding function.
531 engine = cast(str, self.deployment)
--> 532 return self._get_len_safe_embeddings(texts, engine=engine)

File /data/miniconda3/lib/python3.12/site-packages/langchain_openai/embeddings/base.py:336, in OpenAIEmbeddings._get_len_safe_embeddings(self, texts, engine, chunk_size)
334 batched_embeddings: List[List[float]] = []
335 for i in _iter:
--> 336 response = self.client.create(
337 input=tokens[i : i + _chunk_size], **self._invocation_params
338 )
339 if not isinstance(response, dict):
340 response = response.model_dump()

File /data/miniconda3/lib/python3.12/site-packages/openai/resources/embeddings.py:114, in Embeddings.create(self, input, model, dimensions, encoding_format, user, extra_headers, extra_query, extra_body, timeout)
108 embedding.embedding = np.frombuffer( # type: ignore[no-untyped-call]
109 base64.b64decode(data), dtype="float32"
110 ).tolist()
112 return obj
--> 114 return self._post(
115 "/embeddings",
116 body=maybe_transform(params, embedding_create_params.EmbeddingCreateParams),
117 options=make_request_options(
118 extra_headers=extra_headers,
119 extra_query=extra_query,
120 extra_body=extra_body,
121 timeout=timeout,
122 post_parser=parser,
123 ),
124 cast_to=CreateEmbeddingResponse,
125 )

File /data/miniconda3/lib/python3.12/site-packages/openai/_base_client.py:1232, in SyncAPIClient.post(self, path, cast_to, body, options, files, stream, stream_cls)
1218 def post(
1219 self,
1220 path: str,
(...)
1227 stream_cls: type[_StreamT] | None = None,
1228 ) -> ResponseT | _StreamT:
1229 opts = FinalRequestOptions.construct(
1230 method="post", url=path, json_data=body, files=to_httpx_files(files), **options
1231 )
-> 1232 return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))

File /data/miniconda3/lib/python3.12/site-packages/openai/_base_client.py:921, in SyncAPIClient.request(self, cast_to, options, remaining_retries, stream, stream_cls)
912 def request(
913 self,
914 cast_to: Type[ResponseT],
(...)
919 stream_cls: type[_StreamT] | None = None,
920 ) -> ResponseT | _StreamT:
--> 921 return self._request(
922 cast_to=cast_to,
923 options=options,
924 stream=stream,
925 stream_cls=stream_cls,
926 remaining_retries=remaining_retries,
927 )

File /data/miniconda3/lib/python3.12/site-packages/openai/_base_client.py:997, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
995 if retries > 0 and self._should_retry(err.response):
996 err.response.close()
--> 997 return self._retry_request(
998 options,
999 cast_to,
1000 retries,
1001 err.response.headers,
1002 stream=stream,
1003 stream_cls=stream_cls,
1004 )
1006 # If the response is streamed then we need to explicitly read the response
1007 # to completion before attempting to access the response text.
1008 if not err.response.is_closed:

File /data/miniconda3/lib/python3.12/site-packages/openai/_base_client.py:1045, in SyncAPIClient._retry_request(self, options, cast_to, remaining_retries, response_headers, stream, stream_cls)
1041 # In a synchronous context we are blocking the entire thread. Up to the library user to run the client in a
1042 # different thread if necessary.
1043 time.sleep(timeout)
-> 1045 return self._request(
1046 options=options,
1047 cast_to=cast_to,
1048 remaining_retries=remaining,
1049 stream=stream,
1050 stream_cls=stream_cls,
1051 )

File /data/miniconda3/lib/python3.12/site-packages/openai/_base_client.py:997, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
995 if retries > 0 and self._should_retry(err.response):
996 err.response.close()
--> 997 return self._retry_request(
998 options,
999 cast_to,
1000 retries,
1001 err.response.headers,
1002 stream=stream,
1003 stream_cls=stream_cls,
1004 )
1006 # If the response is streamed then we need to explicitly read the response
1007 # to completion before attempting to access the response text.
1008 if not err.response.is_closed:

File /data/miniconda3/lib/python3.12/site-packages/openai/_base_client.py:1045, in SyncAPIClient._retry_request(self, options, cast_to, remaining_retries, response_headers, stream, stream_cls)
1041 # In a synchronous context we are blocking the entire thread. Up to the library user to run the client in a
1042 # different thread if necessary.
1043 time.sleep(timeout)
-> 1045 return self._request(
1046 options=options,
1047 cast_to=cast_to,
1048 remaining_retries=remaining,
1049 stream=stream,
1050 stream_cls=stream_cls,
1051 )

File /data/miniconda3/lib/python3.12/site-packages/openai/_base_client.py:1012, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
1009 err.response.read()
1011 log.debug("Re-raising status error")
-> 1012 raise self._make_status_error_from_response(err.response) from None
1014 return self._process_response(
1015 cast_to=cast_to,
1016 options=options,
(...)
1019 stream_cls=stream_cls,
1020 )

InternalServerError: Error code: 500 - {'error': {'message': 'bad response status code 500 (request id: 20240429183824796230302KI7yGPut)', 'type': 'upstream_error', 'param': '500', 'code': 'bad_response_status_code'}}

Description

I would like langchain's OpenAIEmbeddings to work properly, just like the openai API

System Info

DEPRECATION: Loading egg at /data/miniconda3/lib/python3.12/site-packages/sacremoses-0.0.43-py3.8.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at pypa/pip#12330
DEPRECATION: Loading egg at /data/miniconda3/lib/python3.12/site-packages/huggingface_hub-0.22.2-py3.8.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at pypa/pip#12330
langchain @ file:///home/conda/feedstock_root/build_artifacts/langchain_1712896599223/work
langchain-community @ file:///home/conda/feedstock_root/build_artifacts/langchain-community_1713569638904/work
langchain-core==0.1.46
langchain-openai==0.1.4
langchain-text-splitters @ file:///home/conda/feedstock_root/build_artifacts/langchain-text-splitters_1709389732771/work

@dosubot dosubot bot added Ɑ: embeddings Related to text embedding models module 🔌: openai Primarily related to OpenAI integrations 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: embeddings Related to text embedding models module 🔌: openai Primarily related to OpenAI integrations
Projects
None yet
Development

No branches or pull requests

1 participant