These examples demonstrate the process of generating embeddings in 'udf' mode for a food review provided by a particular employee with ID as 3 stored in a TeradataML DataFrame. Refer to TextAnalyticsAI Example Setup for the prerequisite steps.
df_reviews = df_reviews.set_index('employee_id')
df_filtered_review = df_reviews.filter(items=[3], axis=0)
obj_aws.embeddings(column="reviews", data=df_filtered_review, volatile = True)
Output:
| articles | Embedding |
|---|---|
| The recent 2020 United States election have shown a shift in the political landscape With more youn | 0.024253,-0.004121,0.000308,0.087205,0.067200,0.007380,0.030201,0.091575,-0.055935,0.030420,0.025904,0.000988,-0.066180,-0.001812,0.041369,-0.041612,-0.058654,-0.015574,0.002109,0.021583,-0.007641,-0.017589,0.040908,0.021753,0.010549,0.000587,-0.026584,0.008182,-0.037047,0.057780, . . . |
| Climate change poses significant challenges globally affecting weather patterns ecosystems and human | 0.046721,-0.002385,0.095703,0.023556,-0.017135,0.018786,0.044461,-0.028793,0.028467,0.002525,-0.008442,0.032336,-0.059325,-0.018189,-0.029815,-0.023013,-0.002485,0.001455,-0.000577,0.044483,0.015092,-0.017167,-0.012397,0.024404,-0.028728,0.038942,-0.003116,0.046070,0.017830,0.023339, . . . |
| The 2020 Tokyo Olympics was postponed to 2021 due to the COVID-19 pandemic. This was the first time i | -0.024108,-0.053258,-0.009425,0.042024,-0.007488,-0.024067,-0.032287,0.035608,-0.009086,-0.017230,0.020643,0.028208,-0.007892,-0.031795,0.003406,-0.005494,-0.049691,-0.047190,0.002413,0.004456,0.037781,0.023124,-0.035259,-0.052561,0.014729,-0.017568,-0.007682,0.018091,0.029397,0.080318, . . . |
| The 2019 Amazon rainforest wildfires were a severe environmental crisis The fires burned thousands o | 0.014860,0.018978,0.036646,0.029410,0.042039,-0.053714,-0.005008,-0.000496,0.015682,-0.026680,-0.000567,-0.020298,-0.088562,-0.005688,-0.008007,0.046834,-0.014705,-0.037667,-0.020775,0.019111,-0.016880,-0.024260,-0.015448,-0.016148,0.010077,0.038177,-0.068453,0.036956,0.017080,0.050119, . . . |
| Renewable energy sources such as solar wind and hydroelectric power play a crucial role in reducing | -0.000105,0.073048,0.012078,0.015982,0.017800,0.039406,-0.026823,0.030630,0.026092,0.011852,0.019036,0.013465,-0.045472,0.002460,0.009104,0.036072,0.002162,-0.013573,0.017004,0.036503,-0.005625,-0.020951,0.030394,0.013390,-0.008636,0.021564,-0.007421,0.031211,0.032760,-0.024371, . . . |
df_reviews = df_reviews.set_index('employee_id')
df_filtered_review = df_reviews.filter(items=[3], axis=0)
obj_azure.embeddings(column="articles",data=data,accumulate='articles',output_format='VARCHAR')
Output:
| articles | Embedding |
|---|---|
| The recent 2020 United States election have shown a shift in the political landscape With more youn | 0.024253,-0.004121,0.000308,0.087205,0.067200,0.007380,0.030201,0.091575,-0.055935,0.030420,0.025904,0.000988,-0.066180,-0.001812,0.041369,-0.041612,-0.058654,-0.015574,0.002109,0.021583,-0.007641,-0.017589,0.040908,0.021753,0.010549,0.000587,-0.026584,0.008182,-0.037047,0.057780, . . . |
| Climate change poses significant challenges globally affecting weather patterns ecosystems and human | 0.046721,-0.002385,0.095703,0.023556,-0.017135,0.018786,0.044461,-0.028793,0.028467,0.002525,-0.008442,0.032336,-0.059325,-0.018189,-0.029815,-0.023013,-0.002485,0.001455,-0.000577,0.044483,0.015092,-0.017167,-0.012397,0.024404,-0.028728,0.038942,-0.003116,0.046070,0.017830,0.023339, . . . |
| The 2020 Tokyo Olympics was postponed to 2021 due to the COVID-19 pandemic. This was the first time i | -0.024108,-0.053258,-0.009425,0.042024,-0.007488,-0.024067,-0.032287,0.035608,-0.009086,-0.017230,0.020643,0.028208,-0.007892,-0.031795,0.003406,-0.005494,-0.049691,-0.047190,0.002413,0.004456,0.037781,0.023124,-0.035259,-0.052561,0.014729,-0.017568,-0.007682,0.018091,0.029397,0.080318, . . . |
| The 2019 Amazon rainforest wildfires were a severe environmental crisis The fires burned thousands o | 0.014860,0.018978,0.036646,0.029410,0.042039,-0.053714,-0.005008,-0.000496,0.015682,-0.026680,-0.000567,-0.020298,-0.088562,-0.005688,-0.008007,0.046834,-0.014705,-0.037667,-0.020775,0.019111,-0.016880,-0.024260,-0.015448,-0.016148,0.010077,0.038177,-0.068453,0.036956,0.017080,0.050119, . . . |
| Renewable energy sources such as solar wind and hydroelectric power play a crucial role in reducing | -0.000105,0.073048,0.012078,0.015982,0.017800,0.039406,-0.026823,0.030630,0.026092,0.011852,0.019036,0.013465,-0.045472,0.002460,0.009104,0.036072,0.002162,-0.013573,0.017004,0.036503,-0.005625,-0.020951,0.030394,0.013390,-0.008636,0.021564,-0.007421,0.031211,0.032760,-0.024371, . . . |
df_reviews = df_reviews.set_index('employee_id')
df_filtered_review = df_reviews.filter(items=[3], axis=0)
obj_gcp.embeddings(column="articles",data=data,accumulate='articles',output_format='VARCHAR')
Output:
| articles | Embedding |
|---|---|
| The recent 2020 United States election have shown a shift in the political landscape With more youn | 0.024253,-0.004121,0.000308,0.087205,0.067200,0.007380,0.030201,0.091575,-0.055935,0.030420,0.025904,0.000988,-0.066180,-0.001812,0.041369,-0.041612,-0.058654,-0.015574,0.002109,0.021583,-0.007641,-0.017589,0.040908,0.021753,0.010549,0.000587,-0.026584,0.008182,-0.037047,0.057780, . . . |
| Climate change poses significant challenges globally affecting weather patterns ecosystems and human | 0.046721,-0.002385,0.095703,0.023556,-0.017135,0.018786,0.044461,-0.028793,0.028467,0.002525,-0.008442,0.032336,-0.059325,-0.018189,-0.029815,-0.023013,-0.002485,0.001455,-0.000577,0.044483,0.015092,-0.017167,-0.012397,0.024404,-0.028728,0.038942,-0.003116,0.046070,0.017830,0.023339, . . . |
| The 2020 Tokyo Olympics was postponed to 2021 due to the COVID-19 pandemic. This was the first time i | -0.024108,-0.053258,-0.009425,0.042024,-0.007488,-0.024067,-0.032287,0.035608,-0.009086,-0.017230,0.020643,0.028208,-0.007892,-0.031795,0.003406,-0.005494,-0.049691,-0.047190,0.002413,0.004456,0.037781,0.023124,-0.035259,-0.052561,0.014729,-0.017568,-0.007682,0.018091,0.029397,0.080318, . . . |
| The 2019 Amazon rainforest wildfires were a severe environmental crisis The fires burned thousands o | 0.014860,0.018978,0.036646,0.029410,0.042039,-0.053714,-0.005008,-0.000496,0.015682,-0.026680,-0.000567,-0.020298,-0.088562,-0.005688,-0.008007,0.046834,-0.014705,-0.037667,-0.020775,0.019111,-0.016880,-0.024260,-0.015448,-0.016148,0.010077,0.038177,-0.068453,0.036956,0.017080,0.050119, . . . |
| Renewable energy sources such as solar wind and hydroelectric power play a crucial role in reducing | -0.000105,0.073048,0.012078,0.015982,0.017800,0.039406,-0.026823,0.030630,0.026092,0.011852,0.019036,0.013465,-0.045472,0.002460,0.009104,0.036072,0.002162,-0.013573,0.017004,0.036503,-0.005625,-0.020951,0.030394,0.013390,-0.008636,0.021564,-0.007421,0.031211,0.032760,-0.024371, . . . |
Example setup: Import required packages and set up input data
>>> import os
>>> import teradatagenai
>>> from teradatagenai import TeradataAI, TextAnalyticsAI, load_data
>>> from teradataml import DataFrame
>>> load_data('employee', 'employee_data')
>>> data = DataFrame('employee_data')
>>> df_articles = data.select(["employee_id", "employee_name", "articles"])
Complete the example setup requirements.
Example 1: Generate the embeddings for employee reviews from the 'reviews' column of a teradataml DataFrame using hugging face model 'all-MiniLM-L6-v2'
Create an LLM endpoint.
>>> model_name = 'sentence-transformers/all-MiniLM-L6-v2'
>>> model_args = {'transformer_class': 'AutoModel',
'task' : 'token-classification'}
>>> llm = TeradataAI(api_type = "hugging_face",
model_name = model_name,
model_args = model_args)
Create a TextAnalyticsAI object.
>>> obj = TextAnalyticsAI(llm=llm)
Generate embeddings for input text column.
>>> obj.embeddings(column = "articles",
data = df_articles,
libs = 'sentence_transformers',
delimiter = '#')
Output:
| text | v1 | v2 | v3 | v4 | v5 | v6 | v7 | v8 | v9 | v10 |
|---|---|---|---|---|---|---|---|---|---|---|
| The recent 2020 United States election have shown a shift in the political landscape With more youn | -0.001082 | -0.079335 | 0.087804 | 0.01332 | 0.078489 | 0.05352 | -0.026334 | 0.003003 | -0.0058 | -0.009414 |
| The 2019 Amazon rainforest wildfires were a severe environmental crisis The fires burned thousands o | 0.088247 | 0.069516 | 0.035314 | 0.025108 | 0.107199 | 0.028076 | -0.076618 | 0.013257 | 0.021908 | 0.091251 |
| The 2020 Tokyo Olympics was postponed to 2021 due to the COVID-19 pandemic. This was the first time i | -0.010205 | 0.009119 | 0.096047 | 0.020484 | 0.038536 | 0.03304 | 0.014376 | 0.024337 | -0.014955 | 0.050129 |
| Climate change poses significant challenges globally affecting weather patterns ecosystems and human | -0.009864 | 0.051061 | 0.117482 | 0.018262 | 0.053465 | -0.000745 | -0.100715 | -0.069582 | -0.037874 | 0.020041 |
| Renewable energy sources such as solar wind and hydroelectric power play a crucial role in reducing | -0.002048 | 0.169067 | 0.088543 | 0.031041 | 0.069353 | 0.042704 | -0.016924 | -0.034878 | 0.028716 | 0.025563 |
Example 2: Extend Example 1 to use user defined script as input
>>> base_dir = os.path.dirname(teradatagenai.__file__)
>>> embeddings_script = os.path.join(base_dir,
'example-data',
'embeddings.py')
Construct returns argument based on the user defined script.
>>> returns = OrderedDict([('text', VARCHAR(512))])
>>> _ = [returns.update({"v{}".format(i+1): VARCHAR(1000)}) for i in range(384)]
>>> obj.embeddings(column = "articles",
data = df_articles,
script = embeddings_script,
returns = returns,
libs = 'sentence_transformers',
delimiter = '#')
Output:
| text | v1 | v2 | v3 | v4 | v5 | v6 | v7 | v8 | v9 | v10 |
|---|---|---|---|---|---|---|---|---|---|---|
| The recent 2020 United States election have shown a shift in the political landscape With more youn | -0.001082 | -0.079335 | 0.087804 | 0.01332 | 0.078489 | 0.05352 | -0.026334 | 0.003003 | -0.0058 | -0.009414 |
| The 2019 Amazon rainforest wildfires were a severe environmental crisis The fires burned thousands o | 0.088247 | 0.069516 | 0.035314 | 0.025108 | 0.107199 | 0.028076 | -0.076618 | 0.013257 | 0.021908 | 0.091251 |
| The 2020 Tokyo Olympics was postponed to 2021 due to the COVID-19 pandemic. This was the first time i | -0.010205 | 0.009119 | 0.096047 | 0.020484 | 0.038536 | 0.03304 | 0.014376 | 0.024337 | -0.014955 | 0.050129 |
| Climate change poses significant challenges globally affecting weather patterns ecosystems and human | -0.009864 | 0.051061 | 0.117482 | 0.018262 | 0.053465 | -0.000745 | -0.100715 | -0.069582 | --0.037874 | 0.020041 |
| Renewable energy sources such as solar wind and hydroelectric power play a crucial role in reducing | -0.002048 | 0.169067 | 0.088543 | 0.031041 | 0.069353 | 0.042704 | -0.016924 | -0.034878 | 0.028716 | 0.025563 |
df_reviews = df_reviews.set_index('employee_id')
df_filtered_review = df_reviews.filter(items=[2], axis=0)
obj_nim.embeddings(column="articles",data=data,accumulate='articles',output_format='VARCHAR')
Output:
| articles | Embedding |
|---|---|
| The 2020 Tokyo Olympics was postponed to 2021 due to the COVID-19 pandemic. This was the first time i | 0.049072,-0.074768,-0.000561,-0.038086,0.031021,-0.003189,-0.023376,-0.010620,-0.025040,-0.043823,0.057892,-0.018570,0.012009,0.007942,0.027405,0.019241,0.063782,-0.012543,-0.004662,0.029129,-0.037445,0.016602,-0.027222,-0.001147,0.004402,-0.047241,-0.015823,-0.007942,-0.007919,0.002132, . . . |
| Climate change poses significant challenges globally affecting weather patterns ecosystems and human | 0.016281,-0.067200,0.004745,0.030960,0.049286,-0.010040,-0.071472,-0.036499,-0.024384,-0.035217,0.043549,-0.006405,-0.017914,0.018951,-0.024063,0.044403,0.048401,-0.003736,0.030884,-0.004692,-0.000097,-0.014496,-0.006645,-0.016800,0.011024,0.016159,-0.019638,-0.002644,-0.006935,0.015930, . . . |
Import the modules and create a teradataml DataFrame.
>>> from teradatagenai import TeradataAI, TextAnalyticsAI, load_data
>>> from teradataml import DataFrame
>>> load_data("byom", "amazon_reviews_25")
>>> amazon_reviews_25 = DataFrame.from_table("amazon_reviews_25")
Set up a TeradataAI onnx endpoint.
>>> llm_onnx= TeradataAI(api_type = "onnx",
model_name = "bge-small-en-v1.5",
model_id = "td-bge-small",
model_path = "/path/to/onnx/model",
tokenizer_path = "/path/to/onnx/tokenizer,
table_name = "onnx_models")
Create a TextAnalyticsAI object and generate embeddings for 'rev_text' column in amazon_reviews_25 teradataml DataFrame.
>>> obj = TextAnalyticsAI(llm=llm_onnx)
>>> obj.embeddings(data=amazon_reviews_25,
column = "rev_text",
accumulate= "rev_id",
model_output_tensor = "sentence_embedding")
Output:
| rev_id | SENTENCE_EMBEDDING |
|---|---|
| A10000012B7CGYKOMPQ4L | b'60D343B514A673DF91E84BB58203. . . . ' |
| A26GKZPS079GFF | b'6B18CEBB2333693C2FAE523DDD11. . . . ' |
| A2S166WSCFIFP5 | b'229DB3C0ED68FBB4E3EB0BCD3F38. . . . ' |
| A1MOSTXNIO5MPJ | b'-5C03F1437F90A0C27C9EB4C24FC. . . . ' |
| A3FFNE1DR5SI1W | b'7196C53CBDB6873C6E24173DD249. . . . ' |
| A1KQ80Y692CDOI | b'-66BAAC380DB81C351214F43E4DA. . . . ' |
| A1BM81XB4QHOA3 | b'15A0BBD2454583C6F9E173D56633. . . . ' |
| A1340OFLZBW5NG | b'2C3E9DBCF762B73CB133A0BC1766. . . . ' |
| A5E9TSD20U9PR | b'-5056BD433C1CC3C20F783DC4894. . . . ' |
| A3V1MKC2BVWY48 | b'-381F5C3ADFEECC242B66DC3C3BE. . . . ' |